Link to home
Start Free TrialLog in
Avatar of Mike Paradis
Mike Paradis

asked on

bash script problem with multiple processes

We have a little program that runs an nmap against a few of our servers which is called testnmap.
We want to receive an alert if the service stops running.

Running the following gives the result of how many processes of this name are running.

ps -ef | grep -v grep | grep $service | wc -l

If the program is not running, it returns a 0. If it is running, it returns a 1.

However, if someone is running 'journalctl -u testnmap.service -f', this causes a second process to be found which means the results show 2. If the program has stopped running but a user is still running the above command, then we always have at least 1 as a result so cannot know if the program is still running or not.

Here are two results, one showing only the service running and the next showing the service running and someone using the journalctl command.

# ps -ef | grep -v grep | grep testnmap
root     24684     1  0 09:27 ?        00:00:00 /usr/local/testnmap/testnmap

# ps -ef | grep -v grep | grep testnmap
root     24684     1  0 09:27 ?        00:00:00 /usr/local/testnmap/testnmap
root     25448 24365  0 09:39 pts/1    00:00:00 journalctl -u testnmap.service -f

Can you rework this script to correctly work as needed.
Also, is there any way of preventing the script from sending notices more than once if the service is down since this is running as a cron job.

#!/bin/bash
service='testnmap'
if (( $(ps -ef | grep -v grep | grep $service | wc -l) < 1 ))
then
echo "$service is running"
else
echo "down"

fi
SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Paradis
Mike Paradis

ASKER

This almost works and as you say, you have no pc to check right now.

It shows the service as up if no one is running the journal command.
It shows the service as down if it is in fact down.
If someone is using the journal command, then it doesn't output anything.
>> It shows the service as up if no one is running the journal command. <<

With "ps -eo comm=" the journalctl command is completely ignored, we search "$service" in a commands-only list and don't take any arguments into account, neither those of journalctl nor those of whatever other command.

So does the script show the service as "up" when it's indeed up? Please be aware that you'll get a terminal message only at the first run after a status change. At subsequent runs you'll have to test the return code ("echo $?") where 0=down, 1=up.

>> If someone is using the journal command, then it doesn't output anything. <<

Yes, but this has nothing to do with the journalctl command. Except for the first run after a status change (where you'll get a terminal message) you'll have to test the return code (see above) to really see the status. I thought that this is what you desire (no repeated messages).
Sorry, not sure what to do at this point.
What I was asking for was to fix the code, then I added, if it was possible to prevent sending multiple emails which you gave a good suggestion to.

Right now, all I can give for feedback is what I posted above, the combinations and the output of the script run.
1) I suggested using "ps -eo comm=" instead of "ps -ef" in order to get a commands-only process list where command parameters do not get displayed..

# ps -ef | grep -v grep | grep testnmap
root     24684     1  0 09:27 ?        00:00:00 /usr/local/testnmap/testnmap
root     25448 24365  0 09:39 pts/1    00:00:00 journalctl -u testnmap.service -f

# ps -eo comm= | grep testnmap
testnmap

This will solve the main issue.

2) As for the second issue ("any way of preventing the script from sending notices more than once"):

I gave you a working script (meanwhile tested) to handle this.

You obviously had some difficulties understanding what the script actually does ("It shows ...), so I gave detailed explanations.

What more could I do for you?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes it shows, I'm not a programmer which is why I was asking for help with such a basic thing.
Jan solved the problem, my original script works perfectly now.

Thanks for the help, wish you would not have left such negative comments however woolmilkporc.

Update: After submitting this comment, I see that a new solution was provided. I'm not sure how to award this to be honest since everyone had a part bit I think Jan has the actual solution to the original question.

Thanks again.
I think the points look fine - Jan had the best fix to your actual problem, while wmp and I answered your second question between us!
I always feel compelled to do the right thing when it comes to the points. Thanks for the help and input.