Skip to content

Instantly share code, notes, and snippets.

@rchrd2
Last active December 29, 2015 11:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rchrd2/7665608 to your computer and use it in GitHub Desktop.
These are templates for creating an init.d script for a Procfile that needs to be run with virtualenv. The first uses `daemon`. The second uses `start-stop-daemon`.
#!/bin/bash
### BEGIN INIT INFO
# Provides: johnd
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
user="john"
VIRTUAL_ENV="/path/to/venv"
cmd="foreman start --procfile=/path/to/Procfile_production --root=/path/to"
name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
get_pid() {
cat "$pid_file"
}
is_running() {
daemon --running --name="$name" --user="$user" > /dev/null 2>&1
}
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
PATH="$VIRTUAL_ENV/bin:$PATH"
daemon \
--inherit \
--name="$name" \
--user="$user" \
--command="$cmd" \
--stdout="$stdout_log" \
--stderr="$stderr_log" \
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
echo "Stopping $name"
daemon --stop --name="$name" --user="$user"
;;
restart)
echo "Restarting $name"
daemon --restart --name="$name" --user="$user"
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
#!/bin/bash
### BEGIN INIT INFO
# Provides: johnd
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
user="john"
name=`basename $0`
PIDFILE="/var/run/$name.pid"
VIRTUAL_ENV="/path/to/venv"
DAEMON="/usr/bin/foreman"
DAEMON_OPTS="start --procfile=/path/to/Procfile_production --root=/path/to"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
set -e
. /lib/lsb/init-functions
is_running() {
status_of_proc -p $PIDFILE > /dev/null 2>&1
}
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
PATH="$VIRTUAL_ENV/bin:$PATH"
start-stop-daemon \
--start \
--background \
--user "$user" \
--chuid "$user" \
--make-pidfile \
--pidfile "$PIDFILE" \
--startas $DAEMON -- $DAEMON_OPTS
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
echo "Stopping $name"
start-stop-daemon --stop --signal INT --pidfile $PIDFILE
;;
restart)
echo "Restarting $name"
start-stop-daemon --stop --signal HUP --pidfile $PIDFILE
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment