Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
1  
I would add a 1<&- in front of the final & so that stdin is closed for the daemon. Helps when starting it from the command line. –  w00t Oct 16 '11 at 14:14
    
@w00t couldn't get it. Can you paste the entire line please? –  KPL Oct 16 '11 at 14:52
1  
Sure, I meant like this: /var/customDaemon &> /dev/null 1<&- & –  w00t Oct 17 '11 at 8:33
    
@w00t Thanks :) –  KPL Oct 17 '11 at 8:54
add comment

2 Answers

up vote 4 down vote accepted

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
share|improve this answer
add comment

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.

share|improve this answer
    
I have monit installed and it can monitor the process thereafter I think :) –  KPL 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. –  KPL 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
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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