sag.95 sag.95 - 1 year ago 65
Bash Question

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" "">
<plist version="1.0">

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?

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download