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.
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:
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.
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.If you are using Ubuntu, try using this:
http://manpages.ubuntu.com/manpages/hardy/man8/update-rc.d.8.html
just use:
sudo systemctl enable consumer.service
to enable
sudo systemctl disable consumer.service
to disable
if your script is LSB compliant will work just fine.-