Why does renaming a file not work when cron'd

G

gd

Hello all

I have created a small script to go to the internet and get some data I am
interested in and save it to a linux machine

Can anyone tell me why the script will rename and save the file properly
when I run it from the command line but when I cron it it does not

the line of code to rename the file is

rename("wind.png","$data.png");

thanks

gd
 
G

Gunnar Hjalmarsson

gd said:
Can anyone tell me why the script will rename and save the file properly
when I run it from the command line but when I cron it it does not

Yes, Perl can.

rename 'wind.png', $data.png or die $!;
the line of code to rename the file is

rename("wind.png","$data.png");

See "perldoc -q quoting"
 
A

Aaron Baugher

gd said:
Can anyone tell me why the script will rename and save the file
properly when I run it from the command line but when I cron it it
does not

When things work from the command line but not from cron, it usually
means the cron job is running with different permissions or a
different $PATH than you have when you're logged in. So first, are
you running it as the same user as the owner of the cron job? If so,
do you call any external programs or open any files without using the
full path?
the line of code to rename the file is

rename("wind.png","$data.png");

rename("wind.png","$data.png") or die $!;

Are you sure your script has already chdir'd to the right place before
doing this?
 
B

Bart Lateur

gd said:
Can anyone tell me why the script will rename and save the file properly
when I run it from the command line but when I cron it it does not

the line of code to rename the file is

rename("wind.png","$data.png");

Just a guess: the working directory of the script isn't the same when
run from cron.

Either that, or it's a different user, which lacks the proper
permissions.
 
T

Tintin

gd said:
Hello all

I have created a small script to go to the internet and get some data I am
interested in and save it to a linux machine

Can anyone tell me why the script will rename and save the file properly
when I run it from the command line but when I cron it it does not

the line of code to rename the file is

rename("wind.png","$data.png");

You are making the *very* common mistake of thinking that cronjobs run with
the exact same permissions and environment as your command line. Never,
never blindly assume that certain environment variables exist or that you
are running in a particular directory.

In fact, it makes good sense to assume these things even if you don't run it
as a cronjob, as it will make your script more robust and should enable to
to think more about data verification and error handling.

For the Perl side of things, *always* ensure you check for failures, ie:

rename "wind.png", "$data.png" or die "Can not rename wind.png to $data.png
because $!\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top