1

I have a script that write into a file two datetime of two server. This script is launched every minute but the second stored is not always the same.

Ex:

2015-12-16 0:16:01.864  2015-12-16 0:16:01.972
2015-12-16 0:17:01.241  2015-12-16 0:17:01.350
2015-12-16 0:18:01.626  2015-12-16 0:18:01.735
2015-12-16 0:19:02.102  2015-12-16 0:19:02.210

As you can see the fourth line is written with the 02 second and not the 01.

in crontab i have this situation:

* * * * * root cd /home/Project; ./PollDateServer.sh >/dev/null 2>&1

Thank you

Andrea
  • 383
  • 1
  • 5
  • 19
  • It really depends on the server. crontab will run it at `around` that time, but it is not 100% sure it will be exactly that moment. If you have to rely on that, you may need to add an extra check in the script. – fedorqui Dec 16 '15 at 15:51
  • http://stackoverflow.com/questions/21920975/linux-task-schedule-to-hour-minute-second : take a look at http://quartz-scheduler.org/ – Ôrel Dec 16 '15 at 15:53
  • 4
    You can't ask that kind of accuracy of cron. You need a completely different solution, like find develop something perhaps? A wee bit of python/perl could achieve what you want if you don't mind going down that path. – Michael Dec 16 '15 at 15:53
  • @Ôrel I don't think it's really quite a duplicate, as this question is about very high sub second accuracy, where as those don't seem to care, they just want cron with seconds. Very close to being a duplicate though. – Michael Dec 16 '15 at 15:55
  • 1
    This is impossible unless you had some highly specialised machine. There is no way to guarantee a command will be run the same microsecond as another. – 123 Dec 16 '15 at 16:00
  • 1
    @123 Your point is well made in that we use pre-emptive OS' so almost nothing is GUARANTEED, but the OP is asking for the same second, which to me implies sub second accuracy, but not necessarily micro second accuracy. This is probably possible in 99.99% of cases. – Michael Dec 16 '15 at 16:03
  • @Michael I cannot link to my second link, but quartz is a solution. And what you want is a precision of second, so it is the same problem – Ôrel Dec 16 '15 at 16:10

1 Answers1

-1

If you start the script every minute, you may give "at" a try:

* * * * * echo 'cd /home/Project; ./PollDateServer.sh' | at now + 1 minutes

The "at" package have to be installed!

EDIT:

a more accurate version without "at":

* * * * * sleep $(echo 60-$(date '+\%S.\%N') | bc); cd /home/Project; ./PollDateServer.sh

accuracy so far:

22:10:00.007780247
22:11:00.003994573
22:12:00.007836359
22:13:00.005249502
22:14:00.003309822
22:15:00.004220122
22:16:00.004105306
22:17:00.006213730
22:18:00.006020664
22:19:00.003666325

how does it work:

$ date '+%S.%N'
17.416097038

... gives the current second and nanoseconds

$ echo 60-$(date '+%S.%N')
60-17.416097038

... generates an expression to calculate the remaining seconds and nanoseconds to the next full minute.

$ echo 60-$(date '+%S.%N') | bc
42.583902962

... calculates the remaining seconds and nanoseconds to the next full minute

... and finally sleep that long:

$ sleep $(echo 60-$(date '+%S.%N') | bc); date '+%H:%M:%S.%N'
12:10:00.004634130

... sleeps the calculate time and gives back the current time

st0ne
  • 4,165
  • 2
  • 23
  • 24
  • 2
    Err, NO. That would absolutely NOT do what the OP wants. Even if 'at' was guaranteed or even expected to be any more accurate than cron, if the cron scheduler was a second late then the at job would go in a minute late. – Michael Dec 16 '15 at 16:44
  • Yes... You are right. The job runs a minute to late. But as you can see in the question, the job is meant to run every minute. And my example runs: every minute. – st0ne Dec 16 '15 at 19:18
  • In my tests, the "at"-version is more accurate than cron-only. Give it a try. Even if cron is several seconds too late. The at-scheduler starts the script in second 0 of the following minute. At my point of view, my answer meets the two requirements of the question:It runs every minute in the same second. – st0ne Dec 16 '15 at 20:23
  • @St0ne The version without "at" means that every minute at "00" the job is runned by cron, but what is the meaning of sleep $(echo 60-$(date '+\%S.\%N') | bc)? it forces the script to be runned with a precision of nanosecond? – Andrea Dec 17 '15 at 10:48
  • I will describe it in the post – st0ne Dec 17 '15 at 10:53
  • @st0ne. Thank you this is very clear now...so this is a trick to augment the precision of a scheduled job into the cron? and why the job without this command starts with a delay? Many many thanks – Andrea Dec 17 '15 at 11:23
  • @Andrea: Due to cron's maximal precession of one minute, i think it does what it is intendet to: It starts the job within the given minute. – st0ne Dec 17 '15 at 11:43