Forum Moderators: coopster

Message Too Old, No Replies

How do I run my php script with a cron job?

getting error

         

4string

12:58 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



I am getting an error when I try to execute a php script with a cron job.

The error is:
/home/mydir/cleanup.php: line 1:?php: No such file or directory
/home/mydir/cleanup.php: line 4: syntax error near unexpected token `'/home/mydir/mysql_connect.php''

Is the cron job not expecting php? It's reading the file and access permissions are set correctly. Confused.

Thanks for any help!

gliff

1:10 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



What's your cron job line look like?

My quickest guess is you're trying to call the php script directly, like this

5 * * * * /home/mydir/cleanup.php

and cron is trying to run the php file as a shell script.

If you want to call your php scripts this way, you need to let *nix know what interpreter to use. Add something like the following to the top of your php file and you should be fine

#!/path/to/php

(Where /path/to/php is the full path to the php binary)

If this isn't an option, you'll need to run the php binary with your script as an argument. Your cron would look something like this

5 * * * * /path/to/php /home/mydir/cleanup.php

If you have access to a command line, the command [whereis php] or the command [locate php] should help you find the php binary.

MrSpeed

2:19 pm on Jun 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's my command I use in CPanel to run php scripts.
wget -O /dev/null [yourdomain.com...]

4string

3:04 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



Thank you both!

I got it working now. MrSpeed's command worked for me in cPanel.

4

4string

5:22 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



I have another related question to this.

How do I return values from the script to be emailed to me via the cron job? For instance, I'd like a value showing how many and which records were deleted by the clean up script.

Holzberg

5:51 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



curl works well for cron jobs as well :)

Holzberg

5:57 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



Oh, and to send yourself an email all you do is use the php mail function to send yourself an email inside the script with the ninformation you need.

4string

6:24 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



I figured I could do a mail(), but I thought I could return some value to the cron job since it already emails a successful result after running. This is all new to me (obviously). Thanks.

Anatoly_UA

8:13 am on Jul 2, 2005 (gmt 0)



I have a problem:
HTTP request sent, awaiting response...
07:45:02 ERROR -1: No data received.

My script is writing to file and work a long time. How can I start it from cron job?