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
crontab option to make your script run after reboot,
Open crontab as the root user (you must use
sudo here in order to edit your root's crontab instead of your user's crontab):
sudo crontab -e
Then, add a record to the bottom, containing your desired command to be run as root:
That will do what you want.
Note that you can see your user's and root's crontab entries as follows:
crontab -l # list your user crontab entries sudo crontab -l # list root's crontab entries
For your command to run as root, it must be in your root's crontab.
This worked for me on Ubuntu 17.04:
create a 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 disable_cdrom.service to enable the systemd service
Careful with adding the script to
rc.local - I was stuck in splash screen because of it. Pressing Alt+F1 revealed what was going on behind the splash screen (the script in
rc.local was running).
I could not get out of it.
- Ctrl+Alt+Del or
- Alt+PrtSc+K or
I had to boot from a USB Ubuntu image, find
rc.local, give myself permissions to file and delete it. I guess you shouldn't do stuff if you don't know what you're doing.
You can create a service under systemd. That way your script will run as sudo automatically.
Assuming you have a script
/path/to/script/my_amazing_script.sh that you want to run at ever boot as root:
Make sure only root can work with this:
chmod 0700 /path/to/script/my_amazing_script.sh
Then create the service file and start editing it:
sudo nano /etc/systemd/system/run_amazing_script.service
Put this in the file:
[Unit] Description=Run the best script ever [Service] ExecStart=/path/to/script/my_amazing_script.sh [Install] WantedBy=multi-user.target
Check the service runs fine by running it one time:
sudo systemctl start run_amazing_script.service
Check the log:
sudo journalctl -u run_amazing_script.service
If you are happy with the run, enable it to make it run upon every boot:
sudo systemctl enable run_amazing_script.service
And if you want to stop it from running at every boot you can disable it:
sudo systemctl disable run_amazing_script.service
Note: this solution is very similar to what YTerle posted. But since I don't have enough rep I couldn't comment nor upvote his answer