Solved

Last day of the month cron job

Posted on 2004-08-16
11
837 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11817222
0
 

Author Comment

by:mini-itx
ID: 11817248
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
ID: 11817285
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 38

Accepted Solution

by:
yuzh earned 250 total points
ID: 11818383
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
ID: 11818432
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
 
LVL 38

Expert Comment

by:yuzh
ID: 11818520
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
ID: 11825950
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
ID: 11826057
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
ID: 11826349
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
ID: 11826357
Thanks to everyone for your help!
0
 
LVL 38

Expert Comment

by:yuzh
ID: 11826872
mini-itx,
    You are welcom!

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

0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

691 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