Lopofsky
Lopofsky

Reputation: 528

Linux Daemon that runs in the Background

I have the following code that runs in /etc/init.d/ under centos 6.6:

#!/bin/sh

DAEMON=/usr/local/bin/csvmarine_X.py
PARAMETERS=""
LOGFILE=/var/log/somefile.log

start() {
    echo -n "starting up $DAEMON"
    RUN=`cd / && $DAEMON $PARAMETERS > $LOGFILE 2>&1`

    if [ "$?" -eq 0 ]; then
        echo "Done."
    else
        echo "FAILED."
    fi
}

stop() {
    killall $DAEMON
}

status() {
    killall -0 $DAEMON

    if [ "$?" -eq 0 ]; then
        echo "Running."
    else
        echo "Not Running."
    fi
}

case "$1" in
    start)
    start
    ;;

    restart)
    stop
    sleep 2
    start
    ;;

    stop)
    stop
    ;;

    status)
    status
    ;;

    *)
    echo "usage : $0 start|restart|stop|status"
    ;;
esac

exit 0

It works fine and I want to make it run in the background.

My understanding after a search is that the bash parameter '$' (without the quotes) is responsible for the background running of of any process/script.

So I've experimented with the line:

RUN=`cd / && $DAEMON $PARAMETERS > $LOGFILE 2>&1

adding the '&' parameter at the end of the line or by pipeline it like this:

RUN=`cd / && $DAEMON $PARAMETERS > $LOGFILE 2>&1 &

and

RUN=`cd / && $DAEMON $PARAMETERS > $LOGFILE 2>&1 | &

Can anyone please give me an advise/guideline/tutorial to search further what I must do?

Of course if anyone can provide me with a direct answer it will be most welcome accepted :D

Thank you in advance!

Upvotes: 0

Views: 808

Answers (1)

osirisgothra
osirisgothra

Reputation: 2237

Use the 'template' that is provided, in debian it's in /etc/init.d/skeleton, and for centos it was something like /usr/share/doc/initscripts-*/sysvinitfiles/skeleton at least in 6. Use that, or you can easily download one just via google 'init.d/skeleton'.

Upvotes: 1

Related Questions