LinuxQuestions.org
Help answer threads with 0 replies.
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 09-13-2011, 08:14 AM   #1
ahmadirad
Member
 
Registered: Dec 2010
Posts: 33

Rep: Reputation: 0
Question crontab cannot execute a command!!!!


Dear all
for backup a folder every day i add this line into crontab,
00 18 * * * root tar -czf /home/test/backup_`date '+%Y%m%d'`.tar.gz /home/john/sharedoc

because i want to create uniqe file i add `date '+%Y%m%d'` to file name. but on time i receive an email that contains:

error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file

please help me.
 
Old 09-13-2011, 08:48 AM   #2
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,477

Rep: Reputation: 191Reputation: 191
Try:

tar -czf /home/test/backup_$(date +%Y%m%d).tar.gz /home/john/sharedoc
 
0 members found this post helpful.
Old 09-13-2011, 09:18 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You have to escape the % signs in the crontab entry, since they have a special meaning in crontab. 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.
Moreover there is no need to specify the root user if you use crontab -e to edit the root's crontab:
Code:
00 18 * * * tar -czf /home/test/backup_$(date +\%Y\%m\%d).tar.gz /home/john/sharedoc

Last edited by colucix; 09-13-2011 at 09:20 AM.
 
Old 09-13-2011, 09:33 AM   #4
ahmadirad
Member
 
Registered: Dec 2010
Posts: 33

Original Poster
Rep: Reputation: 0
your solution does not work.
 
Old 09-13-2011, 09:38 AM   #5
rahulchandrak
Member
 
Registered: Apr 2010
Posts: 33

Rep: Reputation: 2
You might consider writing your command in a script and execute the script using crontab.
 
Old 09-13-2011, 10:12 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by ahmadirad View Post
your solution does not work.
Please, can you translate your solution does not work? The previous error message
Code:
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
is addressed by (and only by) means of the escaped percent signs. What else does not work? Any other error message?
 
Old 09-13-2011, 11:11 AM   #7
rahulchandrak
Member
 
Registered: Apr 2010
Posts: 33

Rep: Reputation: 2
post what you wrote in the shell script.
 
Old 09-13-2011, 11:43 PM   #8
ahmadirad
Member
 
Registered: Dec 2010
Posts: 33

Original Poster
Rep: Reputation: 0
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
destination=/home/test/backup_$(date +%Y%m%d).tar.gz


# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root ron-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
53 7 * * * root tar -czf $destination /home/john/sharedoc
------------------------------------------------------------
also i used (\) before (%) but crontab cannot create an archive file.if i remove $(date +%Y%m%d) everythings is ok and crontab execute command and archive file created. crontab exactly has a problem with $(date +%Y%m%d. while, i want create archive files with unique filename. is it possible someone test this command in crontab?

Thanks a lot
 
Old 09-14-2011, 12:43 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
'%' tends to be treated as a special char in cron http://linux.die.net/man/5/crontab.
Just write a simply named script to call from cron and have the script do all the fiddling about; this is generally best practice.
 
Old 09-14-2011, 01:31 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by ahmadirad View Post
also i used (\) before (%) but crontab cannot create an archive file.if i remove $(date +%Y%m%d) everythings is ok and crontab execute command and archive file created. crontab exactly has a problem with $(date +%Y%m%d. while, i want create archive files with unique filename. is it possible someone test this command in crontab
I use the current date as argument to many crontab scripts and never had an issue with the date written as
Code:
$(date +\%Y\%m\%d)
what is the error message you got when it cannot create an archive file?. Please, you should provide more details in order to help us to help you.
 
Old 09-14-2011, 04:10 AM   #11
ahmadirad
Member
 
Registered: Dec 2010
Posts: 33

Original Poster
Rep: Reputation: 0
ok.
all configs in /etc/crontab is:
------------------------------------------------
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
destination=/tmp/new_backup/backup.$(date +\%Y\%m\%d).tar.gz

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root ron-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
33 12 * * * root tar -czf $destination /tmp/new
--------------------------------------------------

after time 12:33 i received an email that contain:
-----------------------------------------------------------------------------------
[root@CentOS new_backup]# mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/root": 2 messages 2 new
>N 1 root@localhost.local Wed Sep 14 08:46 27/1143 "Cron <root@CentOS> ta"
N 2 root@localhost.local Wed Sep 14 12:33 27/1149 "Cron <root@CentOS> ta"
& 2
Message 2:
From root@localhost.localdomain Wed Sep 14 12:33:01 2011
Date: Wed, 14 Sep 2011 12:33:01 +0430
From: root@localhost.localdomain (Cron Daemon)
To: root@localhost.localdomain
Subject: Cron <root@CentOS> tar -czf $destination /tmp/new
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <PATH=/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <MAILTO=root>
X-Cron-Env: <HOME=/>
X-Cron-Env: <destination=/tmp/new_backup/backup.$(date +\%Y\%m\%d).tar.gz>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>

tar: +\\%Y\\%m\\%d).tar.gz: Cannot stat: No such file or directory
tar: Removing leading `/' from member names
tar: Error exit delayed from previous errors

--------------------------------------------------------------------------------

and a file created in destination folder

-----------------------------------------------------------------------
[root@CentOS new_backup]# ll
-rw-r--r-- 1 root root 29948 Sep 14 12:33 backup.$(date
[root@CentOS new_backup]#
-----------------------------------------------------------------------
 
Old 09-14-2011, 05:01 AM   #12
ahmadirad
Member
 
Registered: Dec 2010
Posts: 33

Original Poster
Rep: Reputation: 0
i find problem.

i created script and put that in crontab. everything ok.
 
Old 09-14-2011, 02:55 PM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by ahmadirad View Post
tar: +\\%Y\\%m\\%d).tar.gz: Cannot stat: No such file or directory
tar: Removing leading `/' from member names
tar: Error exit delayed from previous errors


and a file created in destination folder

-rw-r--r-- 1 root root 29948 Sep 14 12:33 backup.$(date
This means that the command substitution is not performed. A variable assigned in the crontab is used by the cron daemon and passed literally to the cron jobs. There is no shell, hence no substitution. On the other hand you can put the command substitution on the command line itself, so that the shell started upon the job execution will take care of it. This should work as expected:
Code:
33 12 * * * root tar -czf "$(/tmp/new_backup/backup.$(date +\%Y\%m\%d).tar.gz)" /tmp/new
 
  


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
[SOLVED] How can I get crontab to execute process every 10 minutes? keevill Linux - Newbie 4 01-31-2011 09:56 PM
Crontab path when execute a job j-me Linux - Newbie 1 04-01-2010 10:33 PM
Crontab - command won't execute laki47 Linux - Newbie 5 02-10-2009 04:57 AM
crontab : why root can't execute ?? bluenectar Linux - General 2 03-12-2007 04:21 AM
crontab execute consecutively SofNine Fedora 17 07-28-2006 02:34 PM


All times are GMT -5. The time now is 01:10 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