Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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 (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:
  • 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.

Regards,
Edward

Comment on Perl Script Not Working With Crontab
Select or Download Code
Re: Perl Script Not Working With Crontab
by FunkyMonk (Canon) 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

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

    michael
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 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 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 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&");

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://634641]
Approved by andyford
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-09-23 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (218 votes), past polls