Accuracy of jobs scheduled in cron

Hello, I have several cron jobs scheduled but looking at the results of running I see in some cases it takes more than 2 seconds, is there any way to adjust the accuracy of execution of cron?
Is there any other tool or way to fix the problem?

Whats 2 seconds ??? peanuts!
Try to reproduce the same case twice, then you will realise how difficult it can be... for you say nothing about your boxes, what they are running how many users, are there concurent cron jobs scheduled?, any network issue at that time?, was someone printing?
...

There I have one job in /etc/crontab

00 12 * * * root /script.sh

in script.sh

#!/bin/bash
DATE=`date +%d_%m_%Y_%_A_%T_%N`
DIR=/
echo "$DATE" > $DIR/exec

Result file exec

11_12_2013_Wednesday_12:00:01_354995160
executing stat -c "%y" exec
2013-12-11 12:00:01.360761572 -0200

At the same time it`s only 1 job running, I don`t have other jobs.
You can see the delay in the execution of more than 1 second.
There is no network problem or overload in the machine, its a virtual machine vmware running Fedora 11.

Unless you are running realtime this is normal, expected behavior.

crond runs exactly once a minute. It has to check anyone of dozens of cron entries, then exec anything that needs to start.

If you need something to execute to within nanoseconds of a time frame you will have to:

  1. write C code with a realtime clock, like clock_gettime()
    clock_gettime
  2. have cron start your C job 1 minute early, make your C job check time constantly then execute the date command as close to the time you need.

You have a bad assumption I think.

You need to understand on a multiprocessing (not realtime) system that the scheduler tries to give all processes a shot at the cpu. That means all of the services you have running, ex.: on the zoned solaris 10 box I am on that is about 140+ processes. Many of them run at high or realtime priority, like zsched. These can preempt a cronjob at any time.

Do not attempt realtime priority on your own unless you are willing to have your code completely lock up the system. I noticed you seem to have root access, so you could do that. Given your question, this is a fair statement.

Ok I understand what you're explaining to me, but in practice I have to compile with gcc and run this source to ejectue anticipates the execution of the script before and at the time more precisely to that process is going to run it?

Which is executed by the sample source code program? as I can modify it to run my script.

thanks

Come again? I don't think that quite crossed the language barrier.

Why exactly does your compile job need to be run at exactly the correct millisecond?

Dear Corona, I need to parse some text files and send the results in more precise or approximate time, 1 second after the data no longer serve because they change constantly.

I am missing something here...

I suggest you re-read JM's and vbe's posts again and absorb what they are saying...

You want realtime access through a cron job to a set of files stored somewhere iside your networking ether that are constantly being read from and written to?

I would have thought your _HDD_ thrashing I/O was the biggest bottleneck assuming that you ARE suggesting real _files_...

This is not an easy task even for the big guns...

It is the application's job to tell you when it is done, then. Cron cannot do this, and it is not correct to assume your application is always ready at exactly the right time. What is your application?

Corona688, first want to run this script at a certain time at the exact millisecond and get the results I asked in the first post if cron or if there is any alternative, I get the results can be either a file on disk and can also be a variable in memory.

A little bit of lateral thinking here.
Instead of relying on the accuarcy of the cron job's timings why not create a shell script and let it run in the backround, OR, inside a visible terminal continuously...

#!/bin/sh
variable="The quick brown fox jumps over the lazy dog."
epochtime=$(date +%s)
newtime=$[ ( $epochtime + 10 ) ]

echo "$variable" > /tmp/sometext.txt
while true
do
	epochtime=$(date +%s)
	if [ $epochtime -le $newtime ]
	then
		read text < /tmp/sometext.txt
		variable=$text
		echo "Epochtime $epochtime = $(date)."
		echo "$text"
		echo "$variable"
		sleep 0.5
	else
		echo ""
		echo "Setting new time slot 10 seconds later..."
		echo ""
		epochtime=$(date +%s)
		newtime=$[ ( $epochtime + 10 ) ]
	fi
done

Results using the "sleep 0.5" in the code...
Apologies for the long printout...

