Last active
March 15, 2019 01:05
-
-
Save whisperity/4573819 to your computer and use it in GitHub Desktop.
Start arbitrary shell script as a background daemon from init.d
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/sh | |
### BEGIN INIT INFO | |
# Provides: scriptname | |
# Required-Start: | |
# Required-Stop: | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: S 0 1 6 | |
# Short-Description: Script short description | |
# Description: Starts/Stops the script | |
### END INIT INFO | |
USER=root | |
PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin | |
DESC="description" | |
NAME=script_name | |
DAEMON=/bin/$NAME | |
DAEMON_NAME=$NAME | |
LOGFILE=/var/log/script.log | |
# Or you can use | |
# LOGFILE=/dev/null | |
# to supress logging. | |
SCRIPTNAME=/etc/init.d/$NAME | |
case "$1" in | |
start) | |
echo -n "Starting $DESC: $NAME" | |
su -c "daemon --user=$USER --name=$DAEMON_NAME --output=$LOGFILE --command=$DAEMON" root | |
echo "." | |
;; | |
stop) | |
echo -n "Stopping $DESC: $NAME" | |
su -c "daemon --user=$USER --name=$DAEMON_NAME --stop" root | |
echo "." | |
;; | |
*) | |
echo "Usage: $SCRIPTNAME {start|stop}" >&2 | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment