4

I'm trying to run a bash script 10 minutes after my system startup and on every reboot. I was planning to the @reboot of crontab, but I'm not sure of two things

  • Whether it will run on the first system start or only on reboot.
  • How to delay the run by 10 minutes after the reboot.

What expression would suit my situation the best? Please note that I can't run 'at' or system timer to accomplish this as both are not accessible to us. I'm working on the RHEL 7..

tripleee
  • 175,061
  • 34
  • 275
  • 318

3 Answers3

5

I would just sleep 600 at the beginning of your reboot script. Sure, there's probably a more "expert" way of doing it, but it'll work.

user3243135
  • 800
  • 1
  • 7
  • 28
  • That's one hour, though, 10 minutes is 600 seconds. But +1; that's the way to do it. – tripleee Jan 08 '18 at 20:31
  • oh ya.. but will @reboot trigger the script the first startup also? – Naveen Balasubramanian Jan 08 '18 at 20:32
  • If you hve managed to install a @reboot entry, the first ever boot has already happened. Cron doesn't really care anyway; "reboot" is technically slightly misleading, in that it really will run after the very first boot, too (cron has no way to find out if the latest boot was a *re*-boot). – tripleee Jan 08 '18 at 20:34
  • 2
    Whoops ^_^ believe it or not I have a math degree. – user3243135 Jan 08 '18 at 20:53
4

I think your question may be more appropriate on the Unix and Linux stack exchange, because I found two answers over there which directly address your question:

https://unix.stackexchange.com/questions/57852/crontab-job-start-1-min-after-reboot

Basically you can always just add sleep 600 to the beginning of your cronjob invocation.

As to whether you should be running a cronjob vs an init script:

https://unix.stackexchange.com/questions/188042/running-a-script-during-booting-startup-init-d-vs-cron-reboot

There are a handful of subtle differences, but basically, your cron @reboot will run each time the system starts and may be more easy to manage as a non-root user.

JawguyChooser
  • 1,816
  • 1
  • 18
  • 32
0

rc-local.service would be better for your needs on a EL7 system.

  systemctl status rc-local.service
  ● rc-local.service - /etc/rc.d/rc.local Compatibility
    Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled)
   Active: inactive (dead)

You need to put your script that can run with any amount of delay inside the file, /etc/rc.d/rc.local, e.g.,

sleep 600 && /usr/local/bin/myscript.sh

OR you can put delay inside the script.

# Give exe permission to the local script as well as `rc.local`
chmod a+x /usr/local/bin/myscript.sh
chmod a+x /etc/rc.d/rc.local

# Enable the service. Note the service name has a `-` compared `.` in the file.
systemctl enable rc-local.service
iamauser
  • 11,119
  • 5
  • 34
  • 52