89

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?

5
  • 4
    Why is it too early? Maybe an init script would be more suitable (by regarding dependencies, e.g. this script needs networking to be set up first)?
    – sr_
    Dec 7, 2012 at 10:11
  • my script need be executed after all others, the last in short words
    – Yurij73
    Dec 7, 2012 at 10:22
  • since your job needs to be executed after everything and since the concept of everything is quite variable (addition of a new autostart@reboot application is not so uncommon), I think it is up to you to place your job into the rc scripts in a way that servers your purpose. Putting your task at the bottom of tasks specified in /etc/rc.local is usually what you want, but depending on your system's startup configuration, this might not hold true all the time. So, test it and see if it works for you.
    – MelBurslan
    Dec 7, 2012 at 15:49
  • Sounds like a case for Upstart?
    – nafg
    Nov 27, 2013 at 10:29
  • 2
    Just had a similar situation where I had to use two @reboot lines because the first command "took over", although maybe a single & could resolve this. Anyway, this worked for me, e.g.: @reboot mongod --port 27017 /var/lib/mongodb and @reboot sleep 60 && /use/bin/node /home/me/server.js. Had to use the sleep to give mongo a chance to boot up. Maybe there's a better way but this works for now.
    – user208787
    Feb 8, 2021 at 13:04

3 Answers 3

125

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.

5
  • 1
    Or use a command line option to tweak the delay (e.g. script -s X which would translate to sleep X inside of the script).
    – peterph
    Dec 7, 2012 at 10:24
  • Can I apply this to the file taskrunner in /etc/init.d/ ? Apr 19, 2018 at 19:32
  • @AndresAngel - I'm sorry, I don't know. I use FreeBSD which doesn't have taskrunner, nor, for that matter, /etc/init.d
    – D_Bye
    Apr 23, 2018 at 11:38
  • on my rhel7 sleep doesn't work at all, it sleeps forever & never comes back. Jun 4, 2018 at 14:02
  • In my Ubuntu sleep in cron delays the whole boot and the terminal appearance
    – meir
    Jan 30 at 8:23
24

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!

3
  • 2
    ...for people who don't hate systemd :) Dec 7, 2021 at 7:06
  • but it is everywhere, whether you hate it or not ;-)
    – Envek
    Jan 31, 2022 at 20:08
  • It's not on the box I write this comment on right now. There's this Debian derivative called "Devuan". ;-) Mar 3, 2022 at 22:12
18

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .