Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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:
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'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.
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?
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.