christophecvr / macos-script-login-logout

Launchagent script to execute a command or script-file at user login or logout

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

macOS (macOSX) Login/Logout Script .

Launchagent script to execute a command or shell script at user login or logout. This works on macOS High Sierra 10.13.6 Last avbl MacOs on my hardware (mid 2010) It will work on the High Sierra and most probably on on some older osx versions. The newer I don't I just can't test it. If someone with MacOs higher then High Sierra 10.13.6 Test this, that would be nice.

Automatic Install and Uninstall with install.sh

If the service was already installed and or loaded , the service will first be unloaded and uninstalled.

If the user answered Yes to the question to uninstall. Else the script will be stopped.

After uninstallation User can answer Yes to reinstall the service or No to just have it removed.

When install,uninstall and or reinstall is done user can choose to keep or remove git repo.

Logging of this script.

After Automatic install the logs will be in the users default log directory which is:

  • $HOME/Library/Logs/login-logout.log
  • $HOME/Library/Logs/login-logout.err

example if You're user alfred it will be /Users/alfred/Library/Logs/login-logout.log From out of finder You find the Library map in you're home map. But finder uses local language if choosen by user. In dutch for example they call the Library map Bibliotheek.

Off course if during install You change the logs location the logs will be somewhere else. There is well a limit You can not set a log path below users home dir. This is the same for the script file login-logout.sh You can change the location during install but it must be in You're home dir or above.

  • Setting for example log files in a own created path example:
  • /Users/alfred/logs is allowed (the path Must well exist !!)
  • /private/var/logs is not allowed !!!

After Installation there will be a install.log file in

  • $HOME/Library/Logs/login-logout-script-install.log

Manual Install :

There are two files:

  • login-logout-script.plist
  • login-logout.sh: bash shell script

Install

Customize the login-logout-script.plist: according explanation in file.

Then copy the file login-logout-script.plist to $HOME/Library/LaunchAgents/

Customize the login-logout.sh to you're needs place it in $HOME/Library/Scripts .

  • Make script executable.
  • chmod 755 $HOME/Library/Scripts/login-logout.sh

To start the login logout service

  • open terminal
  • type : launchctl load -w ~/Library/LaunchAgents/login-logout-script.plist

To stop the login logout service

  • open terminal
  • type : launchctl unload -w ~/Library/LaunchAgents/login-logout-script.plist

The script will be started at every user login. Stopped on user logout. If you only wan't to load it manually set the key RunAtLoad into plist to false.

  • then start manually :
  • launchctl start LOGIN.LOGOUT.SERVICE
  • to stop
  • launchctl stop LOGIN.LOGOUT.SERVICE

After changing file login-logout.sh

unload and reload the service in order to have changes directly applied

  • open terminal
  • type : launchctl unload -w ~/Library/LaunchAgents/login-logout-script.plist
  • type : launchctl load -w ~/Library/LaunchAgents/login-logout-script.plist

Alternatively just logout,login,logout,login and You're changes should be in effect.

Links

About

Launchagent script to execute a command or script-file at user login or logout

License:GNU General Public License v2.0


Languages

Language:Shell 100.0%