2

I want to schedule a command every 1 hour and 1 minute. For example, if the first command executes at 01:01 pm, the next command will execute at 01:02PM; the time between the command executions is 1 hour and 1 minute.

I tried using

*/1 */1 * * *

but it runs every minute. Can anyone help me?

Zorawar
  • 6,505
  • 2
  • 23
  • 41
user3854431
  • 113
  • 1
  • 2
  • 6
  • unclear from your title, versus you two example date/times. Do you mean to launch a program every `61` minutes, or every `60` minutes, or ?? Not really a programming Q for Stackoverflow. Please move to http://superuser.com OR http://unix.stackexchange.com . Good luck. – shellter Dec 11 '16 at 05:01
  • i think i need to change title, How to set crontab every 61 minute – user3854431 Dec 11 '16 at 07:33
  • 2
    I think you mean 01:01 pm and 02:02 pm. – Keith Thompson Dec 11 '16 at 08:16
  • Related [How to do a cron job every 72 minutes](https://stackoverflow.com/q/745901/14928633) – kirogasa Mar 03 '23 at 16:14

2 Answers2

2

You can use this method which tells it to run every 61 minutes after the cron job.

while true
do
  # do stuff here every 61 minutes
  sleep 61m
done

Another option:

Cron can easily run every hour, but 61 minutes is harder to achieve.

The normal methods include using a sleep command or various rather elaborate methods in the script itself to fire off every 61 minutes.

A much simpler method is using cron's cousin, the at command. The at command will run through a file and run all the commands inside, so you just need to place the commands in a file, one per line, then add this line to the bottom of the file:

at now + 61 minutes < file

The commands can be any type of one-liner you want to use.

Here is an example. Call this file foo and to kick off the execution the first time, you can simply run: sh foo

date >> ~/foo_out
cd ~/tmp && rm *
at now + 61 minutes < ~/foo

That will output the date and time to ~/foo_out then move to a tmp directory and clean out files, then tell the at command to run itself again in 61 minutes which will again run the at command after executing the rest.

norcal johnny
  • 2,050
  • 2
  • 13
  • 17
  • My pleasure. If it works out please click the answer if not please let me know what I can do to further help. cheers! – norcal johnny Dec 11 '16 at 02:30
  • @norcaljohnny - not downvoting, but this behavior will require a change in system configuration thanks to the way systemd works and how it has infected most major distributions. https://linux.slashdot.org/story/16/05/29/212204/systemd-starts-killing-your-background-processes-by-default – ivanivan Dec 11 '16 at 02:39
  • @ivanivan thanks for the input. :) Are you referring to the first or second part of the answer. As the OP said he likes the second half I added. Cheers! – norcal johnny Dec 11 '16 at 02:41
  • @norcaljohnny - systemd has to be configured to ignore processes started in screen, or wtih nohup, etc. Or you have to change your command line to let systemd know about it. The slasdot discussion I linked to has a decent summary of what happens... TMYK! – ivanivan Dec 11 '16 at 03:17
  • That will drift, depending on how long the command takes to run. – Keith Thompson Dec 11 '16 at 07:58
  • @KeithThompson Thanks for the input. I believe the OP's and a lot of peoples concern is the fact that 0-59 is what is used and past the 60 min mark people are uncertain how what can be done between 1hour and 2hour intervals. – norcal johnny Dec 11 '16 at 12:42
  • I don't see how that addresses my concern. Take a look at my answer. – Keith Thompson Dec 11 '16 at 19:38
  • @KeithThompson It was not intended to disregard what you wrote nor that my answer trumps yours. I was simply bringing up what I see people having a concern about and that is anything over 60 minutes and up to 2 hours, in general. Cheers. – norcal johnny Dec 11 '16 at 20:17
  • Your second solution could also suffer from drift, and if one iteration fails it could fail to fire off the next iteration. – Keith Thompson Dec 11 '16 at 20:42
2

There's no way in crontab to schedule a job to run every 61 minutes (which, BTW, is an odd thing to want to do), but you can do it indirectly.

You can schedule a job to run every minute:

* * * * * wrapper_script

where wrapper_script invokes the desired command only if the current minute is a multiple of 61, something like this:

#!/bin/bash

second=$(date +%s)
minute=$((second / 60)) 
remainder=$((minute % 61))
if [[ $remainder == 0 ]] ; then
    your_command
fi

This sets $minute to the number of minutes since the Unix epoch, 1970-01-01 00:00:00 UTC. You can adjust when the command runs by using a value other than 0 in the comparison.

That's assuming you want it to run every 61 minutes (which is what you asked). But if you want to repeat in a daily cycle, so it runs at 00:00, 01:01, ..., 23:23, and then again at 00:00 the next day, you can do it directly in crontab:

 0  0 * * * your_command 
 1  1 * * * your_command 
 2  2 * * * your_command 
# ...
21 21 * * * your_command
22 22 * * * your_command 
23 23 * * * your_command
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631