0

I'm trying to run a very simple PHP script on cron on a third-party hosting server and I have encountered a weird problem. The script's contents is NOT executed although I got confirmed that the cron does execute the script in scheduled times. I cannot check the Cron settings as it is not on my server, yet I would like to know if there is something I do wrong?

The script is for testing purpose only and thus it is very simple:

<?php
$file = fopen("file-" . rand(1000, 9999) . '.txt', 'w');
fwrite($file, 'cron');
fclose($file);
?>

When I run this script manually, the file is created. When I leave it on Cron, nothing happens. What can be the cause? The permissions of the script are 777. Do I do something wrong or is the problem on a server?

Many thanks.

2 Answers 2

1

Remember that a script running under cron has a very different environment than the same script being run manually on the command line. In particular, cron will have a different working directory than the shell prompt. You don't specify an absolute path for your file, so when the script is run by cron, it'll try to create that file in whatever directory is the CWD at that time. If the userid this job runs under doesn't have write permissions on that directory (or the file exists but owned by someone else), then the fopen will fail.

Never assume that a file operation succeeds. Always check for error conditions. And while you're at it, specify an absolute path for the file, so you KNOW where it will be written:

$filename = "/path/to/where/you/wantfile-" . rand(1000,9999) . '.txt';
$file = fopen($filename, 'w') or die("Unable to open $filename for output");
Sign up to request clarification or add additional context in comments.

4 Comments

I see. Thanks, I'll try. Nevertheless, this was just an example, I need to use the CRON to insert a line into a database table. I use regular php functions (mysql_connect, mysql_select_db, mysql_query) with correct login data and the result is the same - manually OK, nothing happens with CRON. No relative addresses are used.
What's your cron line specification? Remember that command line PHP scripts require a shebang (#!/usr/bin/php). Otherwise it's just a text file that can't be executed (even if <?php is present within).
Sorry I don't have access to that since it's running on a third party server whose services I use. But since all other scripts from all other users are okay and I have the information that my scripts are actually executed, I assume there won't be a problem with that
Well, I've figured it out. I have no clue why, but I created a complete new script with new name and added it to cron (with exactly the same contents) and it works like a charm.
0

This probably because the script is run as some user other than apache. In some cases I will use curl and make the script check to see if the IP is the local IP to make sure it is being run from the right machine. There certainly less hackish ways to accomplish this, but this is always a quick and easy one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.