Hallo,
some days ago I needed a startup script based on screen. Killing the process on stop (like most of the scripts do) was not a solution. There should be a CTRL+C "keypress" to tell the process "please shutdown".
With this script you could modify your start and stop events as you need. Currently it is created to manage start and stop of sinusbot (TS3 MusikBot) - feel free to use it as you need (sinusbot post).
(At your own risk - no guarantee and no support!)
This script was made out of two ideas of creating a start / stop script:
- Based on: https://github.com/fhd/init-script-template
- Screen idea from: http://minecraft.gamepedia.com…als/Server_startup_script
How it works:
(Your could change start and stop behaviour by changing "sudo ..." and "sudo -u ..." lines)
- Start
- Create an empty screen session
- Send to screen: switch to "dir" variable path
- Send to screen: "cmd" to start the program
- Stop
- Sends to screen CTRL+C ('\003') and CTL+D ('\004'\) to stop process and log out from screen session
- Wait 10 seconds for the process to shut down and close screen session - if it takes more than 10 seconds -> kill the process (had no better idea )
Code
- #!/bin/sh
- ### BEGIN INIT INFO
- # Provides:
- # 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
- name="sinusbot"
- #name=`basename $0`
- dir="/opt/ts3soundboard"
- cmd="./sinusbot"
- user="tsbots"
- history=1024
- pid_file="/var/run/$name.pid" # managed by root user starting the service
- stdout_log="$dir/$name.log" # logfile has to be in a path where user has write permissions
- stderr_log="$dir/$name.log" # logfile has to be in a path where user has write permissions
- get_pid() {
- if [ -z "$user" ]; then
- sudo screen -ls | awk '/\.'$name'\t/ {print strtonum($1)}'
- else
- sudo -u "$user" screen -ls | awk '/\.'$name'\t/ {print strtonum($1)}'
- fi
- }
- is_running() {
- if [ -z "$user" ]; then
- sudo screen -list | grep -q "$name"
- else
- sudo -u "$user" screen -list | grep -q "$name"
- fi
- }
- case "$1" in
- start)
- if is_running; then
- echo "Already started"
- else
- echo "Starting $name"
- cd "$dir"
- if [ -z "$user" ]; then
- # start empty screen session
- sudo screen -h $history -dmS $name &
- # execute commands to start process
- sudo screen -S $name -X stuff 'cd "'$dir'"'$(printf \\r)
- sudo screen -S $name -X stuff $cmd' >> "'$stdout_log'" 2>> "'$stderr_log'"'$(printf \\r)
- else
- # start empty screen session
- sudo -u "$user" screen -h $history -dmS $name &
- # execute commands to start process
- sudo -u "$user" screen -S $name -X stuff 'cd "'$dir'"'$(printf \\r)
- sudo -u "$user" screen -S $name -X stuff $cmd' >> "'$stdout_log'" 2>> "'$stderr_log'"'$(printf \\r)
- fi
- for i in `seq 10`
- do
- if is_running; then
- echo "is running"
- break
- fi
- echo -n "."
- sleep 1
- done
- if ! is_running; then
- echo "Unable to start, see $stdout_log and $stderr_log"
- exit 1
- fi
- fi
- ;;
- stop)
- if is_running; then
- echo -n "Stopping $name.."
- if [ -z "$user" ]; then
- sudo screen -S $name -X stuff '\003' > /dev/null 2>&1
- sudo screen -S $name -X stuff '\004' > /dev/null 2>&1
- else
- sudo -u "$user" screen -S $name -X stuff '\003' > /dev/null 2>&1
- sudo -u "$user" screen -S $name -X stuff '\004' > /dev/null 2>&1
- fi
- for i in `seq 10`
- do
- if ! is_running; then
- break
- fi
- if [ -z "$user" ]; then
- #sudo screen -S $name -X stuff '\003' > /dev/null 2>&1
- sudo screen -S $name -X stuff '\004' > /dev/null 2>&1
- else
- #sudo -u "$user" screen -S $name -X stuff '\003' > /dev/null 2>&1
- sudo -u "$user" screen -S $name -X stuff '\004' > /dev/null 2>&1
- fi
- echo -n "."
- sleep 1
- done
- echo
- if is_running; then
- kill `get_pid`
- fi
- if is_running; then
- echo "Not stopped; may still be shutting down or shutdown may have failed"
- exit 1
- else
- echo "Stopped"
- fi
- else
- echo "Not running"
- fi
- ;;
- restart)
- $0 stop
- if is_running; then
- echo "Unable to stop, will not attempt to start"
- exit 1
- fi
- $0 start
- ;;
- 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