-   Linux - Newbie (
-   -   Cron Job (

chetansharma_softeng 01-12-2009 03:25 AM

Cron Job
Hi All,
actually i have never set up a cron job. i have created a script which will take up the backup.

please tell me step by step how can i set cron job.


colucix 01-12-2009 03:40 AM

First you have to check if the cron daemon is running and if you as a user are allowed to run cron jobs, but don't worry too much, since Linux enables that by default (just check if something doesn't work). To write your own crontab you will issue the command

crontab -e
it will open a vi session in which you can write your own cron jobs. Then save and quit and the system will store the proper file in the proper location automatically. Check man crontab for other options of the crontab command (for example to display or to remove the current crontab).

For the format of a crontab line, that is how to specify a cron job see

man 5 crontab
it describes the crontab format and gives some useful examples. If you're in trouble feel free to ask.

chetansharma_softeng 01-12-2009 04:28 AM

help in cron file
Hi All,

I have created a script to take backup. when i run this script using the browser it will work fine and create tar.gz files but when i try to run this using the command line in Linux it will display a error:

tar: Removing leading `/' from member names

now what to do to solve this problem.

my script code is below
###### script code start here #############

$sdate = date("d_m_Y_H_i_s");


$db_script_name = $back_path."testnewdb_".$sdate.".sql";

$code_sugar = $back_path."sugarCRM_".$sdate.".tar.gz";

shell_exec("mysqldump -h192.168.10.92 -usharma -psharma testnewdb > $db_script_name");
shell_exec("tar -czf $db_script_name.tar.gz $db_script_name");
shell_exec("rm -rf $db_script_name");

shell_exec("tar -czf $code_sugar $target_path/sugarCRM/");

###### script code end here #############


chetansharma_softeng 01-12-2009 05:02 AM

cron job help
Hi All,

I have written this line in >> crontab -e

0 * * * * /home/sites/htdocs/chetan/backuptest/create_zip.php

it this work?

actually i want to run cron job every 15min or hourly.

please help me out


rizwanrafique 01-12-2009 05:17 AM

1. You need to specify shebang line to get the script executed on its own. Do something like:


> which php
and use its output as the first line of your script. For example the above command gives you /usr/bin/php then your shebang will look like:


2. It is a good practise to redirect the output of your script to /dev/null:


0 * * * * /home/sites/htdocs/chetan/backuptest/create_zip.php >/dev/null
2. A script can be executed every 15 min using a line like:


0,15,30,45 * * * * /home/sites/htdocs/chetan/backuptest/create_zip.php >/dev/null
Hope this helps

rizwanrafique 01-12-2009 05:24 AM

Further to this. Hourly/daily/monthly/weekly scripts can be executed by placing them in /etc/cron.hourly or other respective directories.

chrism01 01-12-2009 05:44 AM


2. It is a good practise to redirect the output of your script to /dev/null:
Actually, I recommend redirecting to a log file in case of errors etc eg

0,15,30,45 * * * * /home/sites/htdocs/chetan/backuptest/create_zip.php >/home/sites/htdocs/chetan/backuptest/create_zip.log 2>&1
You may want to change the path for the logfile. Path must be writable for the cronjob owner.

uks 01-12-2009 07:14 AM

A few quick points to note:

When you are executing a script from cron, you should be careful to expand all paths in your script. You might be running the script from a location where you have all the files needed and the script might execute just fine from the command line. But when you put it in the cron file , it will fail. This is a common mistake we do.

To run a cronjob every 15 minutes another way would be :

*/15 * * * * /home/sites/htdocs/chetan/backuptest/create_zip.php >/home/sites/htdocs/chetan/backuptest/create_zip.log 2>&1

The first meathod would not be very easy to use if say for example you want to run it every 5 minutes. So use a */5 and you will get it executed every 5 miutes.

All times are GMT -5. The time now is 10:46 AM.