Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Cron job running sporadically

  1. #1
    Join Date
    May 2005
    Location
    US
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Cron job running sporadically

    I created a root-run cron job using
    Code:
    sudo crontab -e
    This is roughly what it looks like
    Code:
    30 7,13 * * 1-5 /usr/bin/php /var/www/path/to/nameofscript.php > /dev/null
    And when I check with
    Code:
    ps -ef|grep cron
    it shows that cron is running:
    Code:
    root 975 1 0 Jan14 ? 00:00:01 cron
    username 35075 34975 0 17:27 pts/0 00:00:00 grep --color=auto cron
    Am I doing this wrong? I'm trying to have that PHP script run twice a day every weekday.

    It seemed to go well the first day or so, but after that, I've had to run the script manually (by visiting it in a web browser).

    Any tips? Thanks in advance.

  2. #2
    Join Date
    Jul 2013
    Location
    Wisconsin
    Beans
    1,877

    Re: Cron job running sporadically

    Well, could be a lot of possible problems:
    Does the php script require a display? Root cron jobs wouldn't have one.
    Is the machine shut down or suspended at either of those times?
    Is the script or data encrypted? Root cron jobs may not have the key to decrypt.
    Could the permissions of the script or file have changed?

    You can probably find out the problem if you redirect your output to a file instead of /dev/null.

  3. #3
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    9,036
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Cron job running sporadically

    Grep /var/log/syslog for "cron". Do you see any errors?

    I presume this script runs correctly from the command prompt as the root user. However, if you can run the script from a browser, doesn't that mean it works as user "www-data," the user that owns the Apache process, rather than root?

    I agree with Ian. Replace "/dev/null" with "/var/log/nameofscript" and see what it reports.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  4. #4
    Join Date
    May 2005
    Location
    US
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Cron job running sporadically

    Thanks for the replies.

    The PHP script does not require a display, and the machine is never shut down or suspended. Nothing encrypted. And I don't believe the permissions have changed. What's baffling to me is that it sometimes runs. So you'd think if it were a permissions issue (root doesn't have permission, which also makes no sense, since root always permission) that it would never run.

    In the /var/log/syslog, I see
    Code:
    CRON[36922]: (root) MAIL (mailed 1 byte of output; but got status 0x0001, #012)
    and then
    Code:
    CRON [36943]: (root) CMD ( [ -x /usr/lib/php5/maxlifetime ] && [ -x /usr/lib/php5/sessionclean ] && [ -d /var/lib/php5 ] && /usr/lib/php5/sessionclean /var/lib/php5  $(/usr/lib/php5/maxlifetime))
    Not sure if that's bad or not.

    But I'll definitely take the suggestion of outputting to a file instead of /dev/null, just to see what happens.

  5. #5
    Join Date
    May 2005
    Location
    US
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Cron job running sporadically

    Unfortunately, I'm not really any closer to a solution. I did have it output to a text file instead of to /dev/null, but it's pretty clear it's just running whenever it feels like it. For example, it ran just fine this morning (at 7:30am) with no prompting. At 1:30pm, however, nothing... I had to run it manually.

    Can I get confirmation from any cron experts that this first part of the line does what I think it does?
    Code:
    30 7,13 * * 1-5
    If I understand this correctly, it will run the script at 7:30am and 1:30pm every weekday. Is that right?

  6. #6
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    5,571
    Distro
    Xubuntu 14.10 Utopic Unicorn

    Re: Cron job running sporadically

    I would add 2>&1 to the end of the crontab line (when writing to a log file) in order to catch any error output as well. It may come up with something informative.

  7. #7
    Join Date
    May 2005
    Location
    US
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Cron job running sporadically

    Thanks for the suggestion. So if I tweak it, it'd be something like this?
    Code:
    30 7,13 * * 1-5 /usr/bin/php /var/www/path/to/nameofscript.php > /home/username/logfile.txt 2>&1
    or more like this?
    Code:
    30 7,13 * * 1-5 /usr/bin/php /var/www/path/to/nameofscript.php 2>&1 /home/username/logfile.txt
    I guess I'm not really sure where the 2>&1 goes...

  8. #8
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    5,571
    Distro
    Xubuntu 14.10 Utopic Unicorn

    Re: Cron job running sporadically

    The first one. It redirects stdout to the logfile, and redirects stderr to the same place as stdout.

    There is a shortcut notation that works with bash but not sh:
    Code:
    /usr/bin/php /var/www/path/to/nameofscript.php >& /home/username/logfile.txt
    Last edited by The Cog; January 27th, 2014 at 09:21 PM.

  9. #9
    Join Date
    May 2005
    Location
    US
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Cron job running sporadically

    Thanks. I'll give that a go.

  10. #10
    Join Date
    May 2005
    Location
    US
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Cron job running sporadically

    I think it may be something in the cron statement itself, because the 7:30am one seems to launch fine... it's just the 1:30pm one that doesn't launch and doesn't even seem to write anything to the log file.

    Can someone verify that this is the best way to say "twice every weekday at 7:30am and 1:30pm"?

    Code:
    30 7,13 * * 1-5
    Maybe it's not 13? I thought 13 meant 1pm...

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •