LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash script syntax fail as root (https://www.linuxquestions.org/questions/linux-general-1/bash-script-syntax-fail-as-root-874598/)

goncalopp 04-12-2011 12:16 PM

bash script syntax fail as root
 
Hello,
I'm scratching my head over a very simple netcat-based heartbeat monitoring script i wrote.

here we go:
Code:

echo `date --utc "+%Y-%m-%d %H:%M:%SZ"` script started
HOST=$1
SLEEP_TIME=1
LAST_STATE=12345
while [[ 0 == 0 ]]
        do
        heartbeat $HOST
        NEW_STATE=$?
        if [[ $LAST_STATE != $NEW_STATE ]]
                then
                LAST_STATE=$NEW_STATE
                if [[ $NEW_STATE == 0 ]]
                        then
                        echo `date --utc "+%Y-%m-%d %H:%M:%SZ"` Host $HOST up
                else
                        echo `date --utc "+%Y-%m-%d %H:%M:%SZ"` Host $HOST down
                fi
        fi
        sleep $SLEEP_TIME
done

"heartbeat" is just a simple netcat one liner that checks for the "connection established"/ACK.
Now, if i run this script as my user, i get the expected result. However, running it with sudo, out comes:
Code:

2011-04-12 17:13:36Z script started
/usr/local/bin/heartbeat_monitor: 20: [[: not found

I'm completely lost on this one :confused:

catkin 04-12-2011 12:34 PM

It could be that sudo runs the script using a different shell. You could try putting a "shebang" line at the top of the script to ensure it is always run using the nominated shell:
Code:

#! /bin/bash

goncalopp 04-12-2011 02:02 PM

the root shell is bash, but thanks, i sometimes forget the shebang

The script runs fine when in a root shell, but fails with sudo.
Also, I've added "heartbeat_monitor localhost &" to my rc.local, and "sudo /etc/rc.local" fails with the same error

Code:

goncalopp@kirjava:~$ cat /etc/rc.local
#!/bin/sh -e
/usr/local/bin/heartbeat_monitor cb.goncalopp.com >> /var/log/heartbeat.log &
nc -k -l 23576 &
exit 0
goncalopp@kirjava:~$ heartbeat_monitor localhost
2011-04-12 18:58:21Z script started
2011-04-12 18:58:22Z Host localhost down
^Cgoncalopp@kirjava:~$ sudo heartbeat_monitor localhost
2011-04-12 18:58:33Z script started
/usr/local/bin/heartbeat_monitor: 20: [[: not found
goncalopp@kirjava:~$ sudo su
kirjava:/home/goncalopp# heartbeat_monitor localhost
2011-04-12 18:58:44Z script started
2011-04-12 18:58:44Z Host localhost down
^Ckirjava:/home/goncalopp# sudo heartbeat_monitor localhost
2011-04-12 18:58:47Z script started
/usr/local/bin/heartbeat_monitor: 20: [[: not found

seriously... I'm feeling like a total noob

--edit--
oh, i've put heartbeat and heartbeat_monitor on /usr/local/bin/, if that makes any difference... I'd think not

MTK358 04-12-2011 02:07 PM

Did you add a shebang as catkin suggested?

goncalopp 04-12-2011 02:21 PM

Code:

goncalopp@kirjava:~$ sudo echo $SHELL
/bin/bash

I did that and immediately continued trying other things. My mistake, apparently.

Somehow, sudo manages to ignore both the shell defined in the passwd and the $SHELL environment variable, and was executing the script with dash... No idea why :confused:
But, yeah, that'll remember me to put shebangs everywhere...
Thanks for the help of both

catkin 04-13-2011 10:23 AM

Quote:

Originally Posted by goncalopp (Post 4322491)
Somehow, sudo manages to ignore both the shell defined in the passwd and the $SHELL environment variable, and was executing the script with dash... No idea why :confused:

Confirming: this happens both using sudo script_name and sudo su followed by entering the script name at the command line? In all cases with no options to either sudo or su?

Which distro?

goncalopp 04-14-2011 05:25 AM

"sudo su" works, "sudo" doesn't.

Tested on Ubuntu (10.10, 9.04) and Debian squeeze.
Reading the sudo manual, there's an "-i" argument that simulates a login; that seems to work, but then you need to specify absolute paths for the command. "-s" to specify a shell also works.
"sudo echo $SHELL" being /bin/bash still baffles me, though.


All times are GMT -5. The time now is 05:26 PM.