Skip to content

Instantly share code, notes, and snippets.

@markwk
Created October 31, 2018 12:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwk/e5db251adcc15caca83e270fecb7947e to your computer and use it in GitHub Desktop.
Save markwk/e5db251adcc15caca83e270fecb7947e to your computer and use it in GitHub Desktop.
Python Script for Mac to Check if a process is running. If not, restart and post a notification.
#!/usr/bin/env python
"""
Check to see if an process is running. If not, restart.
Run this in a cron job
"""
import os
import subprocess
# notification via osascript
# TODO: Is there a better way to do this?
def notify(title, text):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))
process_name= "RescueTime" # change this to the name of your process
tmp = os.popen("ps -Af").read()
if process_name not in tmp[:]:
print "The RescueTime is not running. Let's restart."
""""Use nohup to make sure it runs like a daemon"""
# message = process_name + " isn't running. Restarting!"
notify("RescueTime is Down", "Restarting RescueTime now.")
subprocess.call(
["/usr/bin/open", "-a", "/Applications/RescueTime.app"]
)
else:
print "The RescueTime is running."
# notify("RescueTime is Running", "Nothing to report")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment