2

I'm developing a application that needs a cron to be executed every minute and a second. I mean, I need to run a software every 01:01min (or 61 seconds).

I already read some stuff about cron's and I also used some good online generators to get a lot of useful cron, but I didn't know how I can generate this one. Check what I've read until the moment:

Please, can someone help me create this cron?

Community
  • 1
  • 1
Paladini
  • 4,522
  • 15
  • 53
  • 96
  • There's alson `man 5 crontab`. I think you would get what you want with `1 * * * *` – spelufo Jan 25 '15 at 09:06
  • No, thats every hour and a minute. What you could do is run every minute, and have your process be `sleep 1 && whatever`. – spelufo Jan 25 '15 at 09:08
  • `cron` only does 1-minute resolution. It can't do what you want to do, at least not directly. Is there some reason you can't use something other than cron? – Keith Thompson Jan 26 '15 at 01:19
  • Related question [How to set crontab every 1 hour 1 minute?](https://stackoverflow.com/a/41082308/14928633) with answer about [`at`](https://man7.org/linux/man-pages/man1/at.1p.html#EXAMPLES). – kirogasa Mar 03 '23 at 16:11

3 Answers3

2

Cron can't do this. It only handles 1-minute resolutions. You can use sleep to run a command a specified number of seconds after HH:MM:00, but it can't run a command every 61 seconds.

Here's a Perl script that should do what you want:

#!/usr/bin/perl

use strict;
use warnings;

my @command = @ARGV;

while (1) {
    sleep 61 - time % 61;
    system @command;
}

The sleep call sleeps until the value of time() reaches the next multiple of 61 seconds (measured since the epoch, 1970-01-01 00:00:00 UTC).

Unlike crontab, this script won't continue running across reboots unless you do something to restart it. If you're using Vixie cron, you can use a a @reboot cron job to restart it.

Also unlike crontab, if the command takes longer than 61 seconds to run, the next iteration will be quietly skipped. (That might be a desirable feature).

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

Try something like:

*/1 * * * *    sleep 1; mycommand 
^^                ^^
every minute   one second
SMA
  • 36,381
  • 8
  • 49
  • 73
  • No, that will run `mycommand` every 60 seconds at one second after every minute, e.g., at 00:00:01, 00:01:01, 00:02:01, etc. The OP wants 00:00:00, 00:01:01, 00:02:02, etc -- every 61 seconds. – Keith Thompson Jan 26 '15 at 01:17
  • */1 means every minute. If i would have specified 1 instead of */1, then it would been the case what you said. – SMA Jan 26 '15 at 09:19
  • Yes, `*/1` means every minute. With the `sleep 1`, `mycommand` is run every minute at one second after the minute. `1` would run the command once an hour. In any case, it's not what the OP is looking for. – Keith Thompson Jan 26 '15 at 09:34
  • I believe OP said it needs to run every 61 seconds "I mean, I need to run a software every 01:01min (or 61 seconds)." and is what its going to do. – SMA Jan 26 '15 at 09:59
  • Yes, exactly. The OP wants the command to run every 61 seconds. Your version will run every 60 seconds. – Keith Thompson Jan 26 '15 at 15:19
  • run every 60 seconds + sleep 1 = 61 seconds. I am not sure what's wrong here. – SMA Jan 26 '15 at 16:31
  • Suppose we start at midnight. The first three runs are at 00:00:01, 00:01:01, and 00:02:01. To meet the OP's requirement, the'd have to be at 00:00:00, 00:01:01, and 00:02:02. Your `sleep 1` doesn't change the interval between successive runs. – Keith Thompson Jan 26 '15 at 16:37
1

A normal cron will fail with de leap-second: 00:01:01 / 00:02:02 / 00:03:03 / .. / 00:59:59 / 01:01:00 / ..

Cron is nice, so do not change it for a complete new solution. Add support in your script

check_program_not_already_running
if [ -f ${SLEEPFILE} ]; then
   sleepsecs=$(cat ${SLEEPFILE})
   (( sleepsecs = sleepsecs + 1 ))
   sleep ${sleepsecs}
else
   sleep 1
   sleepsecs = 2
fi
echo ${sleepsecs} > ${SLEEPFILE}
do_your_thing

check_program_not_already_running: You must do something (pidfile / ps / smart cron schedule) to make sure that after 59 runs still only 1 process is sleeping.

Optimize: When ${sleepsecs} = 60, reset the counters and skip 1 run.

Design: What happens the second day? Restart or keep counting with the script above?

Walter A
  • 19,067
  • 2
  • 23
  • 43