Ubuntu Service with tail not starting

Status
Not open for further replies.

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
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:

Code:
#!/bin/bash
tail -f /path/to/error.log|while read LINE;do
if grep -q "Fatal" <(echo $LINE); then
(
    echo "From: [email protected]"
    echo "To: [email protected]"
    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

Code:
#! /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, however the file comes up empty.

I am using Ubuntu 14.04 LTS.

@Ecko
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,396
960
# strace -o /root/strace.myscript -fvt -s 8192 /etc/init.d/myscript
# less /root/strace.myscript

But yeah, this shouldn't be a service and instead a cron to grep the latest Fatal error(s) from your log file.
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
Actually fixed this issue last week, simply all I had to do was add a & behind the done in the first script, that fixed the issue. Thanks @Ecko for all the help given.
 
Status
Not open for further replies.

Users who are viewing this thread

Top