LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-21-2014, 05:46 PM   #1
hzakaryna
LQ Newbie
 
Registered: Jun 2014
Posts: 5

Rep: Reputation: Disabled
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
 
Old 06-21-2014, 09:09 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by hzakaryna View Post
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
 
1 members found this post helpful.
Old 06-21-2014, 11:59 PM   #3
hzakaryna
LQ Newbie
 
Registered: Jun 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-22-2014, 03:09 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 06-23-2014, 12:39 AM   #5
hzakaryna
LQ Newbie
 
Registered: Jun 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-23-2014, 12:44 AM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
Old 06-23-2014, 01:59 AM   #7
hzakaryna
LQ Newbie
 
Registered: Jun 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-23-2014, 02:04 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,
Quote:
Originally Posted by hzakaryna View Post
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.
 
Old 06-23-2014, 03:17 PM   #9
hzakaryna
LQ Newbie
 
Registered: Jun 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
It worked just fine!

Thanks for all the help.

Hayk
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Debian daily cron job won't run, but does run in cron.hourly. sandersch Linux - General 7 05-24-2012 01:50 AM
Debian cron job log elainelaw Linux - Newbie 1 03-14-2010 11:07 PM
linux cron job duplicate job question cpthk Linux - Newbie 4 09-11-2009 08:52 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
scheduling monthly job in cron from debian sarge 3.1 sridhar11 Debian 2 10-28-2005 09:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration