By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

Perl as a cron job: permissions error

P: 20
Hello everyone,

I am running a database maintenance script to remove old entries from a log file.

The script runs as expected when initiated manually:

Expand|Select|Wrap|Line Numbers
  1. ./maintain.pl $HOME/website/database/ $HOME/logs/
  2.  
However, when it is run as a cron job, the err.log file I have set up for errors generates an error:

Expand|Select|Wrap|Line Numbers
  1. Cannot open lock file!
  2.  
I assume this is line 36 of the Perl:

Expand|Select|Wrap|Line Numbers
  1. open LOCK, ">$logDirectory/maintenance.loc" or die "Cannot open lock file!\n";
  2.  
I tried to fix this problem by changing the permissions on $HOME/website/database/ to 777, however this did not fix the problem.

The original cron job was:

Expand|Select|Wrap|Line Numbers
  1. 0 0 * * * $HOME/maintain.pl $HOME/website/database/ $HOME/logs/ 2>~/err.log
  2.  
I have subsequently changed the job multiple times, and while the job always runs it generates the same error.

Any idea why it isn't getting permission to create the lock file?
Sep 9 '07 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hello everyone,

I am running a database maintenance script to remove old entries from a log file.

The script runs as expected when initiated manually:

Expand|Select|Wrap|Line Numbers
  1. ./maintain.pl $HOME/website/database/ $HOME/logs/
  2.  
However, when it is run as a cron job, the err.log file I have set up for errors generates an error:

Expand|Select|Wrap|Line Numbers
  1. Cannot open lock file!
  2.  
I assume this is line 36 of the Perl:

Expand|Select|Wrap|Line Numbers
  1. open LOCK, ">$logDirectory/maintenance.loc" or die "Cannot open lock file!\n";
  2.  
I tried to fix this problem by changing the permissions on $HOME/website/database/ to 777, however this did not fix the problem.

The original cron job was:

Expand|Select|Wrap|Line Numbers
  1. 0 0 * * * $HOME/maintain.pl $HOME/website/database/ $HOME/logs/ 2>~/err.log
  2.  
I have subsequently changed the job multiple times, and while the job always runs it generates the same error.

Any idea why it isn't getting permission to create the lock file?
That is peculiar considering that when a job is run under Cron that it is Root doing the executing. Why not try modifying your open command to be the following:

Expand|Select|Wrap|Line Numbers
  1. open LOCK, ">$logDirectory/maintenance.loc" or die "Cannot open lock file! - $!\n";
  2.  
The "$!" will print the error that the system is throwing back from the attempt to open the lock file, instead of just your custom error, that way you can see exactly why it cannot open it.

Regards,

Jeff
Sep 9 '07 #2

KevinADC
Expert 2.5K+
P: 4,059
how is $HOME getting expanded inside the cron job arguments? It's not getting interpreted as a literal string?

I would expnad on Jeffs suggestion to incude the file name to make sure the variable is defined correctly:

Expand|Select|Wrap|Line Numbers
  1. open LOCK, ">$logDirectory/maintenance.loc" or die "Cannot open lock file [ $logDirectory/maintenance.loc ] - $!\n";
  2.  
Sep 9 '07 #3

P: 20
Great idea guys!

It turns out that the server's ~/logs directory has some screwy permissions that can't be played with. Problem solved!

Thanks for that!
Sep 10 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.