MacOS – alert if application isn’t running

applescriptmacos

I'm currently having a bit of a nightmare as the company secretary's Mac has failed to run OneDrive since June. I use this to sync a Sharepoint document store that is effectively the company file server. The secretary has been working on a fairly significant number of files (and some pretty important ones regarding finance/invoices, recruitment, payroll, etc,) and did not noticed/mentioned that OneDrive hasn't been running.

Because of this problem, I've got ~500 files that I need to identify and work out how to sync/merge without over writing other people's changes… :/

I've effectively fixed her OneDrive for the time being — it starts at startup and logs in and syncs as expected. I'm nervous, however, that OneDrive will stop again and we'll be back in the same boat, and I don't feel that asking her to keep an eye on it is a long term or a reliable solution.

What I was thinking of doing was to write a script that monitors for the OneDrive process and will fire an alert of some kind if it's not running. I am not a Mac expert however so I'm at a bit of a loss as how best to achieve that.

I guess AppleScript and a cron job is my best option. Can anyone advise if there is a better method, and can anyone help me getting started with AppleScript to detect a process and send an email warning if it's not detected?

EDIT : Process name is OneDrive

Best Answer

IMO your best bet would be to use launchd to make sure that OneDrive is always running no matter what.

(launchd is a bit like cron but much more powerful.)

Save this file as ~/Library/LaunchAgents/com.tjluoma.keep-onedrive-alive.plist (where ~ is the home directory on the secretary's computer):

<?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>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.tjluoma.keep-onedrive-alive</string>
    <key>Program</key>
    <string>/Applications/OneDrive.app/Contents/MacOS/OneDrive</string>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Then load it in launchd using this command:

launchctl load ~/Library/LaunchAgents/com.tjluoma.keep-onedrive-alive.plist

That will:

a) Launch OneDrive

b) Re-Launch OneDrive if it is quit or crashes. Basically anytime it stops running, it will automatically start again.

Note that you should UN-check the option in OneDrive's preferences to open at login, because launchd will be in charge of running it.

OneDrive Preferences