Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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:
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
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
But, yeah, that'll remember me to put shebangs everywhere...
Thanks for the help of both
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
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.