Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

this is Ubuntu 10.10 Maverick.

I have the following shell script in init.d that I want to run as a "daemon" (background service with start/stop/restart really) at system startup. There is a symlink in rc3.d. I tried 4 and 5 too. (Ideally this would initialize before graphical login happens and before a user logs in.)

IMPORTANT: the script works 100% as expected and required when testing this with service MetaLeapDaemon start and service MetaLeapDaemon stop. (This shell script calls a Python program which makes sure the appropriate .pid files are both created at startup and deleted at exit.)

So generally it works fine but now my only issue is why it will not be run at any of the run-levels I tried. I know for sure it isn't run because the log file it normally creates does not get created.

As you can see (by the lack of any uid:gid args in the start-stop-daemon commands) this would currently run only under root, is this forbidden in a default setup?

Here's the script, pretty much your run-off-the-mill daemon script really:

#! /bin/sh

DAEMON=/opt/metaleap/_core/daemon/MetaLeapDaemon.py
NAME=MetaLeapDaemon
DESC="MetaLeapDaemon"

test -f $DAEMON || exit 0

set -e

case "$1" in
    start)
        start-stop-daemon --start --pidfile /var/run/$NAME.pid --exec $DAEMON
        ;;
    stop)
        start-stop-daemon --stop --pidfile /var/run/$NAME.pid
        ;;
    restart)
        start-stop-daemon --stop --pidfile /var/run/$NAME.pid
        sleep 1
        start-stop-daemon --start --pidfile /var/run/$NAME.pid --exec $DAEMON
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart}" >&2
        exit 1
        ;;
esac

exit 0
share|improve this question
    
OK... now that I asked this question I get Related Recommended Questions from AskUbuntu... will check these out now. –  pearjoint Apr 10 '11 at 8:57
    
Will go with Upstart. –  pearjoint Apr 10 '11 at 10:00
add comment

2 Answers 2

I'm answering this for completeness, even though you already went with an Upstart job (and that is the way to go, I'm not suggesting here you go back)....

I think the key thing you missed first time around was the runlevel. I'm going to guess you are used to RPM distro's where it's typically runlevel 3 for multiuser and runlevel 5 for GUI?

In Debian and Ubuntu, there's really only single user (1) and multiuser (with GUI if installed). And multi user is runlevel 2. So your symlinks failed because you never got to RL 3.

Using the update-rc.d command makes managing the symlinks easier.

In any case, I'm writing this up for anyone else you might actually need it. Though I would suggest they convert the script to an Upstart too!

share|improve this answer
add comment

Use the following command to start your script at startup.

Add it to the startup applications.

sudo -u USERNAME COMMAND

By the way, someone told me to use NOPASSWORD, else it won't work:

You'd better run sudo visudo to edit the /etc/sudoers file and add a NOPASSWD for the command instead of writing the password in the script. – Lekensteyn

I hope this helped you,

Daniel

share|improve this answer
1  
Thanks but no longer needed... Upstart is way better for me and worked at first attempt! –  pearjoint Apr 10 '11 at 15:45
1  
Also this is for redistribution for "semi-admin" end users... if I asked them to type their password in an init.d script to finalize setup... I could just as well tell them to script their own daemon ;) –  pearjoint Apr 10 '11 at 15:47
    
please post an answer by yourself, that other users can read what the answer is. thank you, Daniel :) –  omnidan Apr 10 '11 at 15:51
1  
echo PASSWORD | sudo -u USERNAME COMMAND won't work. sudo reads the password from the terminal, not stdin. –  geirha Apr 10 '11 at 22:15
1  
OK Daniel the answer is in Ubuntu it's not sufficient to put a shell script in init.d, it also needs to be registered via the insserv command. And I don't know if this would have worked because the "new" Upstart system is designed to take the place of the "old" init.d system. And indeed it's so much easier to handle and a valid .conf file in /etc/init/ (not .d) is sufficient. –  pearjoint Apr 11 '11 at 3:34
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.