Search This Blog

Thursday, May 27, 2010

How do I set up a Cron Job?

Introduction

In these two examples, we will set up cron jobs to output text to files.


Procedure 1

In this procedure, we will set up a cron job for the user joe that runs at 20 minutes past the hour every Wednesday in January.


1) To edit the crontab for joe, we first need to be logged in as Joe. Once we are logged in as joe, we need to type the following:
# crontab -e

2) Now we need to type in the following cron job (Notice our use of full paths – this is -highly recommended with cron jobs):
20 * * 1 3 /bin/echo $(/bin/date) >> /home/joe/crondates

3) Save the file and exit the editor and that will set the cron job as active.


Procedure 2

In this procedure, we will set up a cron job that runs as root every hour and sends the date to /var/log/messages. This job will be set up in the /etc/cron.* structure as opposed to being setup through crontab.


1) As root, create the script /etc/cron.hourly/printdate:
#!/bin/bash
# A cron job to send the date to /var/log/messages
/bin/echo $(/bin/date) >> /var/log/messages

2) Set the script as executable:
chmod +x /etc/cron.hourly/printdate

3) At this point, the cron job is set.

No comments: