LinuxQuestions.org
Register a domain and help support LQ
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 07-09-2008, 11:01 AM   #1
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Rep: Reputation: 30
end of month cron job


I have a rudimentary understanding of crontab and I want to compose one that will run my bash script before midnight at the end of the month regardless of day, month, or year.

I googled around and found an example that claims to do this:

Code:
58 23 * * * [ `date +%d` -eq `echo \`cal\` | awk '{print $NF}'` ] && myJob.sh
I understand the time execution as well as the 'date' command and 'echo', but can someone explain this command so I have a clear understanding fo what it is doing?

Last edited by tekmann33; 07-09-2008 at 11:06 AM.
 
Old 07-09-2008, 11:14 AM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
The "[" and "]" symbols are the test operators in sh. It is saying if the output of "date +%d" (which will give you today's day of month) equals the output of "echo `cal` | awk '{print $NF}'" ($NF gives the last field in awk, therefore this will give you the last day for the current month) run the myJob.sh script. The && is the sh AND (|| is OR). The shell knows that it only needs to test the second part of the AND IF the first part is true. Therefore it will only be executed IF the current date is the last date of the month.

HTH

Forrest

Edit: The && and || symbols are AND and OR OUTSIDE of the test symbols.

Last edited by forrestt; 07-09-2008 at 11:20 AM. Reason: added Edit
 
Old 07-09-2008, 11:21 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The cal command produces a calendar like this
Code:
     July 2008      
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
The statement echo `cal` (backticks for command substitution) put the above output on one-line. Better to use the $(...) syntax for command substitution, which permits nesting without escaping the backticks (as in your command line). So
Code:
$ echo $(cal)
July 2008 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
This output is piped to the awk command which simply extract the last blank-space-separated field of the above line, that is the last day of the month:
Code:
$ echo $(cal) | awk '{print $NF}'
31
The output of the date command, which gives the day of the months, is compared with the output above. So the test is true if the current day is equal to the last day of the current month. The test is embedded in square brackets. If the result of the test is true, the command/script myJob.sh is executed. This is the meaning of the && sign.

In summary your crontab entry can be written as
Code:
58 23 * * * [ $(date +\%d) -eq $(echo $(cal) | awk '{print $NF}') ] && myJob.sh
using the $(...) notation for command substitution.

Last edited by colucix; 11-14-2009 at 10:30 AM. Reason: Corrected as per http://www.linuxquestions.org/questions/linux-newbie-8/cron-for-last-day-of-month-769026/
 
Old 07-09-2008, 11:28 AM   #4
tekmann33
Member
 
Registered: Nov 2006
Posts: 188

Original Poster
Rep: Reputation: 30
Thanks to all!
 
Old 07-09-2008, 11:42 AM   #5
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,012

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
Nice, clear explanations. Thanks forrestt and colucix

The only small change I'd like to offer would be that you should add the full path to myJob.sh eg /usr/bin/myJob.sh or cron may not find it, and it will not work.
 
Old 12-12-2009, 06:40 AM   #6
varu0612
LQ Newbie
 
Registered: Mar 2009
Posts: 4

Rep: Reputation: 0
Wink

Quote:
Originally Posted by colucix View Post
The cal command produces a calendar like this
Code:
     July 2008      
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
The statement echo `cal` (backticks for command substitution) put the above output on one-line. Better to use the $(...) syntax for command substitution, which permits nesting without escaping the backticks (as in your command line). So
Code:
$ echo $(cal)
July 2008 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
This output is piped to the awk command which simply extract the last blank-space-separated field of the above line, that is the last day of the month:
Code:
$ echo $(cal) | awk '{print $NF}'
31
The output of the date command, which gives the day of the months, is compared with the output above. So the test is true if the current day is equal to the last day of the current month. The test is embedded in square brackets. If the result of the test is true, the command/script myJob.sh is executed. This is the meaning of the && sign.

In summary your crontab entry can be written as
Code:
58 23 * * * [ $(date +\%d) -eq $(echo $(cal) | awk '{print $NF}') ] && myJob.sh
using the $(...) notation for command substitution.

Very nice and clear explanation !

Thanks a lot!!
 
  


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] Scheduling a CRON job to run on the first Sunday of the month New2Linux2 Linux - Software 9 08-05-2013 11:42 AM
Cron Job at the end of every Month? waelaltaqi Linux - Software 6 04-05-2012 11:00 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


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