LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cron job (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-869176/)

deeppal123 03-17-2011 08:57 AM

cron job
 
a job1 is registered first and job2 is registered second in crontab. But when scheduled time is reached, job2 is executed first and job1 is executed second.
Please can anyone give any possible reason of second registered job being executed first instead of first registered job.

TB0ne 03-17-2011 09:39 AM

Quote:

Originally Posted by deeppal123 (Post 4293840)
a job1 is registered first and job2 is registered second in crontab. But when scheduled time is reached, job2 is executed first and job1 is executed second.
Please can anyone give any possible reason of second registered job being executed first instead of first registered job.

This, along with your other posts, sound VERY much like homework.

You say 'registered'...what do you mean? The only way cron will execute jobs, is by chronological order. If Job2 is scheduled to run at 7 AM, and job1 at 8 AM, then that's what they'll do, regardless of the order in which they're 'registered' in cron. If they're both scheduled at the same time, one may take a longer time to fire up a command/query/whatever than the other, so things may not start processing at exactly the same time.

m_kane 03-17-2011 10:38 AM

Quote:

Originally Posted by deeppal123 (Post 4293840)
a job1 is registered first and job2 is registered second in crontab. But when scheduled time is reached, job2 is executed first and job1 is executed second.
Please can anyone give any possible reason of second registered job being executed first instead of first registered job.

Can't you schedule them to run different time if you want them to run in specific order? Eg. second job a minute later that first.
Other way is to create script which run jobs in specific order and then put that script to crontab.

deeppal123 03-17-2011 11:31 PM

well i am new to linux,so obviously questions can seem to be homework.but if u remember clearly there was a lot of follow up work that i did after querying.

TB0ne 03-18-2011 10:02 AM

Quote:

Originally Posted by deeppal123 (Post 4294614)
well i am new to linux,so obviously questions can seem to be homework.but if u remember clearly there was a lot of follow up work that i did after querying.

Spell out your words, and write clearly. And having not posted to one of your questions before, I don't 'remember' you doing follow up work, but just see what you posted in your threads.

And I'm sorry, but the questions you've posted are written as if they were homework assignments, described as if they were, and ask questions that would be in many, many, courses. For example:
  • Find a file location in current directory(recursive directory) without using find command.
  • how to find all child processes of a parent process given to script as argument. i would like to know the methodology as well as approach.
And this thread. The questions are very homework-like, especially since in a real-world environment, why WOULDN'T you be able to use find? Why would you need to write a script to find child processes, when you can type "ps" at a command-prompt? (these things don't come up in production environments).??

szboardstretcher 03-18-2011 10:40 AM

Quote:

Originally Posted by TB0ne (Post 4293903)
This, along with your other posts, sound VERY much like homework.

You say 'registered'...what do you mean? The only way cron will execute jobs, is by chronological order. If Job2 is scheduled to run at 7 AM, and job1 at 8 AM, then that's what they'll do, regardless of the order in which they're 'registered' in cron. If they're both scheduled at the same time, one may take a longer time to fire up a command/query/whatever than the other, so things may not start processing at exactly the same time.

I think what he is asking, which i have to say was hard to tell at first, is that, assuming:

* 5 * * * /scripts/go.sh
* 5 * * * /scripts/go3.sh
* 5 * * * /scripts/go2.sh

What order will they run in. And the answer is.... No idea.

Random?

vonedaddy 03-18-2011 10:45 AM

Quote:

Originally Posted by szboardstretcher (Post 4295079)
I think what he is asking, which i have to say was hard to tell at first, is that, assuming:

* 5 * * * /scripts/go.sh
* 5 * * * /scripts/go3.sh
* 5 * * * /scripts/go2.sh

What order will they run in. And the answer is.... No idea.

Random?

I believe they would all run at the same time. There is no reason multiple processes could not be launched at the exact same time.

szboardstretcher 03-18-2011 10:47 AM

Quote:

Originally Posted by vonedaddy (Post 4295084)
I believe they would all run at the same time. There is no reason multiple processes could not be launched at the exact same time.

According to /var/log/cron, and a little test I just ran, with 3 scripts just like above...

It says that they are running from the Top Down.

savona 03-18-2011 01:40 PM

Quote:

Originally Posted by szboardstretcher (Post 4295087)
According to /var/log/cron, and a little test I just ran, with 3 scripts just like above...

It says that they are running from the Top Down.

Are you sure they are just not logged in order? Fora test put a sleep in the first script and see if it gets logged last.

deeppal123 03-22-2011 12:00 AM

well i just did something like this
5 * * * * echo 1 >/home/temp/abc.txt
5 * * * * echo 2>/home/temp/abc.txt

and followd it up wd same kind of jobs.Now thing is when i change the time,the first job is not being executed first,instead jobs are executing randomly.one thing is at a given time,only one job can be executed,but is there any way where we can specify the order in which jobs are going to be executed? like the first job gets executed first and so on,instead of some random job getting executed.

chrism01 03-22-2011 01:16 AM

cron is designed to try and launch jobs as close as possible to their requested times in the crontab file(s). If multiple jobs are requested to run with the SAME time params, then the order is undefined (to within a small time period).
This goes double for multi-core systems.
The internal src code likely does it in the order specified in the crontab.
Unless you are measuring down to extremely fine levels, this is not likely to be evident.

If you REALLY need jobs to be run in a specific order, have one job call another. Do NOT rely on crontab order.
You MAY use a time difference to separate the jobs, IF you can guarantee that job1 will finish within the time allowed before job2 commences.

deeppal123 03-22-2011 08:08 AM

@vonedaddy----> yes it works fine and accordin to the order the commands are enlisted in the shell script and then on putting the shell script in the cron tab it works just fine.thanks a lot for helping out.


All times are GMT -5. The time now is 09:59 AM.