I had a script that automatically enables my wifi without using networkmanager, but I don't know how to run the script as root while the system is booting. How do I make the script run automatically during boot?
Place the script you want to run in the /etc/init.d directory and make the script executable.
chmod 755 myscript
Once that is done create a symbolic link in the run level directory you would like to use, for example if you wanted to run a program in the graphical runlevel 2, the default runlevel for Ubuntu, you would place it in the
/etc/rc2.d directory. You just cannot place it the directory, you must signify when it will run by indicating the startup with an “S” and the execution order is important. Place it after everything else that is in the directory by giving it a higher number.
If the last script to be run is
rc.local and it is named
S99rc.local then you need to add your script as
ln -s /etc/init.d/myscript /etc/rc3.d/S99myscript
Each backward compatible
/etc/rc*.d directory has symbolic links to the
Include the command in
/etc/rc.local. It will be run whenever the user's runlevel changes.
Note: You have to put the command before the last line in /etc/rc.local that contains: "exit 0".
crontab option to make your script run after reboot,
you can do it by adding
@reboot code in cron
will do it
Include your Script file to
with Executable permission
then set different run level
$ update-rc.d script-name default
It will put your script on boot startup.
This worked for me on Ubuntu 17.04:
create tour script file like
disable_cdromin convenient for you location. In my case
home/yterle/disable_cdrom. In my case:
#!/bin/sh eject /dev/sr0 -i 1
make it executable
chmod 775 disable_cdrom
- navigate to
/etc/systemd/systemand create there a
servicefile. For example
sudo gedit /etc/systemd/system/disable_cdrom.service
disable_cdrom.service looks like this:
[Unit] Description=Disable cdrom [Service] Type=oneshot ExecStart=/bin/sh /home/yterle/disable_cdrom [Install] WantedBy=multi-user.target
ExecStart points to run your script with
systemctl enable vgaoff.service to enable the systemd service
Create a text file like this in
SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin @reboot root yourScriptPath
(You should replace
yourScriptPath with the path to the script you want to run.)