crontab

crontab(5):

Instead of the first five fields, one of eight special strings may appear:

    stringmeaning
    @reboot Run once, at startup.

Me: Hmm, I wonder if that actually works:

% crontab -l
@reboot   echo REBOOT

crond:

Cron Daemon     Cron <root> echo REBOOT     8:46:54 AM
Cron Daemon     Cron <root> echo REBOOT     8:47:24 AM
Cron Daemon     Cron <root> echo REBOOT     8:48:35 AM
Cron Daemon     Cron <root> echo REBOOT     8:48:37 AM
Cron Daemon     Cron <root> echo REBOOT     8:48:48 AM
Cron Daemon     Cron <root> echo REBOOT     8:48:58 AM
Cron Daemon     Cron <root> echo REBOOT     8:49:59 AM
Cron Daemon     Cron <root> echo REBOOT     8:49:59 AM
Cron Daemon     Cron <root> echo REBOOT     8:49:59 AM
Cron Daemon     Cron <root> echo REBOOT     8:50:20 AM
Cron Daemon     Cron <root> echo REBOOT     8:50:31 AM
Cron Daemon     Cron <root> echo REBOOT     8:54:06 AM
Cron Daemon     Cron <root> echo REBOOT     8:54:06 AM
Cron Daemon     Cron <root> echo REBOOT     8:54:06 AM
Cron Daemon     Cron <root> echo REBOOT     8:54:06 AM
Cron Daemon     Cron <root> echo REBOOT     8:54:26 AM
Cron Daemon     Cron <root> echo REBOOT     8:54:39 AM
Cron Daemon     Cron <root> echo REBOOT     8:55:22 AM
Cron Daemon     Cron <root> echo REBOOT     8:56:11 AM
Cron Daemon     Cron <root> echo REBOOT     8:56:50 AM
Cron Daemon     Cron <root> echo REBOOT     8:57:46 AM
Cron Daemon     Cron <root> echo REBOOT     8:58:32 AM
Cron Daemon     Cron <root> echo REBOOT     8:59:16 AM
Cron Daemon     Cron <root> echo REBOOT     9:00:03 AM
Cron Daemon     Cron <root> echo REBOOT     9:00:55 AM
Cron Daemon     Cron <root> echo REBOOT     9:02:03 AM
Cron Daemon     Cron <root> echo REBOOT     9:02:42 AM
Cron Daemon     Cron <root> echo REBOOT     9:03:35 AM
Cron Daemon     Cron <root> echo REBOOT     9:04:27 AM
Cron Daemon     Cron <root> echo REBOOT     9:05:13 AM
Cron Daemon     Cron <root> echo REBOOT     9:06:05 AM
Cron Daemon     Cron <root> echo REBOOT     9:06:48 AM
Cron Daemon     Cron <root> echo REBOOT     9:07:35 AM
Cron Daemon     Cron <root> echo REBOOT     9:08:23 AM
Cron Daemon     Cron <root> echo REBOOT     9:09:07 AM
Cron Daemon     Cron <root> echo REBOOT     9:09:52 AM
Cron Daemon     Cron <root> echo REBOOT     9:10:35 AM
Cron Daemon     Cron <root> echo REBOOT     9:11:28 AM
Cron Daemon     Cron <root> echo REBOOT     9:12:20 AM
Cron Daemon     Cron <root> echo REBOOT     9:13:17 AM
Cron Daemon     Cron <root> echo REBOOT     9:14:00 AM
Cron Daemon     Cron <root> echo REBOOT     9:15:00 AM
Cron Daemon     Cron <root> echo REBOOT     9:15:49 AM
Cron Daemon     Cron <root> echo REBOOT     9:16:40 AM
Cron Daemon     Cron <root> echo REBOOT     9:17:28 AM
Cron Daemon     Cron <root> echo REBOOT     9:18:21 AM
Cron Daemon     Cron <root> echo REBOOT     9:19:08 AM
Cron Daemon     Cron <root> echo REBOOT     9:19:54 AM
Cron Daemon     Cron <root> echo REBOOT     9:20:37 AM
Cron Daemon     Cron <root> echo REBOOT     9:21:36 AM
Cron Daemon     Cron <root> echo REBOOT     9:22:32 AM
Cron Daemon     Cron <root> echo REBOOT     9:23:01 AM
Cron Daemon     Cron <root> echo REBOOT     9:23:54 AM
Cron Daemon     Cron <root> echo REBOOT     9:24:38 AM
Cron Daemon     Cron <root> echo REBOOT     9:25:18 AM
Cron Daemon     Cron <root> echo REBOOT     9:26:11 AM
Cron Daemon     Cron <root> echo REBOOT     9:27:00 AM
Cron Daemon     Cron <root> echo REBOOT     9:27:52 AM
Cron Daemon     Cron <root> echo REBOOT     9:28:43 AM
Cron Daemon     Cron <root> echo REBOOT     9:29:32 AM
Cron Daemon     Cron <root> echo REBOOT     9:30:29 AM
Cron Daemon     Cron <root> echo REBOOT     9:31:18 AM
Cron Daemon     Cron <root> echo REBOOT     9:32:18 AM
Cron Daemon     Cron <root> echo REBOOT     9:33:13 AM
Cron Daemon     Cron <root> echo REBOOT     9:33:34 AM

% uptime
10:43 up 1:09, 6 users, load averages: 5.57 5.49 5.42


Update: It happened again to one of my other machines! Check this shit out! I have no idea why this machine rebooted. It was not a power failure, and system.log provides no clues. This time the cron job was "date; uptime". Several hours after this happened, it reported:

% date; uptime
Wed Mar 16 09:39:03 PDT 2022
9:39 up 6:25, 3 users, load averages: 2.41 2.35 2.33
...which means it booted up at 3:14 AM. And this is the firehose of email that was generated:

Wed Mar 16 02:58:09 PDT 2022   2:58 up 28 days, 1:33, 2 users, load averages: 19.27 6.28 3.99
Wed Mar 16 02:58:09 PDT 2022   2:58 up 28 days, 1:33, 2 users, load averages: 19.27 6.28 3.99
Wed Mar 16 02:59:43 PDT 2022   2:59 up 28 days, 1:34, 2 users, load averages: 129.46 54.89 23.51
Wed Mar 16 03:01:53 PDT 2022   3:01 up 28 days, 1:36, 2 users, load averages: 153.98 98.37 45.77
Wed Mar 16 03:02:58 PDT 2022   3:02 up 28 days, 1:37, 2 users, load averages: 98.98 93.57 47.87
Wed Mar 16 03:03:50 PDT 2022   3:03 up 28 days, 1:38, 2 users, load averages: 154.26 108.06 56.02
Wed Mar 16 03:04:02 PDT 2022   3:04 up 28 days, 1:38, 2 users, load averages: 189.32 118.11 60.52
Wed Mar 16 03:04:15 PDT 2022   3:04 up 28 days, 1:39, 2 users, load averages: 204.15 123.68 63.17
Wed Mar 16 03:04:20 PDT 2022   3:04 up 28 days, 1:39, 2 users, load averages: 202.53 124.68 63.88
Wed Mar 16 03:04:32 PDT 2022   3:04 up 28 days, 1:39, 2 users, load averages: 195.38 125.66 64.93
Wed Mar 16 03:04:40 PDT 2022   3:04 up 28 days, 1:39, 2 users, load averages: 186.21 126.01 65.76
Wed Mar 16 03:04:52 PDT 2022   3:04 up 28 days, 1:39, 2 users, load averages: 158.03 121.96 65.03
Wed Mar 16 03:05:02 PDT 2022   3:05 up 28 days, 1:39, 2 users, load averages: 138.29 118.89 64.60
Wed Mar 16 03:05:12 PDT 2022   3:05 up 28 days, 1:40, 2 users, load averages: 117.26 115.02 63.87
Wed Mar 16 03:05:56 PDT 2022   3:05 up 28 days, 1:40, 2 users, load averages: 137.33 120.70 68.60
Wed Mar 16 03:06:05 PDT 2022   3:06 up 28 days, 1:41, 2 users, load averages: 152.45 124.53 70.57
Wed Mar 16 03:06:20 PDT 2022   3:06 up 28 days, 1:41, 2 users, load averages: 165.60 128.75 73.02
Wed Mar 16 03:06:27 PDT 2022   3:06 up 28 days, 1:41, 2 users, load averages: 163.87 129.00 73.43
Wed Mar 16 03:06:36 PDT 2022   3:06 up 28 days, 1:41, 2 users, load averages: 149.93 127.09 73.40
Wed Mar 16 03:06:36 PDT 2022   3:06 up 28 days, 1:41, 2 users, load averages: 149.93 127.09 73.40
Wed Mar 16 03:06:56 PDT 2022   3:06 up 28 days, 1:41, 2 users, load averages: 129.17 123.51 73.34
Wed Mar 16 03:07:56 PDT 2022   3:07 up 28 days, 1:42, 2 users, load averages: 137.78 125.27 77.33
Wed Mar 16 03:08:24 PDT 2022   3:08 up 28 days, 1:43, 2 users, load averages: 165.72 133.16 81.59
Wed Mar 16 03:08:44 PDT 2022   3:08 up 28 days, 1:43, 2 users, load averages: 153.51 132.50 82.82
Wed Mar 16 03:10:08 PDT 2022   3:10 up 28 days, 1:45, 2 users, load averages: 134.03 132.19 87.21
Wed Mar 16 03:10:08 PDT 2022   3:10 up 28 days, 1:45, 2 users, load averages: 134.03 132.19 87.21
Wed Mar 16 03:10:08 PDT 2022   3:10 up 28 days, 1:45, 2 users, load averages: 134.03 132.19 87.21
Wed Mar 16 03:10:38 PDT 2022   3:10 up 28 days, 1:45, 2 users, load averages: 103.43 125.11 86.21
Wed Mar 16 03:10:48 PDT 2022   3:10 up 28 days, 1:45, 1 user, load averages: 87.91 121.07 85.23
Wed Mar 16 03:11:16 PDT 2022   3:11 up 28 days, 1:46, 1 user, load averages: 64.13 112.71 83.25
Wed Mar 16 03:11:37 PDT 2022   3:11 up 28 days, 1:46, 1 user, load averages: 51.50 105.89 81.64
Wed Mar 16 03:11:49 PDT 2022   3:11 up 10 secs, 0 users, load averages: 2.62 0.56 0.20
Wed Mar 16 03:12:19 PDT 2022   3:12 up 28 days, 1:47, 1 user, load averages: 29.12 93.42 78.19
Wed Mar 16 03:12:19 PDT 2022   3:12 up 28 days, 1:47, 1 user, load averages: 29.12 93.42 78.19
Wed Mar 16 03:12:41 PDT 2022   3:12 up 28 days, 1:47, 1 user, load averages: 23.51 87.95 76.58
Wed Mar 16 03:13:13 PDT 2022   3:13 up 28 days, 1:48, 1 user, load averages: 23.12 80.66 74.38
Wed Mar 16 03:13:35 PDT 2022   3:13 up 28 days, 1:48, 1 user, load averages: 17.13 75.56 72.70

