Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hello! I'm Alex and I'm an intermediate Linux user. Here is the issue I present, for community consumption: When I run: Code: (as root): cat crontab # m h dom ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    7

    Cron time not consistent ....


    Hello! I'm Alex and I'm an intermediate Linux user.

    Here is the issue I present, for community consumption:

    When I run:
    Code:
    (as root): cat crontab
    # m h dom mon dow user  command
    1 * * * * root cd / && run-parts --report /etc/cron.hourly
    0 3 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
    0 3 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
    0 3 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    My cron.daily folder SHOULD be running at 3am, everyday.

    One of my scripts in the cron.daily folder is set to output when it runs to a log file:

    Code:
    echo "Last backup was run at"; date +%c >> /home/misteralexander/lastbackup.log
    However, when I cat this file, i find this:

    Code:
    Sat Apr 20 07:52:44 2013
    Sun Apr 21 08:00:13 2013
    Mon Apr 22 07:50:24 2013
    My first assumption was a time mismatch so I ran:

    Code:
    hwclock --systohc
    Although that didn't change anything. I didn't think it would, as the system and hardware were already within milliseconds of each other anyway.

    So, now I'm just plain stumped. 1.) Why is it not running at 3am everyday? 2.) If its just a matter of the time being off, then shouldn't it be running at 07:52:44 everyday? Why the variance?

    Looking for guidance!

    -Alex.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hello and welcome!

    could it be that there are other scripts in cron.daily that are getting run before your script, and they are taking up that time (between 3am and 7:52am and 8am) to complete?

    Is there anything in the cron log that would help? (usually /var/log/cron)

    If you really need granular control over the time it is run, you could put it in root's personal cron. just run "crontab -e as root and add something like:
    Code:
    0 3 * * * /path/to/cron/script.sh > /tmp/script.log 2>&1
    notice you do not need the USER field in personal cron jobs. i added the logging portion to show you how you could capture STDERR/STDOUT of the script, if you want/need to see what is going on during the cronjob.

  3. #3
    Just Joined!
    Join Date
    Sep 2009
    Posts
    7
    So, the weird thing is this .... when it was in Root's personal crontab NOTHING RAN. As soon as I moved it to cron.daily, it started to run .... late.

    So, I renamed the script to 1a_sysupdate. From what I was reading, CRON does run things sequentially and does wait for one to finish before running the others. There is a script called "0Anacron" that must be run BEFORE all others.

    However, with all these changes, my script still ran today at 2013-04-24 07:37:34

    I'm completely stumped!

    Any help is help & I appreciate it!

    -Alex.

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by misteralexander View Post
    So, the weird thing is this .... when it was in Root's personal crontab NOTHING RAN. As soon as I moved it to cron.daily, it started to run .... late.
    That is mysterious. I would do two things:

    1. look in the system cron log (/var/log/cron or similar). run a tail of it in one window, then add a cron job as root (using "crontab -e") and make sure you see the activity in the system cron log.

    2. in your root cronjob, did you add the logging bit like in my example? it is key to troublshooting things just like this. the order is important, too, e.g:

    Code:
    somecommand > /tmp/my.log 2>&1

  6. #5
    Just Joined!
    Join Date
    Sep 2009
    Posts
    7
    So, this is strange. When I type (as root):
    Code:
    cd /var/log/cron
    it tells me there is no file or directory. So, I go to (as root):
    Code:
    cd /var/log
    and then LS the directory -- there is nothing for or about cron. That's odd, because i HAVE had things in Crontab before (entered with -e, checked with -l, and ultimately removed with -r). So, there should be something from back then.

    As for your logging request -- I think I'm confused -- the script I have (just a very simple bash update script) already has a line that outputs to a log ..... this is how I know the file is getting run late. The logging you are requesting, how is that different?

    I appreciate your help.
    -Alex.

  7. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by misteralexander View Post
    So, this is strange. When I type (as root):
    Code:
    cd /var/log/cron
    it tells me there is no file or directory. So, I go to (as root):
    Code:
    cd /var/log
    and then LS the directory -- there is nothing for or about cron
    okay, try the syslog then: /var/log/syslog. failing that, look for files containing CRON in them:
    Code:
    sudo find /var/log/ -type f -exec grep -l CRON {} \;
    As for your logging request -- I think I'm confused -- the script I have (just a very simple bash update script) already has a line that outputs to a log ..... this is how I know the file is getting run late. The logging you are requesting, how is that different?
    it is different b/c the cron job log will contain the shell STDOUT (Standard Out) and STDERR (Standard Error) that get created when the cron daemon calls the script. if you do any logging INSIDE your script, then that STDOUT/STDERR belongs to that script's subshell. the point is that if anything goes wrong when trying to call the script, the log defined inside your script won't get written to, but the logfile specified in the cronjob will.

    also, out of curiosity, show the output of this command:
    Code:
    sudo crontab -l

  8. #7
    Just Joined!
    Join Date
    Sep 2009
    Posts
    7
    I understand what you're saying about logging, I hadn't thought about that before. Here is what is inside the syslog:

    Code:
    <<~ root [ /var/log ] ~>>  cat syslog | grep -i cron.daily
    Apr 23 07:51:28 awesome-sauce anacron[2351]: Job `cron.daily' terminated (mailing output)
    Apr 24 03:00:01 awesome-sauce CRON[23321]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ))
    Apr 24 07:30:01 awesome-sauce anacron[26315]: Will run job `cron.daily' in 5 min.
    Apr 24 07:35:01 awesome-sauce anacron[26315]: Job `cron.daily' started
    Apr 24 07:35:01 awesome-sauce anacron[26338]: Updated timestamp for job `cron.daily' to 2013-04-24
    Apr 25 03:00:01 awesome-sauce CRON[17792]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ))
    <<~ root [ /var/log ] ~>>
    It looks like despite what I have declared, cron.daily just isn't get run until 0730 -- which (LOL) bothers me even more! Also, per your request, there is not anything inside crontab at the moment, as I removed the job out of frustration. I'm going to put it back right now, with your logging suggestion -- tomorrow I should have an update as to what is happening with that.

    It wasn't like this before, but here is what I have JUST done:
    Code:
    <<~ root [ ~ ] ~>>  crontab -l
    1 3 * * * /root/sysupdate 2>&1 /home/misteralexander/update_error.log
    <<~ root [ ~ ] ~>>
    The file 'sysupdate' is an executable file. Here is what's inside it:
    Code:
    #!/bin/bash
    
    #####################
    # Update The System #
    #####################
    
    	apt-get update -y
    
    ######################
    # Upgrade The System #
    ######################
    
    	apt-get upgrade -y
    
    ############################
    # Remove Unneeded Packages #
    ############################
    
    	apt-get autoremove -y
    
    #############################
    # Update The Linux Database #
    #############################
    
    	updatedb
    
    ###################
    # Report Success! #
    ###################
    
    	date +%c >> /home/misteralexander/lastupdate.log
    
    ######################
    # Reboot the machine #
    ######################
    
    #	reboot now
    
    #############
    # All done! #
    #############
    I have commented out the reboot part, until I can get this timing thing fixed.

    Again, many thanks!

    -Alex.

  9. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by misteralexander View Post
    It wasn't like this before, but here is what I have JUST done:
    Code:
    <<~ root [ ~ ] ~>>  crontab -l
    1 3 * * * /root/sysupdate 2>&1 /home/misteralexander/update_error.log
    <<~ root [ ~ ] ~>>
    you've got the file descriptors in the wrong place. change it to this:
    Code:
    /root/sysupdate > /home/misteralexander/update_error.log 2>&1
    The file 'sysupdate' is an executable file. Here is what's inside it:
    Code:
    #!/bin/bash
    
    #####################
    # Update The System #
    #####################
    
    	apt-get update -y
    
    ######################
    # Upgrade The System #
    ######################
    
    	apt-get upgrade -y
    
    ############################
    # Remove Unneeded Packages #
    ############################
    
    	apt-get autoremove -y
    
    #############################
    # Update The Linux Database #
    #############################
    
    	updatedb
    
    ###################
    # Report Success! #
    ###################
    
    	date +%c >> /home/misteralexander/lastupdate.log
    
    ######################
    # Reboot the machine #
    ######################
    
    #	reboot now
    
    #############
    # All done! #
    #############
    okay, your "date" command is getting called after all the apt-get calls, which could potentially take a lot of time. if you want to know when the script was started, move that call to the very top of the script.

  10. #9
    Just Joined!
    Join Date
    Sep 2009
    Posts
    7
    I have fixed the descriptor:
    Code:
    <<~ root [ ~ ] ~>>  crontab -l
    1 3 * * * /root/sysupdate > /home/misteralexander/update_error.log 2>&1
    <<~ root [ ~ ] ~>>
    I also moved the 'date' to the very top .... ready for a run at 3:01AM (hopefully).

    Why would "date" take any longer to run if it were after or before APT? Somewhere I can read/learn about the difference?

    I will report what happens in the morning!

    -Alex.

  11. #10
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by misteralexander View Post
    Why would "date" take any longer to run if it were after or before APT? Somewhere I can read/learn about the difference?
    The "date" command runs and returns pretty much instantaneously. it has nothing to do with APT, per se. it is just that the command are run serially (one after the other), not in parallel. in other words, the first command (apt-get blah or updatedb) has to finish executing before the date command can be run.

    consider this simple script:
    Code:
    #!/bin/bash
    echo hello i am $0
    echo -n "it is now ";date
    sleep 10
    echo -n "it is now ";date
    the first date command is run, then sleep runs (for 10 seconds), then another date is run.

    in fact, that might be a good test for your cron script: put a date call at the beginning AND end of it, to see when it started and finished.

Page 1 of 2 1 2 LastLast

Posting Permissions

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