Last login: Thu Dec 12 02:55:23 on ttys000
AMIGA:barrywalker~> chmod 755 timejob.sh
AMIGA:barrywalker~> ./timejob.sh
Epochtime 1386817822 = Thu 12 Dec 2013 03:10:22 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817822 = Thu 12 Dec 2013 03:10:22 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817823 = Thu 12 Dec 2013 03:10:23 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817823 = Thu 12 Dec 2013 03:10:23 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817824 = Thu 12 Dec 2013 03:10:24 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817824 = Thu 12 Dec 2013 03:10:24 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817825 = Thu 12 Dec 2013 03:10:25 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817825 = Thu 12 Dec 2013 03:10:25 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817826 = Thu 12 Dec 2013 03:10:26 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817826 = Thu 12 Dec 2013 03:10:26 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817827 = Thu 12 Dec 2013 03:10:27 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817827 = Thu 12 Dec 2013 03:10:27 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817828 = Thu 12 Dec 2013 03:10:28 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817828 = Thu 12 Dec 2013 03:10:28 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817829 = Thu 12 Dec 2013 03:10:29 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817829 = Thu 12 Dec 2013 03:10:29 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817830 = Thu 12 Dec 2013 03:10:30 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817830 = Thu 12 Dec 2013 03:10:30 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817831 = Thu 12 Dec 2013 03:10:31 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817831 = Thu 12 Dec 2013 03:10:31 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817832 = Thu 12 Dec 2013 03:10:32 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817832 = Thu 12 Dec 2013 03:10:32 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

Setting new time slot 10 seconds later...

Epochtime 1386817833 = Thu 12 Dec 2013 03:10:33 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817833 = Thu 12 Dec 2013 03:10:33 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817834 = Thu 12 Dec 2013 03:10:34 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817834 = Thu 12 Dec 2013 03:10:34 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817835 = Thu 12 Dec 2013 03:10:35 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817835 = Thu 12 Dec 2013 03:10:35 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817836 = Thu 12 Dec 2013 03:10:36 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817836 = Thu 12 Dec 2013 03:10:36 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817837 = Thu 12 Dec 2013 03:10:37 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817837 = Thu 12 Dec 2013 03:10:37 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817838 = Thu 12 Dec 2013 03:10:38 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817838 = Thu 12 Dec 2013 03:10:38 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817839 = Thu 12 Dec 2013 03:10:39 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817839 = Thu 12 Dec 2013 03:10:39 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817840 = Thu 12 Dec 2013 03:10:40 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817840 = Thu 12 Dec 2013 03:10:40 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817841 = Thu 12 Dec 2013 03:10:41 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817841 = Thu 12 Dec 2013 03:10:41 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817842 = Thu 12 Dec 2013 03:10:42 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817842 = Thu 12 Dec 2013 03:10:42 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817843 = Thu 12 Dec 2013 03:10:43 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817843 = Thu 12 Dec 2013 03:10:43 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

Setting new time slot 10 seconds later...

Epochtime 1386817844 = Thu 12 Dec 2013 03:10:44 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817844 = Thu 12 Dec 2013 03:10:44 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817845 = Thu 12 Dec 2013 03:10:45 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817845 = Thu 12 Dec 2013 03:10:45 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817846 = Thu 12 Dec 2013 03:10:46 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817846 = Thu 12 Dec 2013 03:10:46 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817847 = Thu 12 Dec 2013 03:10:47 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817847 = Thu 12 Dec 2013 03:10:47 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817848 = Thu 12 Dec 2013 03:10:48 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817848 = Thu 12 Dec 2013 03:10:48 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817849 = Thu 12 Dec 2013 03:10:49 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817849 = Thu 12 Dec 2013 03:10:49 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817850 = Thu 12 Dec 2013 03:10:50 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817850 = Thu 12 Dec 2013 03:10:50 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817851 = Thu 12 Dec 2013 03:10:51 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817851 = Thu 12 Dec 2013 03:10:51 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817852 = Thu 12 Dec 2013 03:10:52 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817852 = Thu 12 Dec 2013 03:10:52 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817853 = Thu 12 Dec 2013 03:10:53 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817853 = Thu 12 Dec 2013 03:10:53 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817854 = Thu 12 Dec 2013 03:10:54 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

Setting new time slot 10 seconds later...

Epochtime 1386817855 = Thu 12 Dec 2013 03:10:55 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Epochtime 1386817855 = Thu 12 Dec 2013 03:10:55 GMT.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
^C
AMIGA:barrywalker~> _