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 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 |
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 |
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 --edit-- oh, i've put heartbeat and heartbeat_monitor on /usr/local/bin/, if that makes any difference... I'd think not |
Did you add a shebang as catkin suggested?
|
Code:
goncalopp@kirjava:~$ sudo echo $SHELL 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 |
Quote:
Which distro? |
"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. |