LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-01-2017, 02:04 PM   #1
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,921

Rep: Reputation: 158Reputation: 158
cron vs. anacron question


OS: CentOS 7.3

By the time I write this up I may have answered my own question - or not...

If I (as root) place a script in /etc/cron.daily it seems that anacron wil run it via run-parts per its scheduling scheme. In this case the machine was suspended over night and the job, created yesterday, ran a few minutes after the machine was resumed this morning. Cool!

Then I read the manual

The RHEL Administrator's Guide 22.1.2 Scheduling a Cron Job states
Code:
Scheduling Hourly, Daily, Weekly, and Monthly Jobs
To schedule an hourly, daily, weekly, or monthly job:
1. Put the actions you want your job to execute into a shell script.
2. Put the shell script into one of the following directories:
/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
From now, your script will be executed - the crond service 
automatically executes any scripts present in /etc/cron.hourly, 
/etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly 
directories at their corresponding times.
Which leads me to ask what are "their corresponding times"? Where are they specified? These times would seem to be different from/redundant to the anacron times.

Further confusing things, I read
Quote:
The root user uses the cron table in /etc/crontab, or, preferably, creates a cron table file in /etc/cron.d/.
There are no entries in /etc/crontab Looking in /etc/cron.d I see two files
Code:
[ken@vmCentOS7Mate cron.d]$ ll
total 8
-rw-r--r--. 1 root root 128 Aug  3 11:33 0hourly
-rw-r--r--. 1 root root 108 Jun 13 10:08 raid-check

[ken@vmCentOS7Mate cron.d]$ cat 0hourly 
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly

[ken@vmCentOS7Mate cron.d]$ cat raid-check 
# Run system wide raid-check once a week on Sunday at 1am by default
0 1 * * Sun root /usr/sbin/raid-check
raid-check is a crontab-like file. Does cron perhaps recognize it as so and treat it accordingly? As to the 0hourly - it looks like a script although it is not executable and has a crontab like entry at the bottom. These are both files from the default install. I do not have raid on the machine so why raid-check is here??? That said...

Given what is installed will the raid-check program fire at 1AM every Sunday? And, what about 0daily? If it was made executable would it run? when or where would I have to make the entry /etc/crontab or make a corresponding crontab like file in /etc/cron.d - or is it a self starting script? I am still a little confused on who is doing what to whom.

TIA,

Ken
 
Old 09-01-2017, 02:45 PM   #2
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,242
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
Have a look at anacrontab.
 
Old 09-01-2017, 05:29 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,158

Rep: Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980
/etc/cron.d and /etc/crontab are for system wide cron jobs that start at specific times.

Since CentOS 6 cron.daily, cron.weekly and cron.monthly jobs now run via anacron so they do not necessarily run at specific times. As suggested look at your /etc/anacrontab file. If the computer is not running 24/7 then at reboot the system will start any scheduled job after the delay time.

By scheduled anacron saves a timestamp of when the job was last run so if the period has been exceeded it will run after the delay time.

If the computer is running then anacron jobs will start at the beginning of the START_HOUR_RANGE after the associated delay.

Although the raid-check script is configured to run via a cronjob it may not be enabled. Check your /etc/sysconfig/raid-check configuration file.
 
1 members found this post helpful.
Old 09-02-2017, 07:40 AM   #4
sweepnine
LQ Newbie
 
Registered: Jun 2017
Posts: 16

Rep: Reputation: Disabled
Quote:
Originally Posted by taylorkh View Post
OS: CentOS 7.3
I am still a little confused on who is doing what to whom.
Yes this cron-anacron thing is not a glorious chapter in linux. cron and anacron implement different concepts:
  • cron: do something a speficic time
  • anacron: do somthing once in a specific period of time

I guess you knew that. How do they interplay?
  • cron runs as deamon and once in a minute it looks at its table and execute the entries matching the given time
  • ananacron may also run a deamon. But I think in centos this is not the case. Instead anacron is called once a start up and by some cron-job (lets say once an hour or once a day but anyway in cron-like manner, i.e. at a specific time)
  • when anacron is called it looks in its table and other notes what to do
    • is there any job that should run once a day but has not been run today?
    • if so should I run it wright away of after some (random or fixed) delay?

Some confusion may arise from crontab-entries like

Code:
 
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
At 6:25 AM it checks whether /usr/sbin/anacron executable, i.e. available, and only if this is not the case, as a sort of failback, the jobs in cron.daily/ are called directly instead of being called via anacron.

update:

If, however, anacron is present the above above crontab entry doesn't do anything. Instead, anacron triggers the scripts by an entry in /etc/anacrontab like the following.
Code:
 
1       5       cron.daily      run-parts --report /etc/cron.daily
It runs once any 1 days with 5 minutes delay, has the identifier 'cron.daily' and lets the command run-parts execute all scripts in /etc/cron.daily/.

So, in a typical linux, anacron is installed, but when you want to know what is does you have to look in cron.daily/ that belongs to cron.

To provide some more confusion, there is a script /etc/cron.daily/anacron0
Code:
 
#!/bin/sh
...
anacron -u cron.daily
that at first sight looks like it calls anacron-entry 'cron.daily' from ancron-tab. But it actually only lets anacron make note that what the anacron-entry 'cron.daily' usually does has already been triggered. This is only needed because it might be that the scripts in cron.daily/ are not called via anacron but from somewhere else, and then anacron could not know its already done.

In my opinion thats all rather quirky and indeed confusing.

Last edited by sweepnine; 09-03-2017 at 08:28 AM.
 
1 members found this post helpful.
Old 09-02-2017, 10:31 AM   #5
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,921

Original Poster
Rep: Reputation: 158Reputation: 158
Thanks lazydog,

My /etc/anacrontab file reads
Code:
[root@vmCentOS7Mate ~]# cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1	5	cron.daily		nice run-parts /etc/cron.daily
7	25	cron.weekly		nice run-parts /etc/cron.weekly
@monthly 45	cron.monthly		nice run-parts /etc/cron.monthly
so my daily job will run between 3:05 AM and 3:50 AM or at least will be placed in the job queue starting at some time in that interval. As this is on a desktop/workstation and not a server it runs shortly after I resume the machine about 6 AM. This is well and good.

And thanks michaelk. raid-check is not present as a .service and there is no .conf file for it. I guess it might be a stub or an example in /etc/cron.d.

sweepnine, thank you for your insight. crond appears in /etc/sysconfig and thus would be started by the systemcfg process. I find the file /etc/cron.hourly/0anacron by which cron will attempt to fire up anacron on an hourly basis. The 0anacron file will only fire up anacron if it has NOT been run during the current day (provided also that the system is not on battery). Amazing

Adding a job to the schedule "thing" is simple. Now I have pulled enough threads to get some handle on what the heck is going on behind the scenes. Thanks again to all!

Ken

p.s. I will give this thread a couple of days before marking it SOLVED - in case someone corrects my vision of reality
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning cron : CentOS7 : anacron and cron fanoflq Linux - Newbie 2 11-24-2016 10:50 PM
understanding anacron (with/without cron) vincix Linux - Newbie 2 07-09-2015 03:55 PM
Anacron job 'cron.daily' cccc Debian 15 05-20-2008 01:39 PM
Does cron/anacron retry failed tasks? enigma_0Z Linux - General 8 03-26-2008 05:58 AM
cron / anacron / fcron Will Linux - General 3 11-09-2001 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:11 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration