Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Last day of the month cron job

Posted on 2004-08-16
11
Medium Priority
?
850 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 38

Accepted Solution

by:
yuzh earned 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

636 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