Last active
August 29, 2015 14:11
-
-
Save int64ago/01fe54e45ffc28442c20 to your computer and use it in GitHub Desktop.
sysvinit service example for python script
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 | |
# | |
# myservice Start up myservice | |
# | |
# chkconfig: 2345 55 25 | |
# description: This's the description | |
# | |
# processname: myservice | |
# Source function library | |
. /etc/init.d/functions | |
#the service name, a python script | |
SNAME=myservice | |
#the full path and name of the daemon program | |
#Warning: The name of executable file must be identical with service name | |
PROG=/usr/bin/$SNAME | |
# start function | |
start() { | |
#check the daemon status first | |
if [ -f /var/lock/subsys/$SNAME ] | |
then | |
echo "$SNAME is already started!" | |
exit 0; | |
else | |
echo $"Starting $SNAME ..." | |
python $PROG & | |
[ $? -eq 0 ] && touch /var/lock/subsys/$SNAME | |
echo $"$SNAME started." | |
exit 0; | |
fi | |
} | |
#stop function | |
stop() { | |
echo "Stopping $SNAME ..." | |
pid=`ps -ef | grep '[p]ython $PROG' | awk '{ print $2 }'` | |
[ "$pid"X != "X" ] && kill $pid | |
rm -rf /var/lock/subsys/$SNAME | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
reload|restart) | |
stop | |
start | |
;; | |
status) | |
pid=`ps -ef | grep '[p]ython $PROG' | awk '{ print $2 }'` | |
if [ "$pid"X = "X" ]; then | |
echo "$SNAME is stoped." | |
else | |
echo "$SNAME (pid $pid) is running..." | |
fi | |
;; | |
*) | |
echo $"\nUsage: $0 {start|stop|restart|status}" | |
exit 1 | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment