Reputation: 696
I have written a shell script given below
unicorn_cnt=$(ps -ef | grep -v grep | grep -c unicorn)
if (( $unicorn_cnt == 0 )); then
echo "Unicorn Stopped" | mail -s "Alert - Unicorn" [email protected]
fi
delayed_job_cnt=$(ps -ef | grep -v grep | grep -c delayed_job)
if (( $delayed_job_cnt == 0 )); then
echo "Delayed Job Stopped" | mail -s "Alert - Unicorn" [email protected]
fi
rake_cnt=$(ps -ef | grep -v grep | grep -c rake)
if (( $rake_cnt == 0 )); then
echo "Convertion Stopped" | mail -s "Alert - Unicorn" [email protected]
fi
This is for checking, is the processes is running, if not send an alert mail. I am not much familiar with shell scripts. While running It shows following error.
process.sh: 3: process.sh: 2: not found
process.sh: 7: process.sh: 0: not found
process.sh: 11: process.sh: 0: not found
From some research I partially understands, this is because space problem while creating variable. Not sure. And I tried to use some solutions like sed and read. But still it showing the error. Can anyone help me.
Thanks Regards
Upvotes: 0
Views: 5067
Reputation: 696
From the above tip I found my answer.
unicorn_cnt=$(ps -ef | grep -v grep | grep -c unicorn)
if [ $unicorn_cnt -eq 0 ];
then
echo "Unicorn Stopped" | mail -s "Alert - Unicorn" [email protected]
fi
delayed_job_cnt=$(ps -ef | grep -v grep | grep -c delayed_job)
if [ $delayed_job_cnt -eq 0 ];
then
echo "Delayed Job Stopped" | mail -s "Alert - Delayed Job" [email protected]
fi
rake_cnt=$(ps -ef | grep -v grep | grep -c rake)
if [ $rake_cnt -eq 0 ];
then
echo "Convertion Stopped" | mail -s "Alert - Convertion" [email protected]
fi
It is now working fine and we can Integrate it with cronjob also.
Upvotes: 0
Reputation: 64623
Use brackets:
if [ "$unicorn_cnt" == 0 ]; then
Or better write it this way:
if ! ps -ef | grep -q [u]nicorn; then
echo "Unicorn Stopped" | mail -s "Alert - Unicorn" [email protected]
fi
That means 'check ps -ef for unicorn, and if it is not found do this'
Upvotes: 1
Reputation: 274878
For numeric comparisons you should use eq
not ==
.
Use [[
for conditional expressions.
Use a here string rather than echo
in your mail command.
Try this:
if [[ $unicorn_cnt -eq 0 ]]; then
mail -s "Alert - Unicorn" [email protected] <<< "Unicorn Stopped"
fi
Upvotes: 0