http://www.perlmonks.org?node_id=634641

monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Hi experts,
I was trying to run this simple script (cron.pl) with CRON
#!/usr/local/bin/perl -w use strict; system("echo test > testcron.txt"); #system("nohup perl later_other_code.pl&");
but it fail to be executed with my cron entries that look like this:
MAILTO=monkfan@gmail.com * * * * * /usr/local/bin/perl /home/monkfan/MyPerl/cron.pl
Meaning that 'testcron.txt' wasn't created at all at this directory: /home/monkfan/MyPerl/. Why is that?

Update:

Regards,
Edward

Replies are listed 'Best First'.
Re: Perl Script Not Working With Crontab
by moritz (Cardinal) on Aug 23, 2007 at 15:02 UTC
    There are two things that usually causes cron failures:
    1. It runs from a different directory than expected
    2. Different environment variables, usually PATH is rather minimal in cronjobs.

    Check those two issues, and change your program to produce useful error messages on failure, so that cron will send you a mail with the error message.

    Update: and be sure always to edit your crontab with crontab -e, it will warn you when the syntax is wrong.

Re: Perl Script Not Working With Crontab
by clinton (Priest) on Aug 23, 2007 at 14:54 UTC
    The two things I can think of both have to do with your environment:
    1. You don't have permission to write to that folder (ie your script isn't running as the user you think it is)
    2. The current working directory is different from what you think it is, so testcron.txt is being created (or not if you don't have permissions), but not in the place where you expect
    Try running a simple cron command like: env && pwd > /home/monkfan/test.txt

    Clint

Re: Perl Script Not Working With Crontab
by misc (Friar) on Aug 23, 2007 at 14:37 UTC
    What do you mean with "it fails" exactly ?
    If you get an error message mailed, this would be helpful.

    A guess:
    You could try replacing testcron.txt with /absolutepath/testcron.txt
    (update) so system("echo test > testcron.txt");
    gets system("echo test > /absolutepath/testcron.txt")

    michael
Re: Perl Script Not Working With Crontab
by pinetree (Scribe) on Aug 24, 2007 at 22:05 UTC
    Edward, As a couple of comments have already pointed out, you do not necessarily have your expected environment set when your cron job runs. The solution is to fully path your commands and files. So, make sure you know where your commands are:
    MBPro:~ oren$ which echo /bin/echo MBPro:~ oren$ which nohup /usr/bin/nohup
    and update your script (change your locations as needed)
    #!/usr/local/bin/perl -w use strict; system("/bin/echo test > /home/monkfan/testcron.txt"); #system("/usr/bin/nohup perl /home/monkfan/later_other_code.pl&");
Re: Perl Script Not Working With Crontab
by cengineer (Pilgrim) on Aug 24, 2007 at 13:59 UTC
    Change your crontab to look something like:
    * * * * * /usr/local/bin/perl /home/monkfan/MyPerl/cron.pl >> /home/mo +nkfan/mycron.log 2>&1
    Then you can check mycron.log to see whats happening
Re: Perl Script Not Working With Crontab
by FunkyMonk (Chancellor) on Aug 23, 2007 at 14:31 UTC
    Shouldn't one of the *'s be a number so cron knows when to run it? For example:
    10 * * * * /usr/local/bin/perl /home/monkfan/MyPerl/cron.pl

    To run it at 10 past each hour

    I was wrong, sorry.

    Have you looked in your home directory? That's where my cron places the file with code equivalent to yours.

    update: crontab entry was wrong

      No. * * * * * is legal - it means run the program every minute of every hour of every day.

      Clint