Howto: Setup cron Jobs to Restart Lighttpd & MySQL

Over the past couple weeks MySQL crashed when spiked with large amounts of traffic. To remedy this, a cron job has been implemented to simply restart mysql and lighttpd every other day. Here’s how it’s done.

First, if you haven’t already, define a cron job editor, like so:

# export EDITOR=vi ;

or…

# export EDITOR=nano ;

The pound symbol obviously represents a root prompt. Now that the editor is defined, there are a couple commands you should make yourself aware of.

crontab -e

Edit or create a crontab file if one doesn’t yet exist

crontab -l

Lists the contents of a crontab file

crontab -r

Take a wild guess! Removes a crontab file

Crontab files are laid out in the following format:

min hour day of month month day of week command

Here’s how to restart lighttpd each day at 8:30 PM

30 20 * * * /etc/init.d/lighttpd restart

To implement this cron job, simply type the following:

crontab -e

and insert the line above, then save your changes. Congratulations! You’ve successfully implemented a cron job. Here’s the line that restarts MySQL every other day at 11:00 PM.

0 23 * * 1,3,5 /etc/init.d/mysql restart

Crontab is a great way to automate a lot of tasks. You could easily configure cron jobs to perform system updates every week, update statistical software like awstats, purge setup files, purge temp files, and more.

What sort of tasks do you automate?

Additional Posts Worth Reading

 

5 Comments »

collapse Comment by Scylexan Subscribed to comments via email
2008-02-11 12:56:06

Ah yes, good ol’ cron jobs. There was a time frame where I used nothing but Linux. Around the time Windows ME was first released I decided to see what else was out there in the world of OS’ and that’s how I first found Linux. I made the switch to Red Had and later on to Fedora Core. I never once thought twice about going back to Windows. Not that I hated Windows but I was in an environment that best suited my needs. I made the switch back to Windows around mid 2007 because I felt it was necessary to learn the DirectX API if I was ever to get my dream career in the works.

Within that time frame, I had written many bash scripts and set them up as cron jobs so that I wouldn’t have to worry about them any longer. Some of the scripts I honestly forgot what they did but a few still remain in the back of my head. For instance, I had a script that was a more robust temporary file cleaner than the one that usually ships with Linux distros. Mine checked /home for particular files that didn’t need to be there. This was mainly within the range of temporary development files created with some of the IDE’s I used.

I had another script setup to run once a week to gather all of the root’s mail (usually just system information for services that ran) and move them all to my mail. I took a rough route with this by actually editing the root’s mail file and my mail file and setting the proper permissions afterwards. It was a good experience with reading/writing to files and setting permissions within a script.

My favorite script that would run once a week as well was my script to gather CVS source code of WINE and Gaim (now Pidgin) and compile and install the latest CVS build. I would receive and email telling me the results of the builds and if they succeeded or not.

I had many others but those are the ones that are coming to my mind as I write this response. Because of the horrors of Vista that I am having, I’ve been seriously considering going back to Linux as I never had any issues other than ones that I’ve done purposely on my own. I just wish I still had all of these scripts for when I make that switch again.

collapse Comment by Guy Patterson
2008-02-11 14:51:34

I’ve yet to get into bash scripting, but it’s something definitely worth toying with. Do you work for a software development company or something? I hope you’re putting your talents to good use.

 
 
collapse Comment by defender
2008-02-11 14:07:51

I use cron to automate my daily backups with rsync… saves me a right load of hassle and gives me peace of mind as well :)

 
collapse Comment by Scylexan Subscribed to comments via email
2008-02-11 15:14:16

Right now, I’m working for a software testing company. We test the software that follow DO-178B standards or basically the software that is placed on commercial airlines. I’m not much of a tester but I’ve learned a lot since I’ve started working here. I like to dab into everything programming related but my main focus is actually 3D programming. I’m currently working on getting my first game up and out on my site. It’s just taking some time as I’m not an artist (need models and textures) and the engine I’m using is lacking a few areas that I need.

 
collapse Comment by D.Ksyte Subscribed to comments via email
2008-06-14 15:05:29

Anyone involved with cron job scheduling might find this resource useful. Cron Sandbox at HxPI is an interactive webpage where you can play with crontab command strings. Enter your ‘m h D M Dw’ parameters and immediately see all the times cron would run your job in the coming days/weeks. Newcomers to cron job scheduling get a safe place to learn crontab commands and try examples from tutorials. System Administrators get a forward schedule to help manage system loading. www.hxpi.com/cron_sandbox.php

 
Name (required)


E-mail (required - never shown publicly)


URI - Web Site Address






Subscribe

Site of The Day

Sponsors

Categories

Recent Comments

  • Arthur: I can’t even be an Ebay affiliate. Tried various ways but still declined. Any help or suggestions?
  • chicago web design: I can’t imagine that these will last very long before Google banishes them all. But for now...
  • D.Ksyte: Anyone involved with cron job scheduling might find this resource useful. Cron Sandbox at HxPI is an...
  • FoNiX: “…decode md5″ is not possible, only bruteforce: generate hash and compare with original.
  • Z@$#: plz plz decript this hash for me any one….its really important…. 2CAD28C7C619F27DDE7B83C4999795BA
  • Joe: Thanks for writing this up. I tried the second method…however after the debian install the screen looks...
  • Scott: Was this issue ever resolved? I just started having a problem a couple weeks ago. I don’t do any kind of...
  • Prashant Patel: I had integrated Sp3 in Xp But finding one problem. In the Task Manager Process windows many services...
  • Ed: This looks like a real ‘leet’ linux command but it misses the point. Why would a hacker keep the name of...
  • Sudesh: My account was disabled but I followed their guideline and in one sec it was back on :) Here is the...

Top Commentators

Miscellaneous

  • Add to Technorati Favorites