init script that logs output of the script

20,347

Solution 1

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

Solution 2

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:
20,347
kapeels
Author by

kapeels

JS, Linux, Web Apps guy.

Updated on September 18, 2022

Comments

  • kapeels
    kapeels over 1 year

    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
    
    • w00t
      w00t over 12 years
      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.
    • kapeels
      kapeels over 12 years
      @w00t couldn't get it. Can you paste the entire line please?
    • w00t
      w00t over 12 years
      Sure, I meant like this: /var/customDaemon &> /dev/null 1<&- &
  • kapeels
    kapeels over 12 years
    I have monit installed and it can monitor the process thereafter I think :)
  • Ziron5
    Ziron5 over 12 years
    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.
  • kapeels
    kapeels over 12 years
    It isn't working somehow. When I do service customDaemon start I get a message Starting customDaemon and the screen remains undetached.
  • Ziron5
    Ziron5 over 12 years
    There was a typo in the code, it is fixed now.
  • Ziron5
    Ziron5 over 12 years
    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.
  • kapeels
    kapeels over 12 years
    I'm about to test the change. I'm on "Amazon Linux" btw.