Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I need assistance in getting a cronjob to run properly in Linux (Debian). (This is my first one).

I need a file to "run" once per hour a set number of times. I got this far:

crontab -e

no crontab for root - using an empty one

edited crontab file: 0 2-21 * * * /www/file.php

crontab: installing new crontab

This was designed to make the cronjob run beginning at 2am (or 200 hrs) and to run each hour on the hour until 2100 hrs. I checked the database to see if the file did its thing at 2:10 and nothing had changed so it clearly didn't run.

I'm fairly new to Linux so there's probably a way to see what happened but I'm really wondering whether I wrote the Cronjob instruction correctly before I get into diagnosing.

share|improve this question

migrated from stackoverflow.com Dec 18 '09 at 6:03

This question came from our site for professional and enthusiast programmers.

    
# m h dom mon dow command –  Adam Dec 17 '09 at 8:59
    
# 42 3-22 * * * /var/www/apache2-default/getUserDetails.php?friend=14522828 –  Adam Dec 17 '09 at 9:00
    
Did you check your Email for output/error messages from cron? –  unbeknown Dec 17 '09 at 10:02

2 Answers 2

Your crontab looks completely fine to me.

Are you sure that you have the appropriate execute bits set and the proper shebang so that the root can execute the file /www/file.php?

share|improve this answer
    
I'm not sure what that is. It's 4am here right now so I will probably wait until the AM to read that. I posted up exactly what I am entering in above (so it should have run starting at 3:42am). I've tried it with or without the hashtag. Is it possible that the server time doesnt coincide with the computer time? –  Adam Dec 17 '09 at 8:58

Regarding this:

42 3-22 * * * /var/www/apache2-default/getUserDetails.php?friend=14522828

You can't put the ? there. It will consider the ? along with the rest after it as part of the file name from the file system (it's looking for the file getUserDetails.php?friend=14522828 at /var/www/apache2-default). What you can do is:

42 3-22 * * * /usr/bin/php-cgi /var/www/apache2-default/getUserDetails.php friend=14522828

(assuming the php-cgi executable is in /usr/bin, change accordingly if php is installed elsewhere)

Alternatively, you could edit getUserDetails.php and have the following line at the very top of the file:

#!/usr/bin/php-cgi
<?php
[your code goes here]
...

Then make sure getUserDetails.php is executable by root (check via ls -l, assign via chmod) and then have the following schedule in cron:

42 3-22 * * * /var/www/apache2-default/getUserDetails.php friend=14522828

After that, you can monitor /var/log/cron for commands being run by crond. And then you can check root's mail (mail command) for the program output or errors encountered.


if on the other hand what you really had in your crontab was

0 2-21 * * * /www/file.php

Then I'm stumpped. The entry does look fine so what you need to make sure is that you can run the file manually in the command line (enter /www/file.php in the shell). Add the execute bits for it if not executable.

After that, check /var/log/cron as well as root's mail for both commands being executed and errors.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.