Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

The distribution is RHEL 5.

The script has been added to chkconfig like so:

# chkconfig --add script
# chkconfig script on

But it refuses to start when taking up the machine, what could be the problem?

#!/bin/bash
# 
# chkconfig: 2345 20 80
# description: starts script

. /etc/rc.d/init.d/functions

PATHB=/xxxx/opt/virtualenvs/primary/bin
USER=userx

function start() {
    /usr/bin/sudo /bin/su - $USER -c "$PATHB/script start"
}

function stop() {
    /usr/bin/sudo /bin/su - $USER -c "$PATHB/script stop"
}

function status() {
    /usr/bin/sudo /bin/su - $USER -c "$PATHB/script status"
}

case "$1" in
    start)
            start
            ;;
    stop)
            stop
            ;;
    restart)
            stop
            start
            ;;
    status)
            status
            ;;
    *)
            echo "Usage: $0 {start|stop|restart|status}"
            exit 1
esac
share|improve this question
    
does "userx" have sudo NOPASSWD right to launch su -c"$PATHB/script" ? (or whatever the sudoers grammar) –  Kwaio Nov 18 '13 at 9:29
    
you can always test it: sudo -K, sudo -n command (where '-K' removes cached credentials, and '-n' never asks for password). You can also put 'set -x' into the script to see verbose output. –  Jiri Xichtkniha Nov 18 '13 at 9:31
    
I haven't added anything like that to sudoers, so no. But executing /usr/bin/sudo /bin/su - $USER -c "$PATHB/script start" is successful.. –  davidl Nov 18 '13 at 9:31
    
Are you root? If so, it won't ask you for password. Anyway, sudo is your script is irrelevent, as the script itself is started by root. And... use 'daemon' instead of 'su'..., read /etc/init.d/functions. –  Jiri Xichtkniha Nov 18 '13 at 9:35
2  
Still put 'set -x' in the script for debugging. –  Jiri Xichtkniha Nov 18 '13 at 9:38

2 Answers 2

up vote 2 down vote accepted

Init scripts are being run by root, so adding sudo into the mix is unnecessary. The only reason to use sudo is when you are an unprivileged user who needs to run some things with a higher privilege - which is never the case when you are already root.

Since using sudo works from the command line but not from the init script, I'd place a small wager that your sudoers file contains requiretty. This makes sudo only available from a shell that has a tty, so that it can't e.g. be run by someone trying to break in through a malicious php script or something - but of course it also disables sudo from cron and init.

share|improve this answer
    
on rhel5 requiretty is there by default. –  Petter H Nov 18 '13 at 11:13
1  
It's a good security measure; one can always add an exception where appropriate. But in this particular case there's no reason at all to use sudo. –  Jenny D Nov 18 '13 at 11:15

Have you run made the script executable? ie by running chmod +x

share|improve this answer

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.