-5

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

kartik
  • 55
  • 4

1 Answers1

3

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

nohup script.sh &
jordi
  • 1,157
  • 1
  • 13
  • 37
  • No; if you run it with `nohup` it will ignore the `HUP` signal. The `&` runs it in the background. Neither of these, isolated or together, is sufficient to properly daemonize the script. – tripleee May 02 '16 at 04:25
  • Maybe I am missing something, but that is how we get this kind of checks running on background, as I understood It is being asked. – jordi May 02 '16 at 07:44
  • 1
    It's a start, but it's not a proper daemon just because it runs in the background. See e.g. http://linux.die.net/man/1/daemonize for things a daemon will commonly want to do. – tripleee May 02 '16 at 07:57