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?



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.
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.
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
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.
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