Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to Use a Cron Job

by Anonymous Monk
on Apr 13, 2003 at 23:08 UTC ( [id://250172]=perlquestion: print w/replies, xml ) Need Help??

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

I finally have a use for Cron jobs (and by luck my host supports them). Now, I made a script I want to run hourly. I went into CPanel and added a Cron job; I put the path my host said was to my account, followed by my script (/path/to/account/myscript.pl).

However, I looked at the files it's supposed to be modifying and it's . . . not.

So, and ideas on what's wrong (FTP is basically the extent of my UNIX experience).

Replies are listed 'Best First'.
Re: How to Use a Cron Job
by perlplexer (Hermit) on Apr 13, 2003 at 23:28 UTC
    Make sure your script always uses absolute paths when working with files. Don't assume it'll always start in the same directory you developed it in. As an example,
    open my $fh, "<file.dat" or die "Error: $!\n";
    The above open() will most likely fail because the statement was written with an assumption that "file.dat" is in the current working directory.
    The same applies to your own libraries. If you had a .pm file in the same directory as your script, "use MyMod;" will no longer work when the script is executed from CRON. To fix, include "use lib '/path/to/libs/';"

    --perlplexer
Re: How to Use a Cron Job
by tachyon (Chancellor) on Apr 14, 2003 at 01:36 UTC

    Also as a sanity check set a cron job for something really simple, say a script like:

    #!/bin/sh echo called >> /home/myhome/cron.txt

    All this script does is write the word called into a file you should be able to write and check. Just to make sure that your cron Web interface is really setting the job.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: How to Use a Cron Job
by Abigail-II (Bishop) on Apr 14, 2003 at 10:08 UTC
    Something very important to realise when running a cron job is, the job was not started by a user shell. Your personal .shellrc or .profile will not have run. The PATH setting will be minimal. Other environment variables you might rely will be unset. Your cron program will not run in an interactive environment. Unless you redirect something, there won't be anything on STDIN. Output to the STDOUT and STDERR channels will typically be mailed to you.

    But enough of this guessing. Without knowing what you do, how can we tell what is going wrong? Perhaps you made a cron syntax error.

    Abigail

Re: How to Use a Cron Job
by vek (Prior) on Apr 14, 2003 at 03:45 UTC
    Hmm, right off the top of my head. Are you sure the cron job ran? Does the program have execute permission? Were you sent any mail? Does the program work from the command line manually? Do you have permission to modify the files in question?

    Posting some code couldn't hurt either.

    -- vek --
Re: How to Use a Cron Job
by spike_hodge (Novice) on Apr 14, 2003 at 08:52 UTC
    Let's see your actual crontab entrie; I find it hany to put this commented text at the top of every crontab: #minute (0-59), #| hour (0-23), #| | day of the month (1-31), #| | | month of the year (1-12), #| | | | day of the week (0-6 with 0=Sunday). #| | | | | commands #00 19 * * 2,5 /home/spike/scripts/testing.sh > /dev/null 2>&1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2024-04-23 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found