Permisions with cron jobs

EdgarZouk

New member
Joined
Jul 26, 2006
Messages
2
Hello.

I'm new with perl and DA, so i need a bit of help.

My problem is that I can run a perl script from my browser without any problem, it opens a file, reads it and continues... but if I try to run the same script from a cron job, the script begins to execute but it wont open the file.

The cron daemon send me a mail saying:

Cannot open ../Aniversaris/subscribers.txt: No such file or directory at /home/stuff/domains/host.com/public_html/cgi-bin/dsendhtml.pl line 19.

Script is in the cgi-bin directory with 755 permision and the file and directory I want to read have 755.

Is cron jobs using diferent permisions?
Why can I run the script from cron jobs?

Thank you very much.
 
Hello,

I'm guessing it's a "current working directory" problem.

cgi-scripts that are run through apache run from the directory they're in. Eg, the process is run from within the cgi-bin directory.

When run through a cron job, the current working directory will always default to:

/home/username

so when a script that lives in the cgi-bin, but is being run from a working directory of /home/username, any relative access of files, such as "../Aniversaris/subscribers.txt" would actually be trying to access /home/Aniversaris/subscribers.txt instead of the desired script.

For this reason, you can do one of a few things.

1) Always use full paths when accessing files in scripts (not always an easy option)

2) change the working directory of your script before it's run. A sample way of doing that would be to setup your cronjob command as:
Code:
cd /home/username/domains/domain.com/public_html/cgi-bin && ./yourfile.cgi
so that the cgi is being run from the directory that it's in.

John
 
Hello again.

Thank you very much! Both solutions worked perfectly.

Before posting the promblem I tried with the full path as you said but I wrote http://www.domain.com/......./subscribers.txt and now I see why it didnt work.

Thanks again for the quick solution and for the clearliness of the answer.

Perfectly understood!!!
 
Back
Top