3

Under unix system ..

I want to run a perl script A.pl in every Saturday. I tried the code below, it works. But I am wondering does there exists any other code can work but does not keep checking the time in a busy loop?

while(1)
{
        @Time = localtime();
        if( $Time[6] eq "6" )
        {
                `perl A.pl`;
        }
}
3
  • On which OS are you? Windows? Linux? Mac? (...)
    – Déjà vu
    Commented Jun 1, 2013 at 13:14
  • Do you really want to run this repeatedly, with no interval between runs? Commented Jun 1, 2013 at 13:20
  • @WilliamPursell No , so I come here for help ... Commented Jun 1, 2013 at 13:23

3 Answers 3

6

Under Unix, running programs periodically is the job of cron. Users can edit their jobs into the crontab using the crontab -e command (set your EDITOR environment variable to your favorite editor).

The format of the crontab is described in crontab(5) which you read with

 man 5 crontab

The most common gotcha for new cron users is that cron jobs are run with a minimal environment; not even PATH is exported so you have to set PATH in your program.

So where does your program's output go? Any output on stdout or stderr will be mailed to your account. If you want it somewhere else, your crontab line must redirect the output, e.g. with a crontab entry like this (note: sh syntax, not csh syntax!):

# Quarter past 8 on Saturdays.
15 20 * * 6  /home/user/bin/A.pl >/home/user/A.stdout 2>/home/user/A.stderr
4

Put it in crontab as follows:

0 0 * * 6 /path/to/perl A.pl

This is, of course, assuming you're on Unix. I have no clue about Windows.

0
1

If you are using a unix/linux system you can put your A.pl script in the crontab.

3
  • How to put something in a crontab? What are the exact steps?
    – Déjà vu
    Commented Jun 1, 2013 at 13:14
  • @hd1 I saw your answer! Don't discourage a new comer... I try to give him/her some clue how to write an acceptable answer...
    – Déjà vu
    Commented Jun 1, 2013 at 13:16
  • type: crontab -e then enter the following lines (as @Jens posted above): 0 0 * * 6 /path/to/perl A.pl i.e.: 0 0 * * 6 /usr/bin/perl A.pl
    – molnarg
    Commented Oct 6, 2014 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.