liverpaul
liverpaul

Reputation: 179

Cron perl script won't run

I'm trying to run a perl script on my Ubuntu server using cron. I have 3 perl scripts:

The only difference I can think of between 'works.pl' and 'fails.pl', is that 'fails.pl' uses the require() statement that references 'functions.pl'. 'Fails.pl' does not reference 'functions.pl'.

Extra info

Any ideas? My user level is novice btw :-)

Thanks.

Upvotes: 2

Views: 1312

Answers (2)

Yes Barry
Yes Barry

Reputation: 9876

Moritz answer is great and true, but my @INC was already properly set; however, my situation is different. My perl program would run on the CLI and cron was running it but it would silently fail.

Turns out, I had:

my $width = `tput cols`; # breaks silently if run by CRON

In my script somewhere which only works on a terminal. I changed it to:

my $width = 160; # cron likes this

Upvotes: 0

user507077
user507077

Reputation:

require tries to find the script in one of the directories listed in the @INC array. This array usually contains the current directory, .. Therefore if your current directory is the one that functions.pl is located in then running fails.pl should work.

There are two ways to achieve this: First, change the directory before running the script, e.g. (cron entry example)

5 12 * * * cd /home/user/path/to/script ; ./fails.pl

Second, tell Perl where to find your script at compile time by putting this near the top of fails.pl:

BEGIN {
  unshift @INC, "/home/user/path/to/script";
};

Upvotes: 3

Related Questions