sag.95
sag.95

Reputation: 43

Launchd task unloads when computer restarts

I have a simple launchd plist file that I am using to run a shell script that is designed to restart the computer:

<?xml vesion="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.sag95.restartscript</string>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/sag95/Desktop/Scripts/restartscript.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>300</integer>
</dict>
</plist>

The script file is a single line force restart command(password in real file, not placed here):

echo <password> | sudo -S shutdown -r now

Once I load the plist file using

launchctl load com.sag95.restartscript.plist

it appears in the list (launchctl list). Currently I have the plist file to run the script every 5 minutes. After the 5 minutes of loading, the script is called the first time and subsequently restarts the computer. Once I login, the launchd plist file is no longer in the list (launchctl list). I waited 30 minutes and it hasn't run again to restart my computer.

My question is why is the com.sag95.restartscript plist file unloading after the restart/force reboot?

Upvotes: 4

Views: 1565

Answers (1)

Gordon Davisson
Gordon Davisson

Reputation: 125708

launchctl load only loads it for the current session. Next time you log in, it loads your list of launch agents from /System/Library/LaunchAgents/*.plist, /Library/LaunchAgents/*.plist, and /Users/sag95/Library/LaunchAgents/*.plist. If the file isn't in one of these folders, it will not get loaded.

Assuming you want it to run only when you are logged in, place the file in /Users/sag95/Library/LaunchAgents. If you wanted it to run for any logged in user, put it in /Library/LaunchAgents instead (but with this specific script, the password will only work for your account, so this wouldn't be useful).

Upvotes: 5

Related Questions