create a dameon of a script that checks if httpd s

2019-10-01 17:45发布

Script to check if httpd is on or off

#!/bin/bash    
service=service_name  
email=user@domain.com     
host=`hostname -f`  
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))  
then  
echo "$service is running"  
else  
/etc/init.d/$service start  
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))  
then  
subject="$service at $host has been started"  
echo "$service at $host wasn't running and has been started" | mail -s   "$subject" $email  
else  
subject="$service at $host is not running"  
echo "$service at $host is stopped and cannot be started!!!" | mail -s   "$subject" $email  
fi  
fi  

How do I make this script run in background so that it keeps checking if httpd service is on or off and emails a message if it goes off !! ( PS-: I DON'T WANT TO MAKE THE SCRIPT AS A CRONJOB) just like a daemon process which runs in background!! Please Help

1条回答
男人必须洒脱
2楼-- · 2019-10-01 18:12

If you launch the script with nohup it will run as a daemon.

nohup script.sh &
查看更多
登录 后发表回答