Cron job on linux

Thursday, July 15, 2010

Cron job on linux

What is cron?
tuxCron is used in Linux operating systems to do some work (e.g. running a shell script) at specific or periodic intervals. Cron is also used to download files from the internet and also used widely to backup important system files.
E.g .In web CMS Drupal a cron job is used to update important modules.

In this tutorial, I will show you how to delete a folder or folders using cron.

A cron job requires a program or a shell script to run.

Some Jargon related to Cron:


Cron Job – The job you want cron to execute
Crontab –  A program that helps you to install a cron job.
Crond – The cron daemon in Linux

How do I use a Cron Job?
Here is the format of a cron job file:
[min] [hour] [day of month] [month] [day of week] [script/program to be run]

where each field is defined as
[min]  Range of value is 0-59.
[hour] Range of value is 0-23.
[day of month] Range of value is 1-31.
[month] Range of value is 1-12.
[day of week] Range of value is 0-6.
[script / program] The script or program you want to execute

Some important points:
In computer language terminology, the character * means zero or more times.  Remember never specify * in the minute field or else the job will be executed very minute.
Always give the full path of program or script you want to execute


How to use cron to delete specific folders?


Approach:

  1. Write a shell script that deletes folders, the folders to be deleted have a similar name like tmp files (temporary files).
  2. So a regular expression is required here that searches for folders with similar pattern.  However there is another way to search using ls command
  3. Tell Cron to run this script at particular time.


I merged the steps 1 and 2 in one shell script.
The shell script:



Save this code as delete_folder.sh in Desktop.

Let us see what does the script does.
The line 2 is used to specify the path from which you want to delete the folders. The line 4 is used  to change the directory. The line 5 takes only folders that have a specific name like tmp.


This approach will fail if you don’t want to delete a folder like mytmp and so regular expressions can be used. LIST only takes folders that need to be deleted.


Give delete_folder.sh executing the following command



4. Now we will set a cron job to execute the shell script at regular intervals say:
The script should run at 2:15pm on the first of every month.
5.We will first enter

 Followed by
15 14 1 * * /home/joseph/Desktop/delete_folder.sh

15  --    The first entry is minutes so 15 mins
14  --     The second entry is hour. 14 stands for 2 pm.
1    --        The third entry is day. So the script should run on the first of every month
             Note the symbol * is used for month and week entries.

Check to see if it was loaded by typing crontab -l.  It will display the date and time along with the program or script you want to execute.

References:

  1. http://www.scrounge.org/
  2. http://aplawrence.com/
  3. http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/



No comments:

Post a Comment

What do you think?.

© 2007-2018
The content is copyrighted to Sundeep Machado


Note: The author is not responsible for damages related to improper use of software, techniques, tips and copyright claims.