LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 12-12-2011, 10:55 PM   #1
said76
Member
 
Registered: Aug 2011
Posts: 113

Rep: Reputation: Disabled
cron job not working


Hi,

I was trying to back up a directory on ubuntu server 10.04 on a daily basis. So I came up with the following command that I put into /etc/crontab

0 0 * * * root tar -pczf /home/backups/sharedfile/daily/backup_$(date +%Y-%m-%d).tar.gz /home/sharedfile

I could not find it the tar file the next day.

Is there something I missed here that caused it not to work?

Any help would be greatly appreciated.

Thank you
 
Old 12-12-2011, 11:30 PM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
Cron uses a limited PATH variable. So tar is most likely not init.
You can set the PATH variable in crontab.
Code:
PATH=/bin     #This may vary for your distro
0 0	* * *	root	tar -pczf /home/backups/sharedfile/daily/backup_$(date +%Y-%m-%d).tar.gz /home/sharedfile
or specify the full path to the command you want cron to run.
Code:
 0 0	* * *	root	/bin/tar -pczf /home/backups/sharedfile/daily/backup_$(date +%Y-%m-%d).tar.gz /home/sharedfile
Also, it is best (although not entirely required) to use
Code:
 crontab -e
to edit the crontab, this will edit the crontab for the user running the command.

Is the crond daemon running? (granted it should be by default, but still could be worth checking)
Code:
ps -ef | grep cron
 
Old 12-15-2011, 05:25 PM   #3
said76
Member
 
Registered: Aug 2011
Posts: 113

Original Poster
Rep: Reputation: Disabled
Hi fukawi1,

I tried as suggested but I could not find the "backups" folder under /home.

00 09 * * * root /bin/tar -pczf /home/backups/sharedfile/daily/backup_$(date +%Y-%m-%d).tar.gz /home/sharedfile
05 09 * * * root echo "Backup completed" >> /home/test.txt

I am very sure that the cronjob is running because I found test.text under /home directory. How do I find out what's causing this problem?

Thank you
 
Old 12-15-2011, 05:33 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Why you put ' root ' word in crontab lines ?

No need to edit /etc/crontab if you have write permission as user in /home/backups/sharedfile/daily

Just write a file in your home dir, say "mycrontab.txt"

with content:
Code:
00 09 * * *  /bin/tar -pczf /home/backups/sharedfile/daily/backup_$(date +%Y-%m-%d).tar.gz /home/sharedfile
05 09 * * *  echo "Backup completed" >> /home/backups/test.txt
then use:
Code:
crontab mycrontab.txt -u <username>
That will load the file in your crontab
Don't do it as root, else it will replace your system crontab :/
 
Old 12-15-2011, 05:57 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,527

Rep: Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108
cron tasks will usually mail the user with any output, including errors. Who are you running this task as? Why did you put "root" as the first command? Do you have any messages in "mail" with cron output?

Throw away the first 5 arguments (the time to run the task), and run the remainder on the command line. If it won't run there, it won't run in the cron:
Code:
root	/bin/tar -pczf /home/backups/sharedfile/daily/backup_$(date +%Y-%m-%d).tar.gz /home/sharedfile
Also, when debugging cron tasks, you should change the frequency with which they run so you don't have to wait an eternity to see if it worked. Change your times to "0,15,30,45 * * * *" to make it run every 15 minutes for testing. Once it's working, change it back to daily.
 
Old 12-15-2011, 07:57 PM   #6
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
Quote:
Originally Posted by said76 View Post
Hi fukawi1,

I tried as suggested but I could not find the "backups" folder under /home.

00 09 * * * root /bin/tar -pczf /home/backups/sharedfile/daily/backup_$(date +%Y-%m-%d).tar.gz /home/sharedfile
05 09 * * * root echo "Backup completed" >> /home/test.txt

I am very sure that the cronjob is running because I found test.text under /home directory. How do I find out what's causing this problem?

Thank you
I think this isnt a cron issue...

If you cant find the directory "/home/backups/" then neither can tar. If you run the tar command from the commandline, it will most likely error out with "No such file or directory".

