last day (15 days later) » 

16:22
0
Q: bash comparison of integer always returns true?

r3wtI have the following simple bash script. for some reason it always returns true. #!/bin/bash service=hhvm status=($(ps -ef | grep -v grep | grep $service | wc -l)) if [[ $status -eq 0 ]]; then echo "starting $service" service hhvm start else echo "$service running" fi

just remove spaces around =. change foo = 3 --> foo=3 and run your script with bash /path/to/script
the real var is status=$(ps -ef | grep -v grep | grep $service | wc -l)
@ThomasW. unfortunately, i have been banned.
@ThomasW. no the first question i asked got a ton of down votes, so my account is more or less teetering on the edge. every down vote my account becomes banned. every couple hundred points it gets unbanned. its stupid.
remove outer parentheses from status=($(ps -ef | grep -v grep | grep $service | wc -l))
@KasiyA already tried it. out put of the call is 3 if service running, 0 if stopped, but always says that its running.
How do you run the script? with sh /path/to or bash /path/to? and what "out of call" is there? and what does mean grep -v grep there?
16:22
i run it with ./script.sh
@P.-H.Lin I know that, but according to what pattern? "grep" is patten there?
@r3wt change status=($(ps -ef | grep -v grep | grep $service | wc -l)) to status=$(ps -ef | grep $service | wc -l). P.-H.Lin your guess is true.
KasiyA, again that's not the problem. the call returns 3, which it is supposed to. even after updating as you suggested, the script still doesn't work as expected.i appreciate your attempts, but no need to try any further. it is my problem i'll try to figure it out eventually.
@r3wt Which call? I'm stuck on it. the status=($(ps -ef | grep -v grep | grep $service | wc -l)) line is completely invalid if you want to save the integer number in it.
@KasiyA the line you just asked me to change. the output returns 3 if the service is running, 0 if not.
Run alone service=hhvm; ps -ef | grep -v grep | grep $service | wc -l in terminal and now what do you see? 3 , 4 ,0 ? what? please reply.
16:22
@KasiyA 1 is what i see.
And when service=hhvm; ps -ef | grep $service | wc -l
2 27 248 - after that command
Ok. last question: please post the results of service=hhvm; ps -ef | grep -v grep | grep $service | wc -l when hhvm service is running and when it's not running
@KasiyA 1 when running, 0 when not. please bear in mind, there may be more than 1 running at any given time, so long as atleast 1 is running the check should pass.
This is silly question but what is the output of if [[ 3 == 0 ]]; then echo "true" ; fi and if [[ 3 -eq 0 ]]; then echo "true" ; fi
16:22
KasiyA, hi
There was no output for either one as should be. which is why i was so perplexed to begin with. the script should work or so i thought
try
#!/bin/bash
service=hhvm
status=$(ps -ef | grep -v "grep"| grep "$service" | wc -l)
if [[ $status -eq 0 ]]; then
echo "starting $service"
service hhvm start
else
echo "$service is running"
fi
it's crazy man
i'm on ubuntu 14.04 fwiw
So you always get false not true
do you know where the problem is?
16:32
no, i don't. i don't even know bash really. just found this when googling and hacked on it but never could get it working
1: when the service isn't running it does not returns ZERO
null?
damn
you were trying to let me figure that out on my own huh
run:
service hhvm stop;service=hhvm; ps -ef | grep -v grep | grep $service | wc -l
then:
service hhvm start;service=hhvm; ps -ef | grep -v grep | grep $service | wc -l
and post please please actual result
0
1
#!/bin/bash
service=hhvm
status=$(ps -ef | grep -v "grep"| grep "$service" | wc -l)
if [[ $status == 0 ]]; then
echo "starting $service"
service hhvm start
else
echo "$service is running"
fi
16:44
nope, still no good
and
#!/bin/bash
if ! (($(ps -ef | grep -v "grep"| grep "hhvm" | wc -l))) ; then
echo "starting hhvm"
service hhvm start
else
echo "hhvm is running"
fi
it works man
Cool
nice job
I'll post it as answer and please mark it as accept :)
16:48
quick question, i can run this as a cron job and dump output to dev null right?
aka the bitbucket
And before that. please test this
#!/bin/bash
service=hhvm
if ! (($(ps -ef | grep -v "grep"| grep "$service" | wc -l))) ; then
echo "starting $service"
service hhvm start
else
echo "$service is running"
fi
and reply any changes
yep, it worked man
good job sir, i truly thank you for your help!
ok I will post it as answer, please wait and check.
if you post answer i will upvote and accept
16:51
for your hard work. man you really helped me out alot
i am really greatful. ironically, i tried quoting $service everywhere but inside of the variable assignment
I have posted my answer
@r3wt are you here?
You asked me about cron?
At what times do you want to run it as cron?
1. Saving the script (example `script.sh`)
2. Make it executable `chmod +x /path/to/script.sh`
3. open the `crontab` file by `VISUAL=gedit crontab -e` or `EDITOR=gedit crontab -e`


4. Now copy and paste '* * * * * /path/to/script.sh' (without quotes) at end of it and save the file.
This will run your command/script every minute

If you want run it in different times, the cron syntax is as following

.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
Good luck.

last day (15 days later) »