LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 02-07-2008, 01:45 PM   #1
waelaltaqi
Member
 
Registered: Sep 2005
Location: USA, TN
Distribution: CentOS & Ubuntu for Desktop
Posts: 454

Rep: Reputation: 31
Cron Job at the end of every Month?


This is a known limitation to cron. I found this somewhere and i'm not sure if works:
Code:
58 23 * * * [ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && myJob.sh
Would this work by the end of everymonth? if yes would somebody explain why?
 
Old 02-07-2008, 02:16 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by waelaltaqi View Post
This is a known limitation to cron. I found this somewhere and i'm not sure if works:
Code:
58 23 * * * [ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && myJob.sh
Would this work by the end of everymonth? if yes would somebody explain why?
yes, it would work.

here's how:
- this cron executes at 11:58pm every day.
- the "&&" means the part2 is dependent on part1 (part1 && part2), therefore if part1 is false or fails, part2 doesn't run.
- breaking down part1:
- - date +%d - gives the date, just the day part, i.e. today it returns '07' (try it - you can run it on a prompt)
- - cal - displays a calendar. "echo `cal`" displays the calender on one line
- - piping the output of echo `cal` through awk allows you to print only the last element of the calendar, which is the last day in the month. So, for this month, it prints out '29' (as this is a leap year).
- - -eq returns true if the left and right are equal, so returns true only if the output of "date +%d" (todays date) is the same as the last day of the calendar.

so, today, this script will break down like so:

[07 -eq 29] && myjob.sh

... 07 is not equal to 29, so the -eq test returns false.
... because the -eq test returns false, the right side of the && will not be executed, i.e. myjob.sh will not be run.

myjob will only be run, this month, on the 29th, then the script breaks down like so:

[29 -eq 29] && myjob.sh

Make sense?
 
1 members found this post helpful.
Old 02-07-2008, 02:31 PM   #3
waelaltaqi
Member
 
Registered: Sep 2005
Location: USA, TN
Distribution: CentOS & Ubuntu for Desktop
Posts: 454

Original Poster
Rep: Reputation: 31
makes absolute sense. I was mainly confused about the 'awk' part. I know nothing about awk the rest was not too bad. I did some reading about date and the %d option is listed right there in the man page. It looks like i need to start educating my self a little in awk.
Thank you very for breaking this down for me.

Last edited by waelaltaqi; 02-07-2008 at 02:32 PM.
 
1 members found this post helpful.
Old 04-04-2012, 07:14 AM   #4
danmurithi
LQ Newbie
 
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
Smile cron jobs

thanks for the useful info
 
Old 04-04-2012, 10:12 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,175

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
I would use a more direct approach that's a little easier to understand:
Code:
[ `date +%m` -ne `date -d tomorrow +%m` ]
Note that the % sign is special in a crontab and needs to be escaped, so the crontab line would be:
Code:
58 23 * * * [ `date +\%m` -ne `date -d tomorrow +\%m` ] && myJob.sh

Last edited by rknichols; 04-04-2012 at 10:22 AM. Reason: escape the % signs in crontab
 
Old 04-04-2012, 11:09 AM   #6
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 13,837

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by rknichols View Post
I would use a more direct approach that's a little easier to understand:
Code:
[ `date +%m` -ne `date -d tomorrow +%m` ]
Note that the % sign is special in a crontab and needs to be escaped, so the crontab line would be:
Code:
58 23 * * * [ `date +\%m` -ne `date -d tomorrow +\%m` ] && myJob.sh
I'd do something similar:
Code:
05 10 * * * [ `date -d tomorrow +%d` -eq '01' ] && /path/to/script
No matter what date is the end of the month, the next day is always "01". The time code will kick it off, but the script will only run if tomorrows date is "01".
 
Old 04-05-2012, 11:00 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,175

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Quote:
Originally Posted by TB0ne View Post
I'd do something similar:
Code:
05 10 * * * [ `date -d tomorrow +%d` -eq '01' ] && /path/to/script
No matter what date is the end of the month, the next day is always "01". The time code will kick it off, but the script will only run if tomorrows date is "01".
That's even better! But, you do need to escape that pesky % sign:
Code:
05 10 * * * [ `date -d tomorrow +\%d` -eq '01' ] && /path/to/script
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Scheduling a CRON job to run on the first Sunday of the month New2Linux2 Linux - Software 9 08-05-2013 11:42 AM
Running a job the first monday of the month? yanik Linux - General 12 03-05-2008 05:08 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
Cron Job to run on 2nd and 4th Friday of each month rust8y Linux - General 1 06-25-2007 09:07 PM
cron last day of every month lobo78 Linux - General 2 03-03-2004 08:41 PM


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