LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with error running command over SSH (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-error-running-command-over-ssh-4175560348/)

pearemb 12-01-2015 06:57 PM

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'

Tinkster 12-01-2015 07:17 PM

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

berndbausch 12-01-2015 07:20 PM

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.

pearemb 12-01-2015 07:38 PM

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.

pearemb 12-01-2015 08:31 PM

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

berndbausch 12-01-2015 09:09 PM

Quote:

Originally Posted by pearemb (Post 5458297)
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.

pearemb 12-01-2015 09:12 PM

Sure. thanks very much for the reply. I'm using bash..

berndbausch 12-01-2015 09:18 PM

Show all your input and all the shell's output and error.

pearemb 12-01-2015 09:40 PM

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?

berndbausch 12-01-2015 10:45 PM

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.

berndbausch 12-01-2015 10:48 PM

Quote:

Originally Posted by pearemb (Post 5458317)
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.

michaelk 12-02-2015 04:56 AM

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


pearemb 12-02-2015 08:49 AM

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

michaelk 12-02-2015 09:21 AM

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

berndbausch 12-02-2015 04:46 PM

Code:

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


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