5

How can this be done? I know it's pretty simple and includes appending something like & or &> to the actual command that starts the init script.

But, what is the best approach and how can it be ensured that the init script detaches itself, suppose the log file is /var/log/customDaemon.log ?

Here's the init script I have. I'm also not sure if the approach in the script is neat or just a nasty hack.

#!/bin/bash
#
# /etc/rc.d/init.d/customDaemon
#
# description: "The Daemon"
# processname: customDaemon
# pidfile: "/var/run/customDaemon.pid"

# Source function library.
. /etc/rc.d/init.d/functions

start() {
    echo "Starting customDaemon"
    /var/customDaemon &> /dev/null &
    return 1
}

stop() {
    echo "Stopping tweriod"
    prockill customDaemon   
    return 2
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    reload)
        restart
    ;;
    status)
        status customDaemon
    ;;
    *)
        echo "Usage: customDaemon [start|stop|restart|status]"
        exit 1
    ;;
esac
kapeels
  • 355
  • 1
  • 6
  • 18

2 Answers2

8

Try this:

/var/customDaemon >> /var/log/customDaemon.log 2>&1 &

I suggest you should running the service with normal user instead of root.

To show the [ OK ], [ FAILED ] messages, you can check the exit status, something like this:

/var/customDaemon >> /var/log/customDaemon.log 2>&1 &
RETVAL=$?
[ $RETVAL = 0 ] && echo -ne '\t\t\t\t\t[  \033[32mOK\033[0m  ]\n'

You may also take a look at pre-define funtions in /etc/rc.d/init.d/functions: daemon, killproc, action, ...

/var/customDaemon >> /var/log/customDaemon.log 2>&1 &
RETVAL=$?
[ $RETVAL = 0 ] && action $"Starting customDaemon... " /bin/true
quanta
  • 51,413
  • 19
  • 159
  • 217
1

Replace /dev/null with a filename for logging.

start() {
    echo "Starting customDaemon"
    /var/customDaemon > /var/log/customDaemon/console.log &
    return 0
}

I also changed the return code, because if it's able to start, it should return 0 - for success.

The quality of the init-script is OK. Not much nasty going on, and you have lsb functions - which is a very good thing.

What could be improved, is if the application supports redirection of logging itself, so you can wrap things properly with logrotation.

It would also be good if you check if the daemon successfully started, and throw an error (and exit 1) if it fails.

Kvisle
  • 4,193
  • 24
  • 25
  • I have monit installed and it can monitor the process thereafter I think :) – kapeels Oct 16 '11 at 13:49
  • That's true - but in the event an init script would fail to start the service, monit will also have trouble starting the service again. Also, it would depend on how monit actively checks if the service is running. – Kvisle Oct 16 '11 at 14:02
  • It isn't working somehow. When I do `service customDaemon start` I get a message `Starting customDaemon` and the screen remains undetached. – kapeels Oct 16 '11 at 14:05
  • There was a typo in the code, it is fixed now. – Kvisle Oct 16 '11 at 14:07
  • Which distribution are you using? If you're using Debian or Ubuntu, I can create an example using start-stop-daemon, which takes all of this into account. – Kvisle Oct 16 '11 at 14:13
  • I'm about to test the change. I'm on "Amazon Linux" btw. – kapeels Oct 16 '11 at 14:34