On 25-Jun-01 Miller, Jim wrote: > Hello guys, > Is there an easy way to run a script the last day of each month > using cron. IE it would handle the 30,31,29/28 issue?
You can't use cron syntax to catch the last day of the month. What you can do is to run, every day, a script which checks whether it's the last day today and then runs what you want. So your crontab entry could be like 0 0 * * * last_dom and now the fun starts. First, the command date '+%m %Y %d' generates the current date as numerical month, year, day. Just now, it gives 06 2001 25 Now take the first two of these, and do "cal" with these arguments: cal 06 2001 getting June 2001 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 The last number of the last non-blank line is the number of the last day of the current month (allowing for 28, 29, 30 and 31). If this matches the third element in the output of "date '+%m %Y%d' then today is the last day of the month. Otherwise it isn't. If it is, the run "whatever". You can conveniently use awk to do these little jobs ... You could also write the script so that "whatever" was an argument to "last_dom", so that you could have crontab entries like 0 0 * * * last_dom check_bank 0 0 * * * last_dom pay_electricity 0 0 * * * last_dom run_payroll ........... which should be intuitive enough for anyone. A script "last_dom" which somewhat crudely does the above is #! /bin/bash echo -n "if [ " > lastday.tmp echo -n "`cal \`date '+%m %Y'\` | \ awk '{if(NF) last=$(NF)}END{print last " -eq "}'`" >> lastday.tmp echo -n `date '+%d'` >> lastday.tmp echo " ] ; then $1 ; fi" >> lastday.tmp chmod 755 lastday.tmp lastday.tmp which, when run today as "last_dom ls", generates (and executes) the file "lastday.tmp" which contains if [ 30 -eq 25 ] ; then ls ; fi (and, by changing "-eq" to "-ne", you can get it to actually do something today). Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <[EMAIL PROTECTED]> Fax-to-email: +44 (0)870 167 1972 Date: 25-Jun-01 Time: 23:10:12 ------------------------------ XFMail ------------------------------