LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-12-2009, 02:25 AM   #1
chetansharma_softeng
LQ Newbie
 
Registered: Jan 2009
Posts: 6

Rep: Reputation: 0
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.

Thanks,
Chetan
 
Old 01-12-2009, 02:40 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
Code:
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
Code:
man 5 crontab
it describes the crontab format and gives some useful examples. If you're in trouble feel free to ask.
 
Old 01-12-2009, 03:28 AM   #3
chetansharma_softeng
LQ Newbie
 
Registered: Jan 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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");

$back_path='/home/sites/htdocs/chetan/back/';
$target_path='/home/sites/htdocs/chetan';


$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 #############

Regards,
Chetan
 
Old 01-12-2009, 04:02 AM   #4
chetansharma_softeng
LQ Newbie
 
Registered: Jan 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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

Thanks,
Chetan
 
Old 01-12-2009, 04:17 AM   #5
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

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

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

Code:
#!/usr/bin/php
2. It is a good practise to redirect the output of your script to /dev/null:

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

Code:
0,15,30,45 * * * * /home/sites/htdocs/chetan/backuptest/create_zip.php >/dev/null
Hope this helps
 
Old 01-12-2009, 04:24 AM   #6
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 147

Rep: Reputation: 19
Further to this. Hourly/daily/monthly/weekly scripts can be executed by placing them in /etc/cron.hourly or other respective directories.
 
Old 01-12-2009, 04:44 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Quote:
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
Code:
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.
 
Old 01-12-2009, 06:14 AM   #8
uks
Member
 
Registered: Jul 2007
Posts: 68

Rep: Reputation: 16
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.
 
  


Reply

Tags
cron, job


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron Job???? ajeetraina Linux - Newbie 3 04-29-2008 09:20 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Cron job help tejama Linux - Newbie 16 11-09-2007 01:08 PM
cron job somsahi Linux - Software 2 10-24-2006 05:40 AM
cron job somsahi Linux - Software 9 10-05-2006 02:19 PM


All times are GMT -5. The time now is 01:46 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration