How do I start a Cron job 1 min after @reboot?

I need my script to be executed a minute after each reboot. When I apply @reboot in my crontab it is too early for my script – I want the script to be executed after all other tasks that are routinely run on reboot. How might I run the script sometime after reboot?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Is the script only ever intended to run one minute after boot up, or can it be used at other times, too? In the former case, you can add sleep 60 to the beginning of your script, or in the latter case, add it to the crontab file:

@reboot sleep 60 && my_script.sh

As has been pointed out by sr_, though, perhaps you are tackling this in the wrong way, and a proper init.d or rc.d script would be a more robust solution.

Method 2

If you need to execute something after reboot when network will become available, for example, you can write systemd unit that will be executed at required time (of course this will work only on systems with systemd).

To do so create file /etc/systemd/system/my_script.service with following contents:

[Unit]
Description=My script that requires network
After=network.target

[Service]
Type=oneshot
ExecStart=/full/path/to/my_script.sh

[Install]
WantedBy=multi-user.target

Then execute:

sudo systemctl daemon-reload
sudo systemctl enable my_script

You’re done!

Method 3

I would use at. As in:

@reboot echo /root/bin/do_the_stuff | at now + 2 minutes
# at assigns it an execution time truncated to whole minutes,
# so this means it will execute in 1--2 minutes.

… with the added mentioned caveat that if what you really want is to run it after all other things, you should check how to do that in the init that your OS is using.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x