Wednesday, January 19, 2011

How to write this simple cronjob in Linux (Debian)?

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.

  • 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?

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

    From Vin-G

0 comments:

Post a Comment