?
Solved

How to setup Crontab to run the job fortnightly

Posted on 2003-03-20
7
Medium Priority
?
1,629 Views
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.

Thanks,
Chewlf
0
Comment
Question by:chewlf
[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
7 Comments
 
LVL 48

Accepted Solution

by:
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:

#!/bin/sh
script=`basename $0`

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

Assisted Solution

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

#!/bin/sh
WK=`date +%W`
ON_WK=`expr $WK % 2`

if [ $ON_WK = 1 ]; then
   /path-to/scriptA
fi
#-------------

Or

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

#!/bin/ksh
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
   else
      echo "Failed to run payroll" | mail someone@abc.com
   fi
fi

# 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
   fi

exit


Cheers!


0
 

Author Comment

by:chewlf
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.

Thanks,
Chewlf
0
 
LVL 38

Expert Comment

by:yuzh
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.
0
 
LVL 2

Expert Comment

by:ellesd
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.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 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…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

801 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