Solved

Last day of the month cron job

Posted on 2004-08-16
11
812 Views
Last Modified: 2010-04-21
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
Comment
Question by:mini-itx
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 11

Expert Comment

by:avizit
Comment Utility
0
 

Author Comment

by:mini-itx
Comment Utility
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
 
LVL 11

Expert Comment

by:avizit
Comment Utility
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
 
LVL 38

Accepted Solution

by:
yuzh earned 250 total points
Comment Utility
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
 
LVL 11

Expert Comment

by:avizit
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 38

Expert Comment

by:yuzh
Comment Utility
Not all OS have GNU date installed, some OS version might not support "data -d",
that's why I wrote the above script!
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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
 

Author Comment

by:mini-itx
Comment Utility
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
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
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
 

Author Comment

by:mini-itx
Comment Utility
Thanks to everyone for your help!
0
 
LVL 38

Expert Comment

by:yuzh
Comment Utility
mini-itx,
    You are welcom!

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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now