Intro to Cron Jobs


—Article by DeathRape

Cron jobs are basically ways to tell a unix-based machine to perform certain tasks at certain times. Of course, there are several things this is useful for. For today’s example, which will be used to teach you the basics of what a cron job does, I will use the command:
rm /home/deathrape/htdocs/*~

This command tells the machine to delete all of my backup files. Generally, unix- based machines put a ~ after the file name to note a backup. So, for example, if my machine froze while I was editing a file named work.php, the system would save a file named work.php~. If I didn’t notice this, and work.php had database configuration information in it which would include my database name and password, then a hacker may be able to access the source of work.php by going to Obviously, I don’t want my passwords floating around- or source for my code, in some cases. Oh, and a note, * is a wildcard in unix, so I’m saying delete everything that ends in ~.

There are other things that cron jobs could be used for. In a webwars contest, I’m using cron jobs to restore file permissions once every minute incase someone gets in and defaces a page, and then removes my permissions to edit it.

So, let’s get started. generally, the basic cron job has 2 or 3 parts. The date/time, and command, and the log file [optional]. The date/time is formatted so that you can tell the machine how often to perform the task in a simple format:
minute hour day/month month day/week

for the day of week, 1 is monday, and therefore 0 is sunday. However, 7 is also considered sunday. Also, the cron job will execute when either day/month or day/week is true. got it? Good. Here is a common diagram of the fields that I used alot when I was new to cron jobs:

* * * * * command to be executed
– – – – –
| | | | |
| | | | —– day of week (0 – 6) (Sunday=0)
| | | ——- month (1 – 12)
| | ——— day of month (1 – 31)
| ———– hour (0 – 23)
————- min (0 – 59)

It basically tells you everything the above does, but in a cleaner format. Alright, so now you know how to structure time (hopefully). Good! Let’s move on. after the time comes the command. So, if we are deleting all of our log files in the htdocs directory, we first need to know where it is located. Fire up your shell and navigate to your htdocs directory. Then type:
On my machine, I get:

write down whatever you got and save it for later. Now, moving on to actually making the cron job. The command for editing cronjobs is:
crontab -e

other parameters for crontab are:
crontab -l = display cron file
crontab -r = remove cron file
crontab -v = display last time crontab file was edited (not universally available)

So, go ahead and type that first command, the one with the -e parameter. If you don’t know vi, you are pretty confused as to what just came up. I recommend learning vi- it’s awesome- but in the mean time, just go ahead and type:

This should let you enter inset mode. Now, type:
* * * * * rm [insert that directory you got earier from pwd here]/*~

then press the Esc key and then type:

Your cronjob has been installed! It will now delete all backup files in your htdocs folder every minute. Go ahead and play around with timing: try and make it only execute on the third of every month and the second day of every week and 10:35 AM and 10:40 AM. (read more for correct answer)

35,40 10 3 * 5 rm /home/deathrape/htdocs/*~

Alright, one other thing: you can make log files. go ahead and execute this command in your home folder:
mkdir cronlogs

now, go back into your cron file like you did above and add the allowing after the command: > [home directory]/remove_backups.log

your cronjob will now look like:
35,40 10 3 * 5 rm [insert that directory you got easier from pwd here]/*~ > [homedirectory]/remove_backups.log

There we go! You have now made your first cronjob. I’m sure that I didn’t include everything, so go ahead and comment if you have any corrections or additions.

6 Responses to “Intro to Cron Jobs”

  1. Just a small note…the user needs to be in the cron group to be able to add/edit cron jobs..this can be done by root using
    gpasswd -a cron

    some unix-es also have “at”…a simpler version that performs the same job as cron but only once..for example,
    user@machine ~ $ at 6:00
    xmms wakeup.mp3
    user@machine ~ $ _

  2. 2 gapp

    Thanx for the info.

  3. hey, just 2 small questions.
    1. Why is this filed under hacks?
    2. Why did you use my article without citing it? I’d be happy to let you use it — but at least ask next time.

  4. 4 deathrape

    If this article is not removed in a timely manner, I’m going to contact wordpress.

  5. 5 Dave Coleman

    Thanks to nathan for reffering me to this very good and reliable hacker who helped with a successful job I wont disclose. Reach him at for confidential purpose

  1. 1 » Intro to Cron Jobs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: