LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-01-2015, 06:57 PM   #1
pearemb
LQ Newbie
 
Registered: Dec 2015
Posts: 8

Rep: Reputation: Disabled
Help with error running command over SSH


I'm trying to ssh to other machine and check if http service is running; if it's not running the code should restart the service. Here is my code -

result=$(ssh -n -t -t username@$remoteTarget "ps -ef | grep -v grep | grep httpd | wc -l")
if[$result > 0];then
logMessage "httd running"
else
ssh -n -t -t username@$remoteTarget "sudo service httpd start"
fi

I'm getting errors. Can someone please guide me?

this is the error : syntax error near unexpected token `then'
` then'

Last edited by pearemb; 12-01-2015 at 06:59 PM. Reason: added the error
 
Old 12-01-2015, 07:17 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
A few suggestions ... put your code inside code-tags; that greatly helps readability. Choose a sensible title; "Very Urgent!!!!" isn't one - firstly it may be important to you, but it's not to others; secondly it tells us nothing about the nature of the problem.


Regards,
Tink
 
Old 12-01-2015, 07:20 PM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
First, a few words about the style of your question.
Your subject is not meaningful. Use something that describes your problem, like "need help with 'if' in bash".
Nobody here gets paid overtime for answering questions here, so your urgency and your exclamation marks aren't very inviting.
See also the rules: http://www.linuxquestions.org/linux/rules.html

Finally, and perhaps most importantly, format your code by highlighting it in the editor and clicking on the '#' button. Or surround it by [code] tags - see also the list of possible tags http://www.linuxquestions.org/questi....php?do=bbcode.

With that out of the way, your code has a few issues.

If the ssh command pipeline fails, the result variable may be unset. You need to check for the exit value $? first.

You need white space around [ and ]. Also, the greater-than operator doesn't work inside [ ... ]; the shell will interpret it as output redirection. Use double brackets or, ideal for arithmetic, double parentheses:
Code:
if (( result > 0 )) ....
Note the absence of the dollar sign, another advantage of the double parentheses.

EDIT: See also the bash reference manual http://www.gnu.org/software/bash/man...al-Expressions.
 
Old 12-01-2015, 07:38 PM   #4
pearemb
LQ Newbie
 
Registered: Dec 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks very much for the quick reply.I'm so sorry for not formatting the code..

Is this correct ?
result=$(ssh -n -t -t username@$remoteTarget "ps -ef | grep -v grep | grep httpd | wc -l")
if (( result == 0 )) then
logMessage "httd not running..starting the service"
ssh -n -t -t username@$remoteTarget "sudo service httpd start"
fi

It is still not working...any help would be appreciated.

Last edited by pearemb; 12-01-2015 at 07:49 PM.
 
Old 12-01-2015, 08:31 PM   #5
pearemb
LQ Newbie
 
Registered: Dec 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
I'm getting this error for the following code : ")syntax error: invalid arithmetic operator (error token is "

Quote:
if (( result > 0 ));
then
logMessage "httd running"
ssh -n -t -t username@$remoteTarget "sudo service httpd reload"
else
logMessage "httd not running..starting the service"
ssh -n -t -t username@$remoteTarget "sudo service httpd start"
fi
 
Old 12-01-2015, 09:09 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by pearemb View Post
I'm getting this error for the following code : ")syntax error: invalid arithmetic operator (error token is "
On my system, this works:
Code:
$ result=33
$ if (( result > 2 ))
> then echo bla
> fi
bla
Which shell are you using to execute this code? Normally, on Linux, Bash is used, but perhaps you (inadvertently or not) use the Bourne Shell or something else?
By the way, you don't need the semicolon if then is on the next line.

And: Don't surround your code with bold and italics, but code tags: http://www.linuxquestions.org/questi...do=bbcode#code.

Last edited by berndbausch; 12-01-2015 at 09:12 PM.
 
Old 12-01-2015, 09:12 PM   #7
pearemb
LQ Newbie
 
Registered: Dec 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Sure. thanks very much for the reply. I'm using bash..
 