As suicidaleggroll said, check the cron mail logs for errors, and get then get the command you want working, THEN worry about setting up cron.
 
Old 12-16-2011, 12:16 AM   #7
said76
Member
 
Registered: Aug 2011
Posts: 113

Original Poster
Rep: Reputation: Disabled
Thank you all for your replies.

This time, I decided to not use cron at all.

so in the terminal, I typed in as follows

user@testserver:/bin$ tar -pczf /home/backups/sharedfile/daily/backup1.tar.gz /home/sharedfile

I received error messages as follows
tar: Removing leading `/' from member names
tar: /home/backups/sharedfile/daily/backup1.tar.gz: Cannot open: Permission denied
tar: Error is not recoverable: exiting now

I am not sure why it said permission denied when I set permission to 755 from backups directory all the way to daily directory. And is it not that the backup1.tar.gz should be the one generated by running.

Many thanks
 
Old 12-16-2011, 03:05 AM   #8
ksovi
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Rep: Reputation: Disabled
you have to be root to enter the /home/backup folder

Quote:
Originally Posted by said76 View Post
Thank you all for your replies.

This time, I decided to not use cron at all.

so in the terminal, I typed in as follows

user@testserver:/bin$ tar -pczf /home/backups/sharedfile/daily/backup1.tar.gz /home/sharedfile

I received error messages as follows
tar: Removing leading `/' from member names
tar: /home/backups/sharedfile/daily/backup1.tar.gz: Cannot open: Permission denied
tar: Error is not recoverable: exiting now

I am not sure why it said permission denied when I set permission to 755 from backups directory all the way to daily directory. And is it not that the backup1.tar.gz should be the one generated by running.

Many thanks
 
Old 12-16-2011, 03:21 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The problem is that the % sign in crontab has a special meaning. From man 5 crontab:
Quote:
Percent-signs (%) in the command, unless escaped with backslash
(\), will be changed into newline characters, and all data after
the first % will be sent to the command as standard input.
This means that the crontab command line results incomplete and all the stuff after the first % sign is interpreted as standard input to the command itself. Solution: as the man page suggests, escape the % signs and the trick is done:
Code:
0 0 * * * root tar -pczf /home/backups/sharedfile/daily/backup_$(date +\%Y-\%m-\%d).tar.gz /home/sharedfile
 
1 members found this post helpful.
Old 12-16-2011, 11:28 AM   #10
alpha01
Member
 
Registered: Jul 2008
Location: Orange County
Distribution: Ubuntu/Debian, CentOS, RHEL, FreeBSD, OS X
Posts: 75

Rep: Reputation: 19
I've always found setting the MAILTO variable to my email address and have cron email me the errors, very useful for troubleshooting cron issues like this.
 
Old 12-16-2011, 11:33 AM   #11
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
+1 for MAILTO
For my part, I prefer use scripts in crontab lines rather than put commands directly
This has the advantage that you can add more things, like report the starting and
ending time of the script, and you can always modify the script without update crontab
 
Old 12-19-2011, 06:46 PM   #12
said76
Member
 
Registered: Aug 2011
Posts: 113

Original Poster
Rep: Reputation: Disabled
Thank you all folks for your replies.

Finally, got it to work by running sudo tar -pczf /home/backups/sharedfile/daily/backup1.tar.gz /home/sharedfile

However, I received a warning message: tar: Removing leading `/' from member names

I read somewhere that said this is just a warning to tell you that it is writing a tar file without absolute paths.

Can I ignore that warning? because I tried the following command
tar -C / -czvf /home/backups/sharedfile/daily/backup1.tar.gz /home/sharedfile

I still got the warning message.

Thank you
 
  


Reply


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
Job in /etc/cron.d not working BrianK Linux - General 6 11-03-2011 09:03 AM
cron job not working? tekram Linux - Server 15 06-09-2011 11:18 PM
[SOLVED] Cron job not working 2handband Slackware 7 11-14-2010 09:29 AM
cron job not working bittus Linux - Software 7 04-27-2009 07:39 PM
Cron Job Not Working... ALInux Linux - Software 2 07-24-2007 04:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration