Two Cronjobs at Reboot

To reboot, it’s a “shut down and boot” sequence

When I click ‘reboot’, I want to run 2 cronjob’s.
A cleanup script before the shut down and an update script after the boot up.

What are the crontab entries to accomplish this?

As mentioned here…
https://tecadmin.net/setting-up-startup-and-shutdown-scripts-in-ubuntu/
I could use the init.d and rc0.d directories in /etc
but that’s not gonna be easy to turn off just by adding a ‘#’ at the beginning of a line.

I understand there are some risks with this, but I’m talking about a normal reboot.

Cronjobs are meant to be run at intervals, not on a certain trigger like a reboot.

What you want is a systemd service.

3 Likes

After looking at that link…
Looks like a whole learning process.
My current knowledge level isn’t ready to proceed into the custom systemd service level.
This is something in my future, but it seems a little too advanced for me currently.

That’s not correct. From man 5 crontab

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

              string         meaning
              ------         -------
              @reboot        Run once, at startup.
              @yearly        Run once a year, "0 0 1 1 *".
              @annually      (same as @yearly)
              @monthly       Run once a month, "0 0 1 * *".
              @weekly        Run once a week, "0 0 * * 0".
              @daily         Run once a day, "0 0 * * *".
              @midnight      (same as @daily)
              @hourly        Run once an hour, "0 * * * *".

       Please  note  that  startup, as far as @reboot is concerned, is the time when the cron(8) daemon startup.  In particular, it may be before
       some system daemons, or other facilities, were startup.  This is due to the boot order sequence of the machine.

Also: CronHowto - Community Help Wiki

2 Likes

@eeickmeyer and @popey You’re both incorrect.

Well, no. That’s wrong, too. You’re both actually correct…but imprecise this one time.

  • Cron does indeed have the @reboot feature.
  • However, the OP has specified they want some jobs to run at shutdown before the reboot occurs. cron @reboot is not capable of that.

@WyattWhiteEagle you are asking cron to do something that it’s not designed to do. Doing jobs at shutdown is tricky, because they mustn’t be critical – maybe you lose power or have some other “unclean” shutdown. A missed job shouldn’t lead to failed boot or lost data. And if they are not critical, why can’t they be done at startup instead?

Your choice is to alter your requirement (do job at startup instead of shutdown), OR to use a different tool that is capable of doing the job (like systemd). It doesn’t matter to us which you choose.

If you want more detailed advice, you might need to share a bit more about what you want to accomplish.

2 Likes

I wasn’t answering op’s question about shutdown jobs. Merely pointing out a minor incorrectness in that cron can and has for many years been able to do something after on reboot, that’s all.

When you get around to learning more about this, here is an article that might be easier to digest.

3 Likes

Thank you for that.
Indeed it does seem a bit easier to follow and understand.
More like it’s complimentary or a summarized version of the first link I was provided with.

I’ve used this past couple days to give this topic some thought to try and get my thoughts straight about it.

Seems @ian-weisser is correct about me giving more details about what I want to accomplish.

The part about running cronjobs at intervals, I can understand why that was mentioned.
Running a cronjob at shut down isn’t a good idea, just as @ian-weisser pointed out.
I’m guessing that’s where the systemd service comes in, especially when the shut down is concerned.

I’m spending most of the time trying to maintain my machine.
So one of my goals is to open time for what I got the machine for…
Goal: To do more in family and homestead upkeep.
There is some constructive hobby entertainment as well.
(I use the machine to help keep my thoughts and routines at least somewhat consistent)

What I want to accomplish…
When I first get to the machine in my mornings, I always manually reboot, first thing.
This reboot isn’t consistent as to when I do the reboot.
The machine is running the rest of the time unless something happens that requires me to reboot more than once.

Sometime before shut down…
Send logs, crash reports if any, and at least the most recent system snapshot to somewhere off the machine.

Within an hour before shut down…
Cleanup the system.

After the bootup…
Run updates and upgrades

This will allow me to spend 2-4 hours instead of 8 to 10 hours every day rectifying errors.
Those hours include testing to verify the errors have been rectified.

The current cronjob does all this at 4:03am every day except the system snapshot.
I just need to find out how to include the most recent snapshot and how to seperate what all is done on which side of the reboot.

Is this correct?
According to the manpage,

In crontab,
instead of doing this…
0 * * * * /path/to/script

I can do something like this…
@reboot /path/to/script

Manpages are rarely wrong.

I wasn’t doubting the manpages,
I was making sure I understood it properly.

I misunderstood before and had ill results.
Thus, my need for a VM setup. (got another thread goin’ for that.)
(right now, I’m working on someone else’s comp)

Yes. Typical crontab syntax indicates a particular set of time-based conditions. @reboot is no different, but it’s special.

Read the fine print: this is whenever the cron dæmon starts. So if you restart it a lot, that script will be kicked off every time you do.

o ok
Thank you for that info

This topic was automatically closed after 29 days. New replies are no longer allowed.