Old 12-01-2015, 09:18 PM   #8
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Show all your input and all the shell's output and error.
 
Old 12-01-2015, 09:40 PM   #9
pearemb
LQ Newbie
 
Registered: Dec 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Here is my code -
Quote:
result=$(ssh -n -t -t username@$remoteTarget "ps -ef | grep -v grep | grep httpd | wc -l")
echo $result
if [ $result \> 0 ]; then
logMessage "httd running"
ssh -n -t -t username@$remoteTarget "sudo service httpd reload"
else
logMessage "httd not running..starting the service"
ssh -n -t -t username@$remoteTarget "sudo service httpd start"
fi
I have tried different brackets, but, no luck..in this case I'm not getting any error but it is not executing the else part when http status is not working(result is 0). Thanks again..I really appreciate your quick response.

oUTPUT:
Quote:
0
httd running

Redirecting to /bin/systemctl reload httpd.service
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
Connection to IP ADRESS closed.
I'm using CENTOS, do we have to use syntax in specific to OS?

Last edited by pearemb; 12-01-2015 at 09:43 PM.
 
Old 12-01-2015, 10:45 PM   #10
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I am not sure about the meaning of greater-than in single brackets, but it obviuosly doesn't perform an arithmetic test. Use the double parentheses that I suggested. They are prettier, too.

Also, don't forget to eventually add testing the success of the ssh command to make your program more robust.

Last edited by berndbausch; 12-01-2015 at 10:47 PM.
 
Old 12-01-2015, 10:48 PM   #11
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by pearemb View Post
I'm using CENTOS, do we have to use syntax in specific to OS?
Bash is Bash I'd say. Certainly the Centos Bash is as standard as it can be.
 
Old 12-02-2015, 04:56 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Try it without using the -t option. And since it appears you are running CentOS 7 which uses systemd I would suggest using systemctl i.e.
systemctl start httpd
systemctl reload httpd

Code:
result=$(ssh -n username@$remoteTarget "ps -ef | grep -v grep | grep httpd | wc -l")
echo $result
if (( $result > 0 )); then
echo "httpd running"
ssh -n username@$remoteTarget "sudo service httpd reload"
else
echo "httpd not running..starting the service"
ssh -n username@$remoteTarget "sudo service httpd start"
fi
 
Old 12-02-2015, 08:49 AM   #13
pearemb
LQ Newbie
 
Registered: Dec 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
It worked when I have used the following code; Not sure though, why we have to use 1 in condition. Thanks very much for all the responses. They are really helpful to me.
Quote:
if [ $result \< 1 ]; then

logMessage "httd not running..starting the service"
ssh -n -t -t username@$remoteTarget "sudo service httpd start"
else
logMessage "httd running"
ssh -n -t -t username@$remoteTarget "sudo service httpd reload"
fi
Also, it is always throwing an error if I use
Quote:
if (( $result > 0 )); then
 
Old 12-02-2015, 09:21 AM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Is the error the same?

My exact code:
Code:
#!/bin/bash
result=$(ssh -n myserver "ps -ef | grep -v grep | grep httpd | wc -l")
echo "result=$result"
exit
if (( $result > 0 )); then
echo "httpd running"
#ssh -n -t -t username@$remoteTarget "sudo service httpd reload"
else
echo "httpd not running"
#ssh -n -t -t username@$remoteTarget "sudo service httpd start"
fi
./myscript
result=10
httpd running
./myscript
result=0
httpd not running
 
Old 12-02-2015, 04:46 PM   #15
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Code:
if (( $result > 0 )); then
Allow me to point out that the $ is not needed here. Saves typing and looks better!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error while running command in ssh quiet mode karthik4455 Linux - Newbie 7 09-25-2015 05:25 PM
Running adduser command via ssh in a loop massy Programming 4 03-09-2014 10:46 AM
running shell command locally and over ssh PoleStar Linux - Newbie 2 07-22-2012 12:22 PM
Keep running a command when disconnect from SSH stuartornum Linux - General 3 02-17-2006 10:19 PM
problem in running ssh command skvasistha Linux - General 1 11-29-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:51 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