Created
June 11, 2015 10:35
-
-
Save danicarrion/118c7b3fa72524b36e3e to your computer and use it in GitHub Desktop.
Starts/stops a python script, storing its PID in /var/run, so that it can be run from monit
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
#!/bin/bash | |
# | |
# startpy: starts/stops a python script, storing its PID in /var/run, | |
# so that it can be run from monit | |
# | |
# Scripts must be placed in a folder under /home/sm/scripts. Startpy | |
# will look in that folder for a script named after the folder (plus | |
# .py extension). | |
# | |
# (In the following example we'll assume a script test.py exists in | |
# /home/sm/scripts/test | |
# | |
# 1) Start the script | |
# $ startpy start test | |
# 2) Start the script from a python environment located at | |
# /home/sm/scripts/test/env | |
# $ startpy -e start test | |
# | |
# (Upon successful start, the PID for the process will be stored in | |
# /var/run/sm/test.pid. Make sure the user executing startpy can | |
# write in that directory.) | |
# | |
# 3) Stop the script | |
# $ startpy stop test | |
while getopts ":e" opt; do | |
case $opt in | |
e) | |
USE_ENV=true | |
shift | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
case $1 in | |
start) | |
if [ -z "$2" ]; then | |
echo "No script name was supplied." | |
exit 1 | |
fi | |
SCRIPT_HOME=/home/sm/scripts/$2 | |
if [ -n "$USE_ENV" ]; then | |
source ${SCRIPT_HOME%%/}/env/bin/activate | |
fi | |
echo $$ > /var/run/sm/$2.pid; | |
exec 2>&1 python ${SCRIPT_HOME%%/}/$2.py | |
;; | |
stop) | |
kill `cat /var/run/sm/$2.pid` | |
;; | |
*) | |
echo "Usage: startpy [-e] {start|stop} script_name" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment