Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 802  / 3 Years ago, mon, july 5, 2021, 7:38:35

I have in my crontab the following


15 */4 * * * ...


My original intent was that I would run ... every 4 hours from when the computer boots up. If I power on at 8 am, it would run at 8, 12, 16, 20, ...; if I power on at 9am it would run at 9, 13, 17, 21, ...


My observation is that crontab seems to divide the hour by 4 and if the remainder is 0 it executes.


Is there a way I can achieve the former rather than the latter?


More From » cron

 Answers
1

This is something that might more easily be done on current Ubuntu systems using a systemd timer.


Following the monotonic timer example in the archlinux wiki:



  1. create a service unit file to run your command (/usr/local/bin/foo in this example) as a oneshot service


    [Unit]
    Description=do some stuff on schedule defined by foo.timer

    [Service]
    Type=oneshot
    ExecStart=/usr/local/bin/foo


  2. create a corresponding timer unit with the desired time specifications (0 seconds after boot, and then 4 hours after the service unit was last activated)


    [Unit]
    Description=Run foo on boot and every 4 hours thereafter

    [Timer]
    OnBootSec=0
    OnUnitActiveSec=4h

    [Install]
    WantedBy=timers.target


  3. enable the timer


    sudo systemctl enable foo.timer



After reboot, you should be able to see the new timer along with its last and next activation times:


$ systemctl list-timers foo
NEXT LEFT LAST PASSED UNIT ACTIVATES
Thu 2023-01-12 19:37:39 EST 3h 58min left Thu 2023-01-12 15:37:39 EST 1min 5s ago foo.timer foo.service

1 timers listed.
Pass --all to see loaded but inactive timers, too.

[#74] Wednesday, July 7, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gigglcept

Total Points: 113
Total Questions: 105
Total Answers: 125

Location: Christmas Island
Member since Wed, Jan 13, 2021
3 Years ago
gigglcept questions
Thu, May 27, 21, 13:35, 3 Years ago
Tue, Feb 8, 22, 09:59, 2 Years ago
Fri, Dec 16, 22, 12:32, 1 Year ago
Sun, Oct 16, 22, 13:09, 2 Years ago
;