1
votes

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?

3
There's alson man 5 crontab. I think you would get what you want with 1 * * * * - spelufo
No, thats every hour and a minute. What you could do is run every minute, and have your process be sleep 1 && whatever. - spelufo
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

3 Answers

2
votes

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).

1
votes

Try something like:

*/1 * * * *    sleep 1; mycommand 
^^                ^^
every minute   one second
1
votes

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?