I am currently trying to understand the difference between init.d
and cron @reboot
for running a script at startup/booting of the system.
The use of @reboot
(this method was mentioned in this forum by hs.chandra) is some what simpler, by simply going into crontab -e
and creating a @reboot /some_directory/to_your/script/your_script.txt
and then your_script.txt
shall be executed every time the system is rebooted. An in depth explanation of @reboot
is here
Alternatively by embedding /etc/init.d/your_script.txt
into the second line of your script ie:
#!/bin/bash
# /etc/init.d/your_script.txt
You can run chmod +x /etc/init.d/your_script.txt
and that should also result for your_script.txt
to run every time the system is booted.
Q1: What are the key differences between the two?
Q2: Which is more robust?
Q3: Is there a better one out of the two?
Q4: Is this the correct way of embedding a script to run during booting?
I will be incorporating a bash .sh file to run during startup.
Best Answer
init.d
, also known as SysV script, is meant to start and stop services during system initialization and shutdown. (/etc/init.d/
scripts are also run on systemd enabled systems for compatibility).start
andstop
and more (see Debian policy)crontab
(and therefore@reboot
).@reboot
script (not just root)multi-user.target
./etc/rc.local
is often considered to be ugly or deprecated (at least by redhat), still it had some nice features:rc.local
was (almost) the last service to start.rc.local
is executed afternetwork.target
by default (notnetwork-online.target
!)Regarding systemd's
network.target
andnetwork-online.target
, read Running Services After the Network is up.