Cron job does not execute

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mih
Forum Newbie
Posts: 12
Joined: Tue Jul 28, 2009 12:32 am

Cron job does not execute

Post by mih »

Hello,

From the CPanel documentation
if I have a file x.php in my www directory, what should I put as the command in the standard cron jobs section to run that file?

You'll need to put the full path to that file as the command. Check the main screen of your cPanel interface for your home directory. It's likely something close to /home/username.

Now, add the directory that the file is in. For example, if your username is ron, and your home directory is /home/ron, you could run that file with /home/ron/www/x.php.

You will need to make sure that x.php is executable. You can check in the File Manager or an FTP program to make sure that the file has permissions necessary for execution (likely 755).

Note: Unless you want x.php to be available to the public, place it in /home/ron/ instead. It doesn't have to be in your www or public_html directory to be run by a cron job.
So I did this the path being

Code: Select all

/home/<my user>/public_html/random.php
and the error (I got it through e-mail)

Code: Select all

/home/<my user>/public_html/random.php: line 1: ?php
: No such file or directory
/home/<my user>/public_html/random.php: line 2: syntax error near unexpected token `"con.php"'
/home/<my user>/public_html/random.php: line 2: `include("con.php");
'
What does it mean ?

random.php starts like that

Code: Select all

<?php
include("con.php");
............
And if I 'execute' the file in the URL it does what it is supposed to do.

Why's that ?

Mihai
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cron job does not execute

Post by requinix »

You have to put a

Code: Select all

#!/usr/bin/php -q
at the very top of the file. That tells the system what it's supposed to use to execute the file.

The /usr/bin/php is the path to the php binary. Yours may be different: cPanel should have a place listing various file paths.
mih
Forum Newbie
Posts: 12
Joined: Tue Jul 28, 2009 12:32 am

Re: Cron job does not execute

Post by mih »

Thank you tasairis !

That was it.

Now I do not receive an e-mail, it is supposed to be sent even if it is no error.

CPanel:
NOTE: You will receive a message every time this job runs. Using your main e-mail address is not recommended.
But you can't have everything :D . Prefer this rather than the alternative.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cron job does not execute

Post by requinix »

Actually, unless something special is supposed to happen, you'll only get an email if your script outputs anything (in which case the email will contain the script output). No output means no email.
Post Reply