tags:

views:

96

answers:

6

I have a cron job that ideally I want to run hourly (it's on the FB accelerators and deals with realtime social stuff, so once an hour doesn't cut it)

1) Can I have a cron job that contains "run(), sleep(60), run()..." that lasts for 60 minutes?

2) Frequency of running aside, it currently outputs:

...web/public/mysqltest.php: ?php: cannot open
...web/public/mysqltest.php: require_once: not found
...web/public/mysqltest.php: include_once: not found
...web/public/mysqltest.php: include_once: not found
...web/public/mysqltest.php: syntax error at line 5: `(' unexpected

(the rest of the path is there).

How can I start debugging that message? It's odd to me that my server can't open a PHP file. When run in a browser it does its job admirably. The first 6 lines of the script are:

<?php
require_once 'facebook.php';
include_once 'lib.php';
include_once 'config.php';
$facebook = new Facebook($api_key, $secret);
get_db_conn();

Nothing super fancy as far as I can tell.

I can confirm that the include/required files are indeed in the same directory. Do they need a certain level of access permissions? Any and all help would be appreciated.

+2  A: 
  1. I'm not familiar with that.

  2. You should probably be calling /path/to/php file.php in your cron script. It looks like it is currently trying to use bash instead.
Justin
Or by putting #!/usr/bin/php at the first line of the php and executing it directly like this ./script.php
Andrejs Cainikovs
A: 

If you specify absolute paths rather than relative paths in your script, cron will be able to find them. Your script doesn't run in it's directory when run from cron.

Brian Ramsay
+2  A: 

It looks like the PHP source is being executed as a list of shell commands - make sure you're running it through the PHP interpreter.

Greg
Exactly! Clear answer.
Andrejs Cainikovs
+2  A: 

1 There is no limits for cron script execution time. Personally, I have a cron php script that lasts for more than 45 minutes after it's execution. Be aware, that in this case you may need to change php.ini, because it will contains php script execution time limitation.

2 You should put following

#!/usr/bin/php

at the start of your php script. This way you will enable it's execution from bash like a usual script.
After this is done and is tested, put in cron.

Andrejs Cainikovs
+3  A: 

Since its being called internally, its not being parsed by the server. The beginning of your cron job should include the path to your php parser

eg:

/usr/bin/php /home/path_to/php_file/yourfile.php

youll need to find your own server's path to php of course

Patrick
Or by putting #!/usr/bin/php at the first line of the php and executing it directly like this ./script.php
Andrejs Cainikovs
A: 

You must be aware that your environment variables are not defined with cron.

In your session you may call

php my_php_file.php

and it works because php is into a directory of your PATH variable.

It's always recommended to use full path names in a cron job.

You may execute a script that define all variables you need too.

Luc M
Or by putting #!/usr/bin/php at the first line of the php and executing it directly like this ./script.php
Andrejs Cainikovs