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
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.If you want to copy the output of a command into a variable use this syntax:
variable=$(my command)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With