LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags ,

Reply
 
Thread Tools
Old 01-12-2009, 03:25 AM   #1
chetansharma_softeng
LQ Newbie
 
Registered: Jan 2009
Posts: 6
Thanked: 0
Cron Job


[Log in to get rid of this advertisement]
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
chetansharma_softeng is offline  
Tag This Post ,
Reply With Quote
Old 01-12-2009, 03:40 AM   #2
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,130
Thanked: 464
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.
colucix is offline     Reply With Quote
Old 01-12-2009, 04:28 AM   #3
chetansharma_softeng
LQ Newbie
 
Registered: Jan 2009
Posts: 6
Thanked: 0

Original Poster
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
chetansharma_softeng is offline     Reply With Quote
Old 01-12-2009, 05:02 AM   #4
chetansharma_softeng
LQ Newbie
 
Registered: Jan 2009
Posts: 6
Thanked: 0

Original Poster
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
chetansharma_softeng is offline     Reply With Quote
Old 01-12-2009, 05:17 AM   #5
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 146
Thanked: 7
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
rizwanrafique is offline     Reply With Quote
Old 01-12-2009, 05:24 AM   #6
rizwanrafique
Member
 
Registered: Jul 2006
Distribution: Debian, Ubuntu, openSUSE, CentOS
Posts: 146
Thanked: 7
Further to this. Hourly/daily/monthly/weekly scripts can be executed by placing them in /etc/cron.hourly or other respective directories.
rizwanrafique is offline     Reply With Quote
Old 01-12-2009, 05:44 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,419
Thanked: 325
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.
chrism01 is offline     Reply With Quote
Old 01-12-2009, 07:14 AM   #8
uks
Member
 
Registered: Jul 2007
Posts: 68
Thanked: 2
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.
uks is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 10:20 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
Cron job help tejama Linux - Newbie 16 11-09-2007 02:08 PM
cron job somsahi Linux - Software 2 10-24-2006 06:40 AM
cron job somsahi Linux - Software 9 10-05-2006 03:19 PM


All times are GMT -5. The time now is 04:48 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration