I need help with cron job
Moderator: General Moderators
I need help with cron job
Okay, first off let me say I'm a relative newbie, so please be kind.
I've created a crontab task in my Plesk Control Panel. Here is the command:
/usr/bin/php -q /var/www/vhosts/mysite.com/httpdocs/crontask.php
This task works fine as long as the file it is calling (in this case crontask.php) doesn't use includes or anything that calls another outside file. That's when it simply doesn't work.
Now I realize this is an environment issue and running the script through crontab task is NOT like running the script through a browser. What I can't figure out is how to either 1) amend the crontab command to run the script as though it was being executed from a browser where relative paths work, or 2) amend the php file so any includes or whatever can be properly called on.
I've tried placing this at the top of the php file (above <?php): #!/usr/bin/php
But that doesn't work.
I've tried using absolute paths in the php file such as:
include("/var/www/vhosts/mysite.com/httpdocs/crontask_include1.php");
But that doesn't work.
I've tried just about everything I can think of and have spent hours looking for solutions online but to no avail.
Does anyone here have an idea how to help a newbie like me with what I realize is probably a simple solution? I would be very grateful.
Thanks!
I've created a crontab task in my Plesk Control Panel. Here is the command:
/usr/bin/php -q /var/www/vhosts/mysite.com/httpdocs/crontask.php
This task works fine as long as the file it is calling (in this case crontask.php) doesn't use includes or anything that calls another outside file. That's when it simply doesn't work.
Now I realize this is an environment issue and running the script through crontab task is NOT like running the script through a browser. What I can't figure out is how to either 1) amend the crontab command to run the script as though it was being executed from a browser where relative paths work, or 2) amend the php file so any includes or whatever can be properly called on.
I've tried placing this at the top of the php file (above <?php): #!/usr/bin/php
But that doesn't work.
I've tried using absolute paths in the php file such as:
include("/var/www/vhosts/mysite.com/httpdocs/crontask_include1.php");
But that doesn't work.
I've tried just about everything I can think of and have spent hours looking for solutions online but to no avail.
Does anyone here have an idea how to help a newbie like me with what I realize is probably a simple solution? I would be very grateful.
Thanks!
Re: I need help with cron job
I suppose it's a Plesk (or your hosting company) related problem.
It works for me (
(c) ):
By the way, you need to make your PHP file executable if you want to use shebang line (i.e. #!/usr/bin/php):
It works for me (
Code: Select all
vladsun@ubuntu:~$ cat /var/www/1.php
#!/usr/bin/php
<?
include("2.php");
`echo "OK\n" >> /home/vladsun/1.php.txt`;
vladsun@ubuntu:~$ cat /var/www/2.php
<?php
`echo "Included!\n" >> /home/vladsun/1.php.txt`;
vladsun@ubuntu:~$ sudo cat /var/spool/cron/crontabs/vladsun
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.eSRwsi/crontab installed on Fri Sep 11 10:59:16 2009)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# m h dom mon dow command
*/1 * * * * /var/www/1.php
vladsun@ubuntu:~$ cat 1.php.txt
Included!
OK
Included!
OK
Included!
OK
Included!
OK
vladsun@ubuntu:~$Code: Select all
vladsun@ubuntu:~$ ls -la /var/www/1.php
-rwxr-xr-x 1 vladsun vladsun 82 2009-09-11 10:58 /var/www/1.phpThere are 10 types of people in this world, those who understand binary and those who don't
Re: I need help with cron job
There are several differences between ISAPI and CLI... you should read the CLI manual.
First, about the includes - the current directory is the directory from which the file is called, and paths are relative to it; it's not the directory in which the called script resists.
So the save way to include a file is not:
But:
In this way the path is absolute, and it's not hard coded
If this do not resolve the issue, post your code
First, about the includes - the current directory is the directory from which the file is called, and paths are relative to it; it's not the directory in which the called script resists.
So the save way to include a file is not:
Code: Select all
include('somepath/somefile.php');Code: Select all
include(dirname(__FILE__) . '/somepath/somefile.php');If this do not resolve the issue, post your code
Re: I need help with cron job
Are you sure about this? I've tried it and it shows that CLI include is relative to "the directory in which the called script resists".Darhazer wrote:There are several differences between ISAPI and CLI... you should read the CLI manual.
First, about the includes - the current directory is the directory from which the file is called, and paths are relative to it; it's not the directory in which the called script resists.
Code: Select all
vladsun@ubuntu:~/Desktop$ cat ../1.php
#!/usr/bin/php
<?php
include "2.php";
echo 1;
vladsun@ubuntu:~/Desktop$ cat ../2.php
<?php
echo 2;
vladsun@ubuntu:~/Desktop$ ../1.php
21There are 10 types of people in this world, those who understand binary and those who don't
Re: I need help with cron job
VladSun... try:
Then read:
http://bg2.php.net/cli
Code: Select all
/usr/bin/php -f /home/vladsun/1.phphttp://bg2.php.net/cli
It does not change the working directory to that of the script. (-C and --no-chdir switches kept for compatibility)
Re: I need help with cron job
I did try it
Code: Select all
vladsun@ubuntu:/$ php -f /home/vladsun/1.php
21
vladsun@ubuntu:/$ cat /home/vladsun/*.php
<?php
include "2.php";
echo 1;
<?php
echo 2;
vladsun@ubuntu:/$ php -v
PHP 5.2.4-2ubuntu5.7 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug 21 2009 19:52:39)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with Xdebug v2.0.2, Copyright (c) 2002-2007, by Derick Rethans
with Suhosin v0.9.22, Copyright (c) 2007, by SektionEins GmbHThere are 10 types of people in this world, those who understand binary and those who don't
Re: I need help with cron job
OK, maybe it depends on some circumstances, I had headaches with such scripts on live servers.
And another reason you have to use some kind of absolute path is that your script may be included, and I'm sure in that case the include in your script will be relative to the path of the script that included yours.
And another reason you have to use some kind of absolute path is that your script may be included, and I'm sure in that case the include in your script will be relative to the path of the script that included yours.
Re: I need help with cron job
Darhazer wrote:OK, maybe it depends on some circumstances...
Yep, I agreeVladSun wrote:It works for me ((c) ):
Strange ... Check for permission issues - maybe the user running crontab has no permission to read the file you are trying to include ...geronimo wrote:I've tried using absolute paths in the php file such as:
include("/var/www/vhosts/mysite.com/httpdocs/crontask_include1.php");
But that doesn't work.
Also, check your crond logs.
There are 10 types of people in this world, those who understand binary and those who don't