Skip to content

Instantly share code, notes, and snippets.

@peacefixation
Last active March 27, 2019 01:36
Show Gist options
  • Save peacefixation/0cb7a4e629ce92e737efca24eb945603 to your computer and use it in GitHub Desktop.
Save peacefixation/0cb7a4e629ce92e737efca24eb945603 to your computer and use it in GitHub Desktop.
Init script for a program that runs in a screen session
#!/bin/sh
### BEGIN INIT INFO
# Provides: some-program
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Some Program
### END INIT INFO
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
NAME="some-program"
DESC="Some Program"
LOG_FILE=/var/log/some-program-run.log
. /lib/lsb/init-functions
start_some_program()
{
/usr/bin/screen -S some-program -d -m /bin/bash -c "while true; do /opt/bin/some-program >> ${LOG_FILE} 2>&1; done"
}
stop_some_program()
{
# check if a screen session exists
SESSION_EXISTS=$(/usr/bin/screen -S some-program -Q 'select .' >/dev/null; echo $?)
if [ ${SESSION_EXISTS} -eq 0 ]; then
/usr/bin/screen -S some-program -X quit >> ${LOG_FILE} 2>&1
fi
}
case "$1" in
start)
echo "Starting ${DESC}"
start_some_program
;;
stop)
echo "Stopping ${DESC}"
stop_some_program
;;
restart|force-reload)
echo "Restarting ${DESC}"
stop_some_program
case "$?" in
0|1)
start_some_program
;;
*)
echo "Failed to stop ${DESC}" >> ${LOG_FILE} 2>&1
exit 1
;;
esac
;;
*)
echo "Usage: /etc/init.d/${NAME} {start|stop|force-reload|restart}" >&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment