0

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
davidl
  • 3
  • 2
  • does "userx" have sudo NOPASSWD right to launch su -c"$PATHB/script" ? (or whatever the sudoers grammar) – mveroone Nov 18 '13 at 09: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. – jirib Nov 18 '13 at 09: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 09: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. – jirib Nov 18 '13 at 09:35
  • OK. I'll take a look at daemon. – davidl Nov 18 '13 at 09:36
  • 2
    Still put 'set -x' in the script for debugging. – jirib Nov 18 '13 at 09:38
  • I'm not sure daemon is the way to go since the script itself is a python-script that daemonizes itself. I figure that what's in the script should be sufficient. @JiriXichtkniha: If I put `set -x` in the script, where can I review the output after reboot, in /var/log/messages? – davidl Nov 18 '13 at 09:42
  • `daemon` is a wrapper. – jirib Nov 18 '13 at 09:49
  • Have you tried starting the script manually with `/sbin/service script start`. what says `chkconfig --list script`. You need to find out weather your script has triggerd but failed or if it has not run. you don't need sudo probably, since root starts the services on the server . – Petter H Nov 18 '13 at 10:06
  • Removing sudo solved the problem.. not sure why? – davidl Nov 18 '13 at 10:12
  • Try replacing `/usr/bin/sudo /bin/su - $USER -c` with `/usr/bin/sudo -u $USER`. This will run the command as `$USER` directly. – plasmid87 Nov 18 '13 at 10:52

2 Answers2

2

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.

Jenny D
  • 27,780
  • 21
  • 75
  • 114
0

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

seanl
  • 470
  • 1
  • 5
  • 13