Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Cron Jobs


Cron Jobs

By ghostghost | 5178 Reads |
0     0

Making Cron Jobs

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 mysite.com/work.php~. 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: pwd On my machine, I get: /home/deathrape/htdocs

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: i

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: :x

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. (scroll down 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.

Shouts to wikipedia for the ASCII time table.

Comments
SySTeM's avatar
SySTeM 17 years ago

Nice article man, pretty interesting

ghost's avatar
ghost 17 years ago

good article, i learnt a few things from it so i guess it achived wat it was created to do, just the name 'deathrape' was a little worrying tho :S

AldarHawk's avatar
AldarHawk 17 years ago

Well Written article on one of the most useful commands in *NIX :P cronjob is a great tool to automate shit you do not wanna do :P

ghost's avatar
ghost 17 years ago

Nice article, doesn't seem ripped. Thanks for contributing.

ghost's avatar
ghost 17 years ago

np, happy to contribute

ghost's avatar
ghost 17 years ago

I didn't ever know about cronjob existance, great article