I need help with cron job

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
geronimo
Forum Newbie
Posts: 5
Joined: Thu May 14, 2009 9:59 am

I need help with cron job

Post by geronimo »

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!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: I need help with cron job

Post by VladSun »

I suppose it's a Plesk (or your hosting company) related problem.
It works for me ( ;) (c) ):

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:~$
By the way, you need to make your PHP file executable if you want to use shebang line (i.e. #!/usr/bin/php):

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.php
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: I need help with cron job

Post by Darhazer »

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:

Code: Select all

include('somepath/somefile.php');
But:

Code: Select all

include(dirname(__FILE__) . '/somepath/somefile.php');
In this way the path is absolute, and it's not hard coded
If this do not resolve the issue, post your code
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: I need help with cron job

Post by VladSun »

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.
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".

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 
21
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: I need help with cron job

Post by Darhazer »

VladSun... try:

Code: Select all

/usr/bin/php -f /home/vladsun/1.php
Then read:
http://bg2.php.net/cli
It does not change the working directory to that of the script. (-C and --no-chdir switches kept for compatibility)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: I need help with cron job

Post by VladSun »

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 GmbH
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: I need help with cron job

Post by Darhazer »

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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: I need help with cron job

Post by VladSun »

Darhazer wrote:OK, maybe it depends on some circumstances...
VladSun wrote:It works for me ( ;) (c) ):
Yep, I agree :)
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.
Strange ... Check for permission issues - maybe the user running crontab has no permission to read the file you are trying to include ...
Also, check your crond logs.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply