0

So I'd like to set up a cronjob in a very specific way:

  • run script_A on boot/wake

  • then every 5 minutes after, relative to that boot/wake (so not just modulo-5 clock-minutes)

  • run script_B on each of the 4 minutes in between (ie the minutes when script_A does not run)

I'm really inexperienced in this whole thing,
so I'm looking for advice on what to start digging into first...


cron itself (or maybe it's technically anacron I'm using?) seems quite limited,
and the best it can do is apparently
*/5 * * * * /home/username/script_A.sh


fcron seems a bit more powerful/flexible, including some syntax for:
@ 5 /home/username/script_A.sh
which seems to get the part "every 5 minutes starting relative to boot/wake, not just modulo clock-minutes" [... although I note stackexchange doesn't have fcron as an available tag, so that's maybe not a good sign...]


Still no clue how to handle "script_B on each of the 4 minutes in between script_A", though.


What are systemd timers like?
From a very superficial first glance, they look kinda boiler-plate-y verbose...?
But maybe they do have the power/flexibility I'm looking for...?


Is there anything else I should look into?



(
I'm on opensuse tumbleweed, so like,
I know for instance if I do go with fcron
it's gonna take a little work to make sure I've really got it installed properly
because I have to do so manually.
(
ie, I did install it from the github already,
but I'm pretty sure there's at least one or two tricky bits I need to track down to actually get it correctly integrated into my system.
)

What I do have in my package manager's repos is:
$zypper info cron cronie cronie-anacron kcron
#=>

 Loading repository data...  
 Reading installed packages...


 Information for package cron:  
 -----------------------------  
 Repository     : Main Repository (OSS)  
 Name           : cron  
 Version        : 4.2-91.3  
 Arch           : x86_64  
 Vendor         : openSUSE  
 Installed Size : 181 B  
 Installed      : Yes (automatically)  
 Status         : up-to-date  
 Source package : cronie-1.6.1-91.3.src  
 Upstream URL   : https://github.com/cronie-crond/cronie  
 Summary        : Auxiliary package  
 Description    :  
     Auxiliary package, needed for proper update from vixie-cron 4.1 to cronie 1.4.4


 Information for package cronie:  
 -------------------------------  
 Repository     : Main Repository (OSS)  
 Name           : cronie  
 Version        : 1.6.1-91.3  
 Arch           : x86_64  
 Vendor         : openSUSE  
 Installed Size : 310.2 KiB  
 Installed      : Yes (automatically)  
 Status         : up-to-date  
 Source package : cronie-1.6.1-91.3.src  
 Upstream URL   : https://github.com/cronie-crond/cronie  
 Summary        : Cron Daemon  
 Description    :  
     cron automatically starts programs at specific times. Add new entries  
     with "crontab -e". (See "man 5 crontab" and "man 1 crontab" for  
     documentation.)

     Under /etc, find the directories cron.hourly, cron.daily, cron.weekly,  
     and cron.monthly.  Scripts and programs that are located there are  
     started automatically.


 Information for package cronie-anacron:  
 ---------------------------------------  
 Repository     : Main Repository (OSS)  
 Name           : cronie-anacron  
 Version        : 1.6.1-91.3  
 Arch           : x86_64  
 Vendor         : openSUSE  
 Installed Size : 43.6 KiB  
 Installed      : Yes  
 Status         : up-to-date  
 Source package : cronie-1.6.1-91.3.src  
 Upstream URL   : https://github.com/cronie-crond/cronie  
 Summary        : Utility for running regular jobs  
 Description    :  
     Anacron becames part of cronie. Anacron is used only for running regular jobs.  
     The default settings execute regular jobs by anacron, however this could be  
     overloaded in settings.


 Information for package kcron:  
 ------------------------------  
 Repository     : Main Repository (OSS)  
 Name           : kcron  
 Version        : 23.04.3-1.1  
 Arch           : x86_64  
 Vendor         : openSUSE  
 Installed Size : 410.8 KiB  
 Installed      : No  
 Status         : not installed  
 Source package : kcron-23.04.3-1.1.src  
 Upstream URL   : https://www.kde.org  
 Summary        : Cron job configuration tool  
 Description    :  
     KCron allows you to change your cron jobs setup.  

(plus whatever might be in https://software.opensuse.org/search?baseproject=ALL&q=cron and I just don't know what to search for)
)

dwawlyn
  • 23
  • 5

1 Answers1

3

An uncommon request. But what if you instead of searching two cron expressions for "every 5 minutes" and "every other minute" you create one cron entry which runs a script every minute and the script decides if it is time for the 5-minute-script or the other. Something like:

* * * * * /home/username/script-manager.sh

#!/usr/bin/env sh
minute=$(date +%M)
if [ 0 = $(( ${minute#0} % 5 )) ]; then
  /home/username/modulo-5 clock-minutes
else
  /home/username/script_B
fi

If you insist that it has to be in 5 minutes step relative to boot you can just add something like

minute=$(date +%M)
echo "$(( ${minute#0} % 5 ))" > /run/boot-5-minute-offset

to your script which runs at boot and adapt the decider script to if [ "$(cat /run/boot-5-minute-offset)" = $(( ${minute#0} % 5 )) ]; then

  • 1
    You can avoid the (additional) boot-time job by using a temporary directory that's wiped on boot (if you have one) such as `/tmp`. If the modulo file doesn't exist then create it containing `date +%M`. Then you go off and check exactly as your current code – roaima Jul 25 '23 at 20:43
  • 1
    Yes. Another alternative is to do something with the output of `uptime`. – Paul Pazderski Jul 25 '23 at 20:57
  • @PaulPazderski Thanks, interesting! (I *did* vaguely consider putting the logic *inside* a script but stopped after the "but that would just be stuck at modulo clock-minutes" part.) I thought `uptime` just knows about time from *boot*, not anything about sleep/suspend-to-ram(/whatever it's officially called)? Or is there something that *does* know about sleep/wake(/resume or whatever it's called)? – dwawlyn Jul 25 '23 at 21:43
  • I'm not sure but I would assume `uptime` is much more than now - "start timestamp" so does not consider any sleep/hibernate. Another approach to better keep the "script pattern" is to record the number of executions in a state file. (i.e. on each execution, read count, if modulo 5 do script a else script b, write count+1 in state file, repeat next minute) Might better keep the cycle if sleep is involved. – Paul Pazderski Jul 25 '23 at 22:03