1

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?

  • try to replace "#!/bin/sh" with "#!/bin/sh -x" and re-run script via service. Output may help you to resolve your issue. – Maxiko Jul 5 '16 at 7:56
  • Unfortunately, there is no output. Neither on service node2 start nor on service node2 stop. – KittMedia Jul 5 '16 at 7:58
  • Also upstart logs your service execution in a log file by the same name in /var/log/upstart/your-service-name.log. It should be helpful. – Maxiko Jul 5 '16 at 8:04
  • 1
    Use "$SCREEN -ls " in order to determine if there already is an active screen in this environment – Maxiko Jul 5 '16 at 8:07
  • $SCREEN -lsworked well, thank you! If you write this as answer I will mark it as correct. By the way: There is no /var/log/upstart in Debian, which I’m using. – KittMedia Jul 5 '16 at 8:11
2

According manpage use $SCREEN -ls in order to determine if there already is an active screen in this environment. If there is no logs of your script in /var/log/upstart then try to add debug info in your script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.