LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training 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 06-07-2010, 10:08 AM   #1
recomboDNA
LQ Newbie
 
Registered: Jun 2008
Distribution: Arch
Posts: 13

Rep: Reputation: 1
why does this cron job fail?


I would have sworn this used to work, but now I'm not so sure.

I have a script that I want to run on the last day of every month. So, I have this as a cron job:

Code:
0 23 28-31 * * [ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && /path/to/script
But it fails. I get mail that says:

Code:
command failed with exit status 1

/bin/bash: -c: line 0: unexpected EOF while looking for matching ``'
/bin/bash: -c: line 1: syntax error: unexpected end of file
I tried a slightly different syntax:
Code:
0 23 28-31 * * [ $(date +%d) -eq $(cal | awk '!/^$/{ print $NF }' | tail -1) ] && /path/to/script
and I get:
Code:
command failed with exit status 1

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

This is Debian Squeeze, in case it matters.
 
Old 06-07-2010, 10:19 AM   #2
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
Hi and welcome to LinuxQuestions! You encountered maybe one of the less known features of crontab, where the % sign has a special meaning. From the crontab manual page:
Code:
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 your cron job is correct, except for the % sign in the date command that needs to be escaped.
 
Old 06-07-2010, 10:22 AM   #3
recomboDNA
LQ Newbie
 
Registered: Jun 2008
Distribution: Arch
Posts: 13

Original Poster
Rep: Reputation: 1
Thank you.
 
Old 06-07-2010, 10:27 AM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Try this (KISS principle) --
Code:
0 23 28-31 * * [ $(/bin/date +\%d -d "1day") -lt $(/bin/date +\%d) ] && /path/to/script
(eg: Is tommorow a lower number than today? If so then its end of month, run.)

Last edited by rweaver; 06-07-2010 at 10:31 AM.
 
  


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
linux cron job duplicate job question cpthk Linux - Newbie 4 09-11-2009 08:52 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
SSH remote backup fail on cron job - keychain eyt Linux - General 4 07-17-2007 10:17 PM
All cron tasks fail Ateo Linux - Software 2 06-11-2005 01:34 PM
cron job the_rhino Linux - Newbie 10 10-02-2004 03:34 PM


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

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