LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Cron job on Debian 6 (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-on-debian-6-a-4175508745/)

hzakaryna 06-21-2014 05:46 PM

Cron job on Debian 6
 
Hi all,

I am having a hard time to run a cron job in Debian 6.
Basically i have a sample php script test.php that just adds a line of string(on every execution) to a text file named people.txt
Both test.php and people.txt are located on my Apache document root /var/www directory. The script works fine standalone, but when i want to execute it every minute (for example) through a Cron Job it does not work.

I setup a Cron Job as a root user, so when i run "crontab -e" as root user i see this line on Nano
*/1 * * * * /var/www/test.php

when i run /etc/init.d/cron status i see message
'cron is running'
But my people.txt file is not getting updated

Any advise on how to make Cron Jobs run on my system?

Thanks,
Hayk

norobro 06-21-2014 09:09 PM

Quote:

Originally Posted by hzakaryna (Post 5191824)
The script works fine standalone

From the above I assume that you have a shebang in your script. If so try removing it and call the php cli (man php) from crontab:
Code:

*/1 * * * * php -f /var/www/test.php

hzakaryna 06-21-2014 11:59 PM

I did not have shebang in my script.
Added the above parameters to crontab entry, restarted the cron, but still no change.
Standalone the script runs just fine.

Maybe the problem is with the cron in the system and not with this particular script?
Any steps you would suggest to debug?

Thanks,
Hayk

chrism01 06-22-2014 03:09 AM

I'd check /var/log/cron (or the equiv on your system), but its the case that the env given to cron jobs is minimal hence its recommended to specify full/absolute pathnames for cmds & data files used.
You could post the script content if you want

hzakaryna 06-23-2014 12:39 AM

I checked /var/log/syslog and i see these lines related to cron

Quote:

Jun 22 22:29:01 moodletest /USR/SBIN/CRON[13834]: (root) CMD (php -f /var/www/test.php)
Jun 22 22:30:01 moodletest /USR/SBIN/CRON[13834]: (root) CMD (php -f /var/www/test.php)
Does this mean that the cron job successfully runs every minute?

Here is my php script:
Quote:

<?php
$file = 'people.txt';
$current = file_get_contents($file);
$current.= "Hayk\n";
file_put_contents($file,$current);
?>
I will appreciate any further advise for debugging, seems like cron jobs do not run on my system at all?

Thanks,
Hayk

evo2 06-23-2014 12:44 AM

Hi,

as already mentioned you should specify full paths to files. EG
Code:

<?php
$file = '/path/to/people.txt';
$current = file_get_contents($file);
$current.= "Hayk\n";
file_put_contents($file,$current);
?>

Evo2.

hzakaryna 06-23-2014 01:59 AM

Thanks Evo,

Adding the absolute path fixed the issue and the cron job works fine.

If i want to have my test.php script outside Apache document root directory (for example in my home directory /home/haykz) what should i change in the Crontab entry?

Thanks again for all the help.

Hayk

evo2 06-23-2014 02:04 AM

Hi,
Quote:

Originally Posted by hzakaryna (Post 5192369)
Adding the absolute path fixed the issue and the cron job works fine.

Great.
Quote:

If i want to have my test.php script outside Apache document root directory (for example in my home directory /home/haykz) what should i change in the Crontab entry?
You just need to change the path to the script. Eg you'd have something like:
Code:

*/1 * * * * php -f /home/haykz/test.php
However, you should say which user you want this job to run as. Ie you should add that to the crontab of the appropriate user. Have a look at the crontab man page, and post back if you have more questions.

Evo2.

hzakaryna 06-23-2014 03:17 PM

It worked just fine!

Thanks for all the help.

Hayk


All times are GMT -5. The time now is 01:29 PM.