I have the following startup script, which should either start or stop a screen in detached mode:
#!/bin/sh
# node2
# Maintainer: @KittBlog
# Authors: mk@kittmedia.com
### BEGIN INIT INFO
# Provides: node2
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: node2 in screen detached mode
# Description: node2 in screen detached mode
# chkconfig: - 85 14
### END INIT INFO
NODE_NAME=node2
SCREEN=$(which screen)
SCRIPT_PATH=/home/jail/
do_start() {
if !($SCREEN -r | grep -o "[0-9]*\.$NODE_NAME"); then
$SCREEN -S $NODE_NAME -d -m $SCRIPT_PATH/start-npm.sh
fi
}
do_stop() {
for session in $($SCREEN -r | grep -o "[0-9]*\.$NODE_NAME"); do
$SCREEN -S "${session}" -X quit
done
}
case "$1" in
start|stop)
do_$1
;;
restart)
do_stop
do_start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
It works properly if I start the script manually or if I use it by calling /etc/init.d/node2 {start|stop|restart}
.
Also using service node2 start
works fine except that the if clause is getting ignored, so that there is always a new screen detached even if there already is one.
The problem is that service node2 stop
doesn’t work. I know that service
runs the script in a “predictable environment”. Is that a problem here? Can’t I use the $SCREEN -r | grep -o "[0-9]*\.$NODE_NAME"
in order to determine if there already is an active screen in this environment?