Trouble with scripts when using cron

software development

#1

I wrote a script in php that references another file in another folder. In order to access the file, I am referencing it as “…/file.txt”. When I manually execute the script myself, everything works. However when the cronjob executes, I receiver an error:

Warning: main(…/file.txt): failed to open stream: No such file or directory in /home/.loadie/xyz/xyz.com/files/script.php on line 3

In my cronjob, I am executing this line while using the web panel tool:

/usr/local/bin/php /home/xyz/xyz.com/files/script.php

Any help would be appreciated.


#2

Does /usr/local/php5/bin/php /home/xyz/xyz.com/files/script.php work?


CEO Norm

Opinions are my own views, not DreamHosts’.
I am NOT a DreamHost employee OK! :@

Act on my advice at your own risk!


#3

instead of …/file.txt (which is probably not starting from the directory you expect it to), try something like:

dirname(dirname($_SERVER[‘SCRIPT_NAME’])) . ‘/file.txt’

the two dirname()s handle removing /script.php and then /files from the end of /home/xyz/xyz.com/files/script.php

track7 - my dream-hosted site


#4

dirname(dirname($_SERVER[‘SCRIPT_NAME’])) . ‘/file.txt’ did not work, but clearly was on the right track. Instead of using a relative path name, I just changed it to an absolute path name. I assume there is a more elegant way of doing it with system variables, but as long as it works…