init.d script not starting on startup

5,724

Solution 1

Doing "consumer &" will simply background the task and go on. If the owning shell terminates, it will terminate any background tasks. You say the script works on command line, but your daemon won't survive if you log out?

You want to start your daemon with something like start-stop-daemon.

EDIT: actually, on reading your text again, I'm not sure if consumer is a daemon at all? If you just want to run some code on startup (e.g. housecleaning), you write a line in /etc/rc.local.

If the script takes a long time to run, you may want to disown it. I.e:

consumer &
disown %1

The script will now survive the shell termination. Note tho, that if the shell outputs text, it will retain the same tty, which may cause problems, depending on what happens to it after its owning shell terminates.

Solution 2

Try specifying the full path to consumer. I would suspect that it isn't in the default path at that point, hence it's failing to execute it.

Share:
5,724

Related videos on Youtube

jeff-h
Author by

jeff-h

Updated on September 18, 2022

Comments

  • jeff-h
    jeff-h almost 2 years

    I have what I consider to be a pretty simple script that I want to run on startup, however I'm pretty new to init.d scripts, and maybe there is a better way to do this in general.

    Basically I want my script to run when the system starts, so I have a ruby script, which I've moved to /usr/bin, and named just consumer

    For the sake of brevity say it looked like this, but actually did something:

    #!/usr/bin/env ruby
    
    # just example code
    puts "do stuff"
    

    Then I have my init.d script placed in /etc/init.d and named just consumer.

    #!/bin/bash
    ### BEGIN INIT INFO
    # Provides:          consumer
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Start daemon at boot time
    # Description:       Enable service provided by daemon.
    ### END INIT INF
    
    # /etc/init.d/consumer
    #
    
    # Some things that run always
    touch /var/lock/consumer
    
    # Carry out specific functions when asked to by the system
    case "$1" in
      start)
        echo "Starting Consumer"
        consumer &
        echo "Consumer started successfully."
        ;;
      stop)
        echo "Stopping Consumer"
        echo "Nothing happened..."
        ;;
      *)
        echo "Usage: /etc/init.d/consumer {start|stop}"
        exit 1
        ;;
    esac
    
    exit 0
    

    Now if I save this file and I just run sudo /etc/init.d/consumer start, it works perfect! It starts and gives me the desired output. So then I run:

    $ sudo update-rc.d consumer defaults
    Adding system startup for /etc/init.d/consumer ...
      /etc/rc0.d/K20consumer -> ../init.d/consumer
      /etc/rc1.d/K20consumer -> ../init.d/consumer
      /etc/rc6.d/K20consumer -> ../init.d/consumer
      /etc/rc2.d/S20consumer -> ../init.d/consumer
      /etc/rc3.d/S20consumer -> ../init.d/consumer
      /etc/rc4.d/S20consumer -> ../init.d/consumer
      /etc/rc5.d/S20consumer -> ../init.d/consumer
    

    But when rebooting the system my script never gets started, any ideas? I'm not sure what action to take next. I've adjusted all the scripts permissions to 775, and made sure root owned them all as well.

    Any help would be hugely helpful.

  • jeff-h
    jeff-h about 13 years
    Well I guess its not a daemon, it's really just a script that loops, but I want it to do so forever, and ideally I should devise some way to make this a proper program that I can monitor. I'm sort of new to this sort of programming as I come from the web development world, any thoughts?
  • Bittrance
    Bittrance about 13 years
    You're doing fine. If it is a sheel script, you may want to have a look at the trap command. It allows you to react to signals. Typically interesting signals is 1) HUP, which means the script should reread its config and rotate any files it has open, 2) TERM which means someone is trying to shutdown your script, and 3) USR1,USR2 which means your script should perform some action meaningful to its context, e.g. stop sleeping and doing its job.