Make the file executable:

chmod +x ~/com.script.startCam.sh

Login

Using launchd Create a file: (~)/Library/LaunchAgents/com.script.startCam.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.script.startCam</string>
    <key>Program</key>
    <string>/Library/Scripts/poweron_cam</string>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

The following example shows a complete job definition with only three keys:

  • Label This key is required for every job definition. It identifies the job and has to be unique for the launchd instance. Theoretically it is possible for an agent to have the same label as a daemon, as daemons are loaded by the root launchd whereas agents are loaded by a user launchd, but it is not recommended.
  • Program This key defines what to start, in this case a shell script /Users/Me/Scripts/cleanup.sh.
  • RunAtLoad This is one of several optional keys specifying when the job should be run, in this case right after it has been loaded.

Now, let’s add it as a Login Hook:

sudo defaults write com.apple.loginwindow LoginHook /usr/local/bin/script.sh 
sudo defaults write com.apple.loginwindow LogoutHook /usr/local/bin/script2.sh
sudo defaults delete com.apple.loginwindow LoginHook
sudo defaults delete com.apple.loginwindow LogoutHook

Reference