Created
October 31, 2018 12:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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