Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to setup Crontab to run the job fortnightly

Posted on 2003-03-20
Medium Priority
Last Modified: 2007-12-19
To all the expert,

Anyone know how to write the crontab to run the job fortnightly? Not type of 1st and 3rd week of the month (typical example) because the accuracy is not there. What I want is the crontab able to run the job exactly two weeks once. Please advice.

Question by:chewlf
LVL 48

Accepted Solution

Tintin earned 200 total points
ID: 8179048
Depends a bit on what Unix flavour you have, but a portable solution is to run the command weekly on a particular day (say Saturday at 11pm), ie:

0 23 * * 6 /some/script

Then in the script have a check to see if it is due to run, eg:

script=`basename $0`

if [ -f /var/lock/$script ]
   rm -f /var/lock/$script
   exit # Come back to run next week
   touch /var/lock/$script
LVL 38

Assisted Solution

yuzh earned 200 total points
ID: 8179466
Let's say your want to run scriptA once a fornight. You can edit the crontab to run the job once a week to call up the following wrapper script:

WK=`date +%W`
ON_WK=`expr $WK % 2`

if [ $ON_WK = 1 ]; then


Have a look at the following script run a payroll for every fornight, just modify it to suit your needed.

if [ ! -z `find /path-to-file -mtime 14 -name lastrun.maker` ] ; then
   #need to run the program
   run_biweekly_payroll >/dev/null 2>&1
   if [ $? -eq 0 ] ; then
      touch /path-to/lastrun.maker
      echo "Failed to run payroll" | mail someone@abc.com

# If the lastrun.maker is not present, then run it once

   if [ ! -r /path-to/lastrun.maker ] ; then
       run_biweekly_payroll >/dev/null 2>&1
       touch /path-to/lastrun.maker




Author Comment

ID: 8179499
what about any simple/direct script in crontab to schedule the job? for example, 00 12 * * * /dir/script to schedule to run the script on everyday noon.

LVL 38

Expert Comment

ID: 8179525
Do you want to run the script everyday in ONE week ON and ONE week OFF mode.

My first script should work for you.

It this is not what you want, please let us know exactly what you want to do.

Expert Comment

ID: 8183044
To have a script schedule a cron job, it should:
  1st - copy the crontab file to a temp file.
  2nd - append the job to the temp file.
  3rd - run crontab with the tempfile as its argument.
        This will replace the existing crontab file.

To unschedule the cron job:
  1st - sed the crontab file to remove the job.
        Redirect the sed output to a temp file.
  2nd - run crontab with the tempfile as its argument.
        This will replace the existing crontab file.

If you need exact commands, let me know.

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.

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
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…
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.
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

572 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