Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Perl Script Not Working With Crontab

by monkfan (Curate)
on Aug 23, 2007 at 14:25 UTC ( #634641=perlquestion: print w/replies, xml ) Need Help??
monkfan has asked for the wisdom of the Perl Monks concerning the following question:

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

  • Also, I have no root access in this Linux box.
  • Five (*) indicates cron to run the command at every minute of every hour of every day of every month.


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


Re: Perl Script Not Working With Crontab
by misc (Pilgrim) 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")

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/");
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/ >> /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/

    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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://634641]
Approved by andyford
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-06-23 05:16 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.