Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Using Cron to Run Script

by wileykt (Acolyte)
on Dec 28, 2001 at 19:42 UTC ( [id://134886]=perlquestion: print w/replies, xml ) Need Help??

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

I'm having a problem getting a script to work when it's run in cron. I've tried everything I could think of and still no results. The script is one I got from a post by tilly. The script cleans up old files in the specified dir. When it's scheduled in cron, nothing happens, the script must run because I get no errors, but no files are deleted.
$logdir = "/whatdir/"; opendir(LOGDIR, $logdir) or die "Can't open $logdir: $!"; foreach $file (grep {-f && (-M > 5)} readdir(LOGDIR)) { unlink $logdir . $file; } closedir(LOGDIR);
This works when run from the command line. I've looked at file permissions, and directory permissions, and paths, and everthing is correct. Help, Please! It's got me stumped.
Thanks

Replies are listed 'Best First'.
Re: Using Cron to Run Script
by scain (Curate) on Dec 28, 2001 at 21:19 UTC
    The most common problem I have seen with running cron jobs is one of execution environment. One of two things (or both) is problably wrong: you are assuming you know where the script is being executed, and are using a relative rather than absolute path, or your environment variables are not properly set up, like $PATH. This would include not giving the full path to the script in the crontab entry.

    Scott

Re: Using Cron to Run Script
by vek (Prior) on Dec 28, 2001 at 20:06 UTC
    Are you redirecting STDOUT & STDERR to a file of somesort? This will help you track down why the program isn't working:
    00,30 * * * * /path-to/the-perl-prog >> /path-to/your-output-file 2>&1
    Any helpful error messages should be captured in 'your-output-file'.
Re: Using Cron to Run Script
by count0 (Friar) on Dec 28, 2001 at 19:56 UTC
    My first guess is that something is wrong with your syntax in the crontab entry, since it works ok at your command line. Double check it with crontab(5) (type 'man 5 crontab').

    If it seems ok, add something in your script to debug. Printing something to stderr from a cronjob should email the output to you. Try something such as: print STDERR 'At the top of my perl cron job'; at the top, and a similar statement at the bottom.

    Another way you could debug it is by printing some output to a temporary file.
Re: Using Cron to Run Script
by sifukurt (Hermit) on Dec 28, 2001 at 19:59 UTC
    I ran into a similar problem once with a script that was making backups of files. It ran correctly from the command line, but didn't run correctly from cron. To be honest, I never really did figure out why it didn't work in the cron, but I ended up with a work-around that solved the problem. I don't have a Linux machine at hand, so I haven't tested this to make sure that it will work in your situation, but it worked for me with my backup script.
    $logdir = "/whatdir/"; opendir(LOGDIR, $logdir) or die "Can't open $logdir: $!\n"; chdir $logdir; foreach $file ( grep {-f && (-M > 5)} readdir(LOGDIR) ) { unlink $file; } closedir(LOGDIR);
    As I said, I haven't been able to test that to confirm that it will work in your situation, but the situation I had was very similar, and it worked for me.

    HTH
    ___________________
    Kurt
      Thanks,
      For some reason chdir works even though before I was telling it where to find the file to delete in the unlink.
      Keith
      The reason for the problem is that the filetests are going against the file names directly. So if you are listing files in directory /foo while you are in /bar, you are looking at whether /bar/baz is an old file when what you really wanted to know is whether /foo/baz is an old file.

      You have posted one workaround. The other is:

      my $logdir = "/whatdir/"; opendir(LOGDIR, $logdir) or die "Can't open '$logdir': $!\n"; foreach my $file ( grep {-f && (-M > 5)} map "$logdir$_", readdir(LOGDIR) ) { unlink $file; } closedir(LOGDIR);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-20 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found