LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 04-12-2011, 12:16 PM   #1
goncalopp
Member
 
Registered: Jun 2008
Posts: 47

Rep: Reputation: Disabled
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
 
Old 04-12-2011, 12:34 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
1 members found this post helpful.
Old 04-12-2011, 02:02 PM   #3
goncalopp
Member
 
Registered: Jun 2008
Posts: 47

Original Poster
Rep: Reputation: Disabled
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

Last edited by goncalopp; 04-12-2011 at 02:03 PM.
 
Old 04-12-2011, 02:07 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Did you add a shebang as catkin suggested?
 
1 members found this post helpful.
Old 04-12-2011, 02:21 PM   #5
goncalopp
Member
 
Registered: Jun 2008
Posts: 47

Original Poster
Rep: Reputation: Disabled
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
But, yeah, that'll remember me to put shebangs everywhere...
Thanks for the help of both
 
Old 04-13-2011, 10:23 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by goncalopp View Post
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?

Which distro?
 
Old 04-14-2011, 05:25 AM   #7
goncalopp
Member
 
Registered: Jun 2008
Posts: 47

Original Poster
Rep: Reputation: Disabled
"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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script syntax different for Ubuntu(Lucid)? debsys07 Ubuntu 2 02-17-2011 05:45 PM
[SOLVED] Question about sed syntax in bash script musonio Linux - Software 2 08-21-2009 07:17 AM
Bash script syntax error snowman81 Programming 5 11-16-2007 02:35 AM
How to check the bash script syntax? mesh2005 Linux - General 2 04-23-2006 08:22 AM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 06:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration