Go Back   HostV Community - VPS Hosting Forums > HostV Support > HOW-TOs

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-22-2008, 04:12 AM
Moderator
 
Join Date: Feb 2008
Posts: 195
Default How To: Restart Failing Services Automatically using Cron

Hi,

Now first we need to create a script to check for services and restart if any of them is down. Then we need to add the script as a cron job to autmatically execute it at any specified time interval. But what's the code to put inside the script ?

Here's it,

Quote:
/usr/bin/pgrep cpanel || /etc/init.d/cpanel restart
/usr/bin/pgrep httpd || /etc/init.d/httpd restart
/usr/bin/pgrep named || /etc/init.d/named restart
/usr/bin/pgrep ftp || /scripts/restartsrv_ftpserver
/usr/bin/pgrep exim || /etc/init.d/exim restart
/usr/bin/pgrep mysql || /etc/init.d/mysql restart
/usr/bin/pgrep courier || /scripts/restartsrv_courier
/usr/bin/pgrep sshd || /etc/init.d/sshd restart
Thats it. Save it to any filename say /root/check_services.
Give execute permission to the script,

Quote:
chmod 700 /root/check_services
Then add it as a cron job. You can use the following command to add a line for the new cron job.

Quote:
crontab -e
OR

Just edit the file /var/spool/cron/root and add the line to it,
for eg: here is a sample line for executing the script every 15 minutes,

Quote:
*/15 * * * * /bin/bash /root/check_services
save the file and restart cron service.

Quote:
/etc/init.d/crond restart
Thats it, done.

Now to check whether a vps is down and restart it a script has to be setup on the main hardware node. For that you will need to contact us. Just leave a word at our help desk.

Cheers !

Last edited by HostV Harry; 08-22-2008 at 04:14 AM.. Reason: Optimization
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-23-2008, 10:13 AM
bdh734's Avatar
Senior Member
 
Join Date: Jun 2008
Location: A small room with flickering blue and green lights
Posts: 309
Send a message via MSN to bdh734 Send a message via Yahoo to bdh734
Default

Interesting......
Very good, I am going to try it.

But...
What are the two pipes for or what do they mean? ( || )
Thanks!
__________________
For HELP, summon support with the Bat-Signal: http://support.hostv.com/

Sysadmins are sexy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-24-2008, 07:02 PM
Junior Member
 
Join Date: Aug 2008
Posts: 25
Default

The pipes mean "or". In other words,
"find the process named <something> OR start the process named <something>".
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-24-2008, 11:57 PM
bdh734's Avatar
Senior Member
 
Join Date: Jun 2008
Location: A small room with flickering blue and green lights
Posts: 309
Send a message via MSN to bdh734 Send a message via Yahoo to bdh734
Default

Thanks....but there is two of them.
I know what one means...but what about two side-by-side?
__________________
For HELP, summon support with the Bat-Signal: http://support.hostv.com/

Sysadmins are sexy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-25-2008, 08:55 PM
bdh734's Avatar
Senior Member
 
Join Date: Jun 2008
Location: A small room with flickering blue and green lights
Posts: 309
Send a message via MSN to bdh734 Send a message via Yahoo to bdh734
Default

Hello,
Okay I tried this and now I am getting emails every 15 minutes with
Code:
18025
1909
7489
9363
16033
17922
21669
22063
22438
22442
28056
30329
31957
Usage: /etc/init.d/named {start|stop|status|restart|condrestart|reload|probe}
26566
7606
28655
28668
29700
9883
28428
28430
28447
28449
28456
28457
28464
28465
28574
9804
13570
How do I stop this, and do you see any errors from this output.
__________________
For HELP, summon support with the Bat-Signal: http://support.hostv.com/

Sysadmins are sexy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-25-2008, 09:51 PM
Member
 
Join Date: Dec 2007
Posts: 51
Default

Edit the cron so it sends to an email such as cron@yourdomain.com that way if you need them you get them but if you dont u still have them there just incace
__________________
Parody - Cirtex Hosting & HostV Community Guide.

Need Help? Contact helpdesk
CirtexHosting.com Come Join Us!
Need Shared, Reseller hosting? Or a Semi-Dedicated Server? CirtexHosting Can do that!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-25-2008, 10:23 PM
Moderator
 
Join Date: Feb 2008
Posts: 195
Default

Hi,

@bdh734,

|| or two pipes stand for 'OR' where as the singe pipe just pipes the output of the preceding command to the successive one.

The output of these lines would of course be the process id's of these services OR a service restart. Thats why you are getting these mails with process id's.

@Parody

Just been late to check out the forum, Thanks for replying and sharing info..
After all information sharing is the first step to wisdom. :-)

To be frank , checking for process ID is not that the best idea to determine if a service is up, since there can be situations where the will a lot of pid's, but the service would be in almost a dead state. I have seen this with mysql and apache sometimes.. But thats quite rare and this logic works in most cases.

Cheers !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-25-2008, 10:39 PM
bdh734's Avatar
Senior Member
 
Join Date: Jun 2008
Location: A small room with flickering blue and green lights
Posts: 309
Send a message via MSN to bdh734 Send a message via Yahoo to bdh734
Default

Thank you all for your reply.
@Harry, I have learned something today. It is this little bit of knowledge that makes learning awesome.

@Parody, I had to disable the cron job because of the emails. This is a great bit of advise. One thing I have noticed about apache and php, we are always having to "out-think" it. We "out-think" apache into parsing html as php and so on.

@inteluser, I just a little confused at what you were trying to explain. Sorry.

Again...thanks to you all.
__________________
For HELP, summon support with the Bat-Signal: http://support.hostv.com/

Sysadmins are sexy.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-16-2010, 09:30 PM
Moderator
 
Join Date: Feb 2008
Posts: 195
Default

Hello,

Even though a process ID does exist for a service, it might still be down. We have had such cases with apache, named and mysql mainly. So here's the altered script which would definitely make it more effective.

Code:
/usr/bin/pgrep cpanel || /etc/init.d/cpanel restart
/usr/bin/pgrep httpd || /etc/init.d/httpd restart
/usr/bin/nc -z localhost 80 || /etc/init.d/httpd restart
/usr/bin/pgrep named || /etc/init.d/named restart
/usr/bin/nc -z localhost 53 || /etc/init.d/named restart
/usr/bin/pgrep ftp || /scripts/restartsrv_ftpserver
/usr/bin/pgrep exim || /etc/init.d/exim restart
/usr/bin/pgrep mysql || /etc/init.d/mysql restart
/usr/bin/nc -z localhost 3306 || /etc/init.d/mysql restart
/usr/bin/pgrep courier || /scripts/restartsrv_courier
/usr/bin/pgrep sshd || /etc/init.d/sshd restart

The '-z' option of Netcat ( /usr/bin/nc) allows us to test connectivity to a port and returns success or failure as result.

Code:
 /usr/bin/nc  -z 80 || /etc/init.d/httpd restart
The above command tests connectivity to port 80 and restarts apache if it fails and hence makes better sense than looking out whether a process ID for apache exists !!

But for the above script to work netcat should be present on the server which is not the case by default. How ever you can install netcat on the server easily by using yum,

Code:
 yum -y install nc
Cheers !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:57 PM.


Powered by: vBulletin
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15