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' |
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 |
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 )) .... EDIT: See also the bash reference manual http://www.gnu.org/software/bash/man...al-Expressions. |
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. |
I'm getting this error for the following code : ")syntax error: invalid arithmetic operator (error token is "
Quote:
|
Quote:
Code:
$ result=33 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. |
Sure. thanks very much for the reply. I'm using bash..
|
Show all your input and all the shell's output and error.
|
Here is my code -
Quote:
oUTPUT: Quote:
|
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. |
Quote:
|
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") |
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:
Quote:
|
Is the error the same?
My exact code: Code:
#!/bin/bash result=10 httpd running ./myscript result=0 httpd not running |
Code:
if (( $result > 0 )); then |
All times are GMT -5. The time now is 09:09 AM. |