3

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?

sag.95
  • 33
  • 3

1 Answers1

4

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

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • Thank you for your answer! Is there anyway to have it run whenever the system is on? If the user logs in and then out, will it still be loaded? Sorry, I am still new to this tool. – sag.95 Nov 03 '16 at 16:23
  • 1
    @sag.95 It sounds like you want to load it as a launch daemon rather than a launch agent. The difference is that agents run within user sessions, as the logged-in user, while daemons run in system context independent of who is logged in (if anyone), and usually run as root (making `sudo` unnecessary/irrelevant). To load it in the current boot as a daemon, use `sudo launchctl load`. To get it to auto-load next time the computer boots, put it in /Library/LaunchDaemons, and make sure the file is owned by root, group is wheel, and permissions are 644. – Gordon Davisson Nov 03 '16 at 16:53
  • @sag.95 BTW, if you make it a launch daemon, it's going to make the system rather hard to work with in some ways. For example, if you need to install a system update, and the system restarts partway through, it could be ... bad. You can temporarily disable daemons (and agents) from the /Library/Launch* folders by holding the shift key as the computer starts up, which invokes [safe mode](https://support.apple.com/en-us/HT201262). – Gordon Davisson Nov 03 '16 at 16:57
  • Thank you for your help. I am trying to do this because my end goal is to have all of my client's mac computers restart every night so the central server can update successfully. I am going to be scheduling the restart to eventually be happening sometime in the early hours, like 2am every morning. Do you think this is a good idea? – sag.95 Nov 03 '16 at 17:00
  • @sag.95 Forcing a restart is somewhat dangerous, because you don't know what's going on on the computer. The most common problem will be that someone has a document open, with unsaved changes; if you force a restart, their edits will be lost. Restarting in the middle of some system operation will be less common, but potentially disastrous. So I can't really recommend it. – Gordon Davisson Nov 03 '16 at 17:07
  • If the files are being lost because it wasn't saved, that is fine. I am just unsure about restarting in the middle of a system update or operation. Do you know of any workaround? – sag.95 Nov 03 '16 at 17:13
  • @sag.95 I don't know of any; sorry. – Gordon Davisson Nov 03 '16 at 22:54