So my "@reboot" job ran 37 times over the course of 8½ minutes BEFORE the machine rebooted, and then zero times after.

That's... super helpful. really just super helpful.


Previously, previously, previously, previously.

Tags: , , , ,

16 Responses:

  1. phuzz says:

    Just to confirm, this is on OSX?

  2. someguy says:

    Any chance the cron daemon is restarting for some reason? Depending on where Apple actually got their crontab from, it may not have any tracking of actual "reboots" other than an assumption that crond runs on startup and persists from then onwards.

    Possibly unrelated cron-reboot anecdote: Linux crond does attempt to track reboots via a lockfile, but at least on Debian/Ubuntu erasure of the reboot-tracking lockfile between boots was never explicitly performed and only assumed, because it was supposed to be stored on a tmpfs in /var somewhere. So when that filesystem is persistent, Cron never thinks it's rebooted.

  3. Michael Sternberg says:

    The logs are for entries that ran or were scanned?

    FWIW, this works in Mojave:

    @reboot /bin/date +"%c REBOOT" >> /tmp/cron.log 2>&1
    * * * * * /bin/date +"%c other" >> /tmp/cron.log 2>&1

    … Gives:

    # last -1 reboot
    reboot ~ Mon Mar 14 13:43
    # cat /tmp/cron.log
    Mon Mar 14 13:44:17 2022 REBOOT
    Mon Mar 14 13:45:00 2022 other
    Mon Mar 14 13:46:00 2022 other

    • jwz says:

      I would not have gotten 65 distinct emails from one reboot if it wasn't executing the job 65 times.

      • Michael Sternberg says:

        Sure is annoying and proof enough. But we couldn't know that, could we?

    • Michael G. Sternberg says:

      Correction: This is the crontab as it ran (with backslashes):
      @reboot /bin/date +"%c REBOOT" >> /tmp/cron.log 2>&1
      * * * * * /bin/date +"%c other" >> /tmp/cron.log 2>&1

  4. jwz says:

    Also note that it stopped about 47 minutes after the reboot. Why that duration? Why didn't it continue forever? We may never know.

    • Michael Sternberg says:

      From what uptime said, the reboot time was ≈9:34, a good time to run that cron job (ideally) once. The question is in what kind of delirium crond was before that, and if that instance or the rebooted one generated the 9:33:34 entry. (I hear M1 Macs boot quickly.)

      • jwz says:

        Yeah, I typed reboot at 9:34, and prior to that uptime was 26 days.

        It does not boot particularly fast, being a Macmini7,1 10.15.7, though with SSD.

      • jwz says:

        So maybe none of those emails were from this reboot but were from the previous instance? But all the emails came in after the reboot. So now I'm suspecting DST fuckery, but all the emails seemed to be labelled PDT, and all of the machines involved in the mail path know what time it is.

  5. Carlos says:

    You always find the most delightfully fucked-up corners of the behaviour space for our elucidation. Thanks, it's almost like I've found my soulmate. My wife thinks it's only me that has this experience.

    C.

  6. Jonathan says:

    I no longer trust MacOS's cron; I believe they put the effort into launchd, which actually starts cron according to cron's man page. My bet would be they've goofed up launchd so that it keeps scanning for modified crontab files and running the reboot action.

    I use launchd tasks to run things at login which works for my usage but have not personally had to run something on reboot only. Google claims you can have plist files that have the RunAtLoad Key to accomplish the @reboot equivalent though, maybe with a LaunchOnlyOnce Key too.

    I also dislike dealing with the goofy plist files so I end up using Launchd Task Scheduler to get 'job' details right, but that's just laziness. Presumably moving a plist file into /Library/LaunchDaemons with the right keys would accomplish the desired behavior in the convoluted reinvention of cron way.

    • jwz says:

      Launchd plist files and systemd service files are fine for what they do (giving you options about daemon restarts, log files, users, etc.) but having to write a 30 line script just to run some dumb thing once an hour is stupid.

  7. nooj says:

    That update is fucked up!

    What's with that one guy at 03:11:49 reporting from the future??

    And those load averages are bananas. Looks like someone decided to do a bunch of shit all at once. I wonder what it would show if you added a "top -l1" or similar to the cron. (Maybe that would be too much compute on an already loaded machine and it would explode that much sooner.)

  • Previously