0

I've managed to create my own service script, however it's having some problems starting. From the service I'm calling the following script in /usr/bin/myscript.sh:

#!/bin/bash
tail -f /path/to/error.log|while read LINE;do
if grep -q "Fatal" <(echo $LINE); then
(
    echo "From: noreply@example.com"
    echo "To: inbox@example.com"
    echo "Subject: PHP Error (URGENT!)"
    echo $LINE
) | sendmail -t
fi
done

The script itself is fine, as when I run it manually it works. However when starting the service which calls the script, it doesn't actually start and run on the background. I think it has something to do with the ongoing while loop, but I'm just a starter in shell scripts.

I have the following in /etc/init.d/myscript

#! /bin/sh
### BEGIN INIT INFO
# Provides:          myscript
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: myscript shortdesc
# Description:       myscript longdesc
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="myscript desc"
NAME=myscript
DAEMON=/usr/bin/$NAME.sh
DAEMON_ARGS="--options args"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

[ -x "$DAEMON" ] || exit 0

[ -r /etc/default/$NAME ] && . /etc/default/$NAME

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start()
{
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
                || return 1
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
                $DAEMON_ARGS \
                || return 2
}

do_stop()
{
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2

        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
        [ "$?" = 2 ] && return 2

        rm -f $PIDFILE
        return "$RETVAL"
}

case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
  restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;;
                        *) log_end_msg 1 ;;
        esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
        exit 3
        ;;
esac

:

I also ran update-rc.d when I start the service (service myscript start) it gives the same output as hen I run the script itself, it's just blank (as the script doesn't output anything). My guess is that it does call the script, but doesn't run it in the background.

Both files also have been chmodded.

I have added exec > /tmp/log 2>&1 to the beginning of the script for debugging as requested in the comments, however the file comes up empty.

We are using Ubuntu 14.04 LTS.

1

So I fixed the problem, all I had to do was add a & after the done. That fixed it all. Thanks everyone for the help.

#!/bin/bash
tail -f /path/to/error.log|while read LINE;do
if grep -q "Fatal" <(echo $LINE); then
(
    echo "From: noreply@example.com"
    echo "To: inbox@example.com"
    echo "Subject: PHP Error (URGENT!)"
    echo $LINE
) | sendmail -t
fi
done &
  • " it doesn't actually start and run on the background" so when you said this, you meant that it actually did start, but it didn't go to the background? – muru Apr 6 '17 at 9:10
  • Yes, I might have wrongly phrased that. My apologies. – WesselV Apr 6 '17 at 11:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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