LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   end of month cron job (https://www.linuxquestions.org/questions/linux-newbie-8/end-of-month-cron-job-654629/)

tekmann33 07-09-2008 11:01 AM

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?

forrestt 07-09-2008 11:14 AM

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.

colucix 07-09-2008 11:21 AM

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.

tekmann33 07-09-2008 11:28 AM

Thanks to all!

tredegar 07-09-2008 11:42 AM

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.

varu0612 12-12-2009 06:40 AM

Quote:

Originally Posted by colucix (Post 3208956)
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!!


All times are GMT -5. The time now is 09:19 AM.