Solved

cron job to start a service?

Posted on 2013-07-01
10
389 Views
Last Modified: 2013-07-03
Hello, I am looking for a cron job script that can run every 1 minute to check if  a service is running on my linux box. If the service is running the script does nothing but if the service is not running the script should activate another script to start the service? Can someone help me with this? Thank you.
0
Comment
Question by:aej1973
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 39292007
My first suggestion is to fix whatever is causing your service to stop running. Services are designed to be running all the time. Instead of merely restarting the service, it would be better to fix the underlying problem.

However, the following script answers your question:

if [ "`/bin/ps -ef | grep "your_script_name" | grep -v "grep"`" = "" ]; then
  echo NOT Running!
fi

Since cron jobs are 1-liners, change each statement line to end with a semicolon, and remove the hard returns to get this cron entry:

if [ "`/bin/ps -ef | grep "your_script_name" | grep -v "grep"`" = "" ]; then  echo NOT Running!; fi

Obviously, you will need to change your_script_name and replace the echo statement with the command to start services for your distro (the command is different for different distros.)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39292027
You should look into defining an /etc/inittab entry for the service.
0
 

Author Comment

by:aej1973
ID: 39292030
Tintin, how do I do that?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 39292054
Assuming /etc/inittab exists on your system (not installed by default on some Linux distros), add an entry like:


zz:3:respawn:/path/to/service

Where:

zz is a 2 character reference (you can make this anything unique)
3 is the run level the entry is valid for
respawn means restart the command if it dies



Running

init q

will force the init process to reexamine /etc/initab to pick up the new entry.

Note that using this process won't give you any control as to manually stopping/starting the daemon, which may, or may not be important to you.
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 100 total points
ID: 39292119
the solution given by tintin is good but if /path/to/service is a script that run another process and exit then it may not work.

what you could do is:

- run the monitoring script as an inittab entry as given by tintin, and let the script loop:

while true
do
   grep processname | grep -v grep
   if [ $? -ne 0 ]
   then
       put your command to restart the process
   fi
   sleep 60
one
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Accepted Solution

by:
skullnobrains earned 400 total points
ID: 39292166
ps -p `cat /var/run/myservice.pid` >/dev/null || /etc/rc.d/service_name start

you'll get an email everytime the service is started

if you're using an os where services provide status commands, you can use that instead of looking for a pid. on linux, you usually do not have status, but your rc scripts use startstopcmd that can be queried for the status

---

if you're not really monitoring a service an want a pseudo-daemon, try this

flock -w 60 /var/run/mylock.lock -c "YOUR COMMAND HERE"

this command will restart the process as soon as it dies at most once per minute. if you want it to work more than once per minute, you can use more than 60 for the wait time

---

or a combination

flock -w 60 /tmp/mylock.lock -c "while sleep 0.1 ; do ! ps -p 11565 >/dev/null && echo restarting myservice && RESTART COMMAND && break ; done"

this will restart the given process at most once per minute, send a warning email when it does. make sure whatever script you use does not provide output when it does not restart anything or cron will have to store that output
0
 

Author Comment

by:aej1973
ID: 39294097
skullnobrains, so if I run this cron job every 5 minutes this is how it is going to look?

*/5 * * * * ps -p `cat /var/run/myservice.pid` >/dev/null || /etc/rc.d/service_name start


Thank you,
A
0
 

Author Comment

by:aej1973
ID: 39294100
Also, how do I check to make sure my cron is running every 5  mins?
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 39296809
yes it would look like this.
check that the path is set properly or use the complet path to ps (likely /bin/ps)

if cron runs, it will run.

you can stop the sevice and wait. pretty much tests the whole thing.

cron should also send you a mail when there is output, so you'll receive a mail each time with the output (ie when it does restart the service). you can remove the redirection into /dev/null if you want a mail every time

you can also add some logging to it. try something like this

*/5 * * * * date >>/var/log/myservice.mon.log ; ps -p `cat /var/run/myservice.pid` >>/var/log/myservice.mon.log || /etc/rc.d/service_name start 2>&1 >>/var/log/myservice.mon.log

if your monitoring script becomes too complicated to be a one-liner, you had better stick it in a file and run the file from cron
0
 

Author Closing Comment

by:aej1973
ID: 39296854
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Monitor Aliased network interface bandwitch CentOS 4 91
EXCHANGE 2007, SCRIPTS 67 75
Perl Write to Specific line in a file 15 76
Issue with cURL on Windows 7 83
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now