• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 855
  • Last Modified:

Last day of the month cron job

I would like to run a script on the last day of the month on a FreeBSD system. I have tried several solutions out there and none of them are easy or smooth. Many didn't work reliably as well. Does anyone have an easy way to do this. I could use a wrapper script if required. I've seen some examples where they run the wrapper on the 28-31 day of the month, check to see if it is the last day, and then run. I could accept this if the wrapper was simple and supported with no special packages or ports. The ideal would be if you could somehow just add a crontab entry to do it. Thanks.
0
mini-itx
Asked:
mini-itx
  • 3
  • 3
  • 3
  • +1
1 Solution
 
mini-itxAuthor Commented:
As I indicated above, it would be nice if it did not require any additional ports or packages. That solution requires ksh to be installed and some work to incorporate the wrapper they suggest. It would be better if you could just refer to the wrapper in the cron job and pass it the command you want to run as a parameter. Simplicity is what is required. Also flexibility to reuse the wrapper for multi cron jobs, not a custom one for each entry.
0
 
avizitCommented:
If you have the 'date' command installed

if [ ! "`/usr/local/bin/date -d tomorrow +%d`" = "01 ]; then

exit

else

<run_the_script>

fi

run the above script on 28,29,30,31 of every month


ps: is runing the script on 1st of every month unacceptable ?

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
yuzhCommented:
Here's a wrapper script for you to do the job:

#!/bin/ksh

DD=`date +%d`
MM=`date +%m`
YY=`date +%Y`

case $MM
   in
   01|03|05|07|08|10|12 )
      END=31 ;;
   04|06|09|11 )
      END=31
      ;;
   *)
      # Feb
      if `cal $MM $YY | grep 29 >/dev/null` ; then
          END=29
      else
          END=28
      fi
 esac

 if [ $DD -ne $END ] ; then
    # Not end of the moth
       exit
 else
    #Today is end of month
      /path-to/yourscript
 fi

 exit

# End of script    
0
 
avizitCommented:
yuzh if the date command is there
you can just check

date -d tomorrow +%d

if its "1" then its the end of the month today
0
 
yuzhCommented:
Not all OS have GNU date installed, some OS version might not support "data -d",
that's why I wrote the above script!
0
 
TintinCommented:
FreeBSD doesn't have GNU date, so yuzh's script is suitable.

BTW, does the cronjob *have* to be for the last day of the month?  Much easier to make a cronjob for the first day of the month if you can.
0
 
mini-itxAuthor Commented:
It must be for the last day of the month for sure due to what it is doing. We have some stuff working on the 1st day, but the programs we are working with are date sensitive and cannot be hacked to go back to the prior month. Can yuzh's script be adapted so you pass it the program to be called as a parameter? That way it's not hardcoded into the script but rather we can add everything to the cron job?
0
 
TintinCommented:
Just change it to:

#!/bin/ksh

DD=`date +%d`
MM=`date +%m`
YY=`date +%Y`

case $MM
   in
   01|03|05|07|08|10|12 )
      END=31 ;;
   04|06|09|11 )
      END=31
      ;;
   *)
      # Feb
      if `cal $MM $YY | grep 29 >/dev/null` ; then
          END=29
      else
          END=28
      fi
 esac

 if [ $DD -ne $END ] ; then
    # Not end of the moth
       exit
 else
    #Today is end of month
     $*
 fi

BTW, make sure you give the points to yuzh.
0
 
mini-itxAuthor Commented:
Thanks to everyone for your help!
0
 
yuzhCommented:
mini-itx,
    You are welcom!

 Tintin,
    Thank for your extra help while I'm sleeping, cheers!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now