JaxPHP / JaxWeb Message Board › Cron Job fails to create file

Cron Job fails to create file

A former member
Post #: 41
On a shared server I created this cron job that is run once a day. Rather than say I have answered this for I will not know till tomorrow after it runs, I got rid of the $_SERVER and hard coded the relative directory. I could be rambunctious and create a test cron but in search of Google, I have seen many people who are having an issue with the file system difference in cron and browser run scripts. Plus an easy puzzle to go with your Saturday morning coffee!

Any answers on best way to handle directory structure with cron jobs?

<b>Warning</b>: fopen(/test/20090815am.php) [<a href='function.fopen'>function.fopen<­/a>]: failed to open stream: No such file or directory in <b>/home/nottherealdir/public_html­/nottherealdir/nottherealdirectory/bexpi­re.php</b> on line <b>96</b><br />
file exist already

//works through browser but not cron:
$datefile = date("Ymda");
$bizfile = $_SERVER['DOCUMENT_ROOT']."/test/"­.$datefile.".php";
// I create $QAB here
$fh = fopen($bizfile, 'w') or die("file exist already");
$begin = "<?php \n";
$end = "?>\n";
fwrite($fh, $begin);
fwrite($fh, $QAB);
fwrite($fh, $end);
fclose($fh);
chmod($bizfile , 0644);



ZipOnOver - Local Business Tools
Eric Noble
user 4016762
Group Organizer
Orange Park, FL
Post #: 40
It could be something simple; tell me if I'm reading this correctly.

So, your browser version works with:
$bizfile = $_SERVER['DOCUMENT_ROOT']."/test/"­.$datefile.".php";
­
and for the non-browser version you say:
I got rid of the $_SERVER and hard coded the relative directory.

but then you get the warning:
fopen(/test/20090815am.php­) ... failed to open stream: No such file or directory...

My simple observation is, "/test" isn't a relative directory, it's a directory named "test" in root. I'm guessing that directory doesn't exist, or if it does, you can't write to it. If that's the problem, you just need to lose the leading "/";

A former member
Post #: 42
No, I have not tested yet (too busy or too lazy). Actually too busy to worry as I have a few days to come to a solution on this.

the warning and code is what I woke up to this morning. (uncorrected)

the directory fopen(/test/20090815am.php) is indicating to me that $_SERVER['DOCUMENT_ROOT'] was not resolved. Plus I was not running the browser script from the same directory as the cronjob, so it could be that the test directory is not relative to the document root BUT that does not explain the document root not being resolved in the cron script.


Khalid Ajlouny
kajlouny
Jacksonville, FL
Post #: 5
When running a script as a cron, the $_SERVER super global is either unavailable or has different values. One option is to hard code the path, but this wouldn't be portable.

If you're running the cron as the same user (as opposed to ROOT), the code below figures the user and web root directories.

$user_info = posix_getpwuid( posix_geteuid() );
$user_root_dir = $user_info['dir'];
$web_root_dir = $user_root_dir . '/public_html';
A former member
Post #: 43
Thanks,
It does not need it to be portable for this situation so I just hard coded it. I did not test as it will run again tomorrow morning and should work.
(that is what I said yesterday wink ).



Just trying to get a signature image I like, other was too big!
A former member
Post #: 44
When running a script as a cron, the $_SERVER super global is either unavailable or has different values. One option is to hard code the path, but this wouldn't be portable.

If you're running the cron as the same user (as opposed to ROOT), the code below figures the user and web root directories.

$user_info = posix_getpwuid( posix_geteuid() );
$user_root_dir = $user_info['dir'];
$web_root_dir = $user_root_dir . '/public_html';

khfinity,
It worked! Though I had hard coded before my first post I mostly appreciate the information. I just removed the $_SERVER['DOCUMENT_ROOT'] and set to a relative directory in the script. My email this morning had no nasty messages and the file was created as advertised.

Tim
Eric Noble
user 4016762
Group Organizer
Orange Park, FL
Post #: 41
When running a script as a cron, the $_SERVER super global is either unavailable or has different values. One option is to hard code the path, but this wouldn't be portable.

If you're running the cron as the same user (as opposed to ROOT), the code below figures the user and web root directories.

$user_info = posix_getpwuid( posix_geteuid() );
$user_root_dir = $user_info['dir'];
$web_root_dir = $user_root_dir . '/public_html';

Thanks, Khalid. I didn't know how to make this kind of PHP script portable without $_SERVER. Like most of us, (probably) I usually just use PHP with Apache, but Tim's script is an example of how PHP can be used for other, related functions.
Khalid Ajlouny
kajlouny
Jacksonville, FL
Post #: 6
I've also seen an uglier solution, but haven't tested it. It calls your script through the domain name, which also allows you to use $_SERVER. If your provider allows using "wget", you can define your cron like this:

0 * * * * * wget http://www.your-domai...­
Eric Noble
user 4016762
Group Organizer
Orange Park, FL
Post #: 42
Thinking curl might be a little less ugly, I found this example.

I can think of 2 advantages of doing this with curl or wget, especially with shared hosting:

  • the script can remain part of the web site, and;
  • the cron job doesn't need to be on the web server.

A former member
Post #: 2
As a sysadmin, I do not recommend running a cron as root unless absolutely necessary. What if someone compromises that site? Then the code gets executed as the root user. Consider if you had a script trying to recursively delete something and you flubbed up the path? You could wipe out your server that way.

Also, if using wget via cron, be sure to throw away the output if you don't use it:

wget -O /dev/null -q http://www.domain.com...­

These options will route the HTTP output to /dev/null and suppress messages.

If you just run with wget, you may find you generate files with .1 .2 .3 suffixes.
Powered by mvnForum

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy