Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to ask about cron jobs. OK, we place the scripts in the crontab and the cron daemon executes them.

Now if I understand this, each minute, cron checks the crontab of each user and executes the configured scripts. But how is this actually done? Does it fork child processes etc?

It can not execute the tasks sequentially since the timings would be lost (e.g. due to waiting a long-running script to finish). So how is this actually implemented.

Just to help out, I'm not looking for low level code. A high level description (of the algorithm perhaps?) or how this is implemented in most distros would suffice to me.

share|improve this question
add comment

1 Answer

I found this Q&A over on StackOverflow titled: How does cron internally schedule jobs?.

excerpt from that post & the wikipedia article on cron

The algorithm used by this cron is as follows:

1. On start-up, look for a file named .crontab in the home directories of 
   all account holders.

2. For each crontab file found, determine the next time in the future that
   each command is to be run.

3. Place those commands on the Franta-Maly event list with their corresponding
   time and their "five field" time specifier.

4. Enter main loop:

   1. Examine the task entry at the head of the queue, compute how far in 
      the future it is to be run.

   2. Sleep for that period of time.

   3. On awakening and after verifying the correct time, execute the task 
      at the head of the queue (in background) with the privileges of the 
      user who created it.

   4. Determine the next time in the future to run this command and place 
      it back on the event list at that time

This SuperUser Q&A titled: How does cron work? covers some of your additional questions. For example your question as to how does cron deal with jobs that are scheduled for the same time. One of the answers in that thread states that as the cron daemon processes each task, it forks each scheduled job so that no single job will act as a blocker for jobs that have overlapping times.

share|improve this answer
    
If it is a queue then tasks run one after the other (those in the same queue). So if a task needs 3 mins to execute and the next task in the queue is expected to be scheduled in the next minute, how does this happen? –  Jim Jul 11 '13 at 5:58
    
@Jim - I think one simplification that you're assuming is that it's reading the files every minute when it's doesn't do this at all. When you save the file, the crond daemon is incorporating the contents of the crontab for a given user then and adding it to the queue in a sorted order based on the time in the future that it's suppose to run. –  slm Jul 11 '13 at 11:22
    
Ok.So in this case, the queue could have tasks that the should be run concurrently.So child processes are forked or something? –  Jim Jul 11 '13 at 19:23
    
@Jim - exactly. Also see my additional comments in the answer. –  slm Jul 11 '13 at 19:42
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.