Reputation: 588
I'm trying to write a script that counts the number of processes running matching a patern. If it exceeds a hardcoded value then do something...else do something else.
I am finding out the count of processes using:
ps ax | grep process_name | wc -l | sed -e "s: ::g"
If the output of the above command is greater than 15..it should echo "Done". Otherwise, echo "Not Complete".
So far, I have this but it isn't working:
numprocesses=ps ax | grep sms_queue | wc -l | sed -e "s: ::g"
if [ $numprocesses -le "15" ] ; then
echo "Done."
else
echo "Not Complete."
fi
Upvotes: 7
Views: 24630
Reputation: 38718
How about
pgrep -c sms_queue
?
And the whole (portable) version of the script would look like this:
if [ "$(pgrep -c sms_queue)" -le 15 ]; then
echo "Done."
else
echo "Not Complete."
fi
Upvotes: 0
Reputation: 881373
numprocesses=$(ps ax | grep '[s]ms_queue' | wc -l)
if [[ $numprocesses -gt 15 ]] ; then
echo "Done."
else
echo "Not Complete."
fi
You had a few problems.
xyz
command, you should use $(xyz)
.grep
process as well (because it too has the pattern it's looking for), you should use the [firstchar]rest
grep pattern (or you can use | grep sms_queue | grep -v grep
to remove the grep
process from the count as well.Upvotes: 19
Reputation: 9376
If you want to copy the output of a command into a variable use this syntax:
variable=$(my command)
Upvotes: 1