88

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?

pzkpfw
  • 5,632
user128712
  • 2,372
  • Auto-start programs are configured in autostart manifests or in *.service files in several locations, as well as in init.d or crontab. See this: https://unix.stackexchange.com/a/525845/43233 – Noam Manos Jun 20 '19 at 06:30

8 Answers8

64

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 S99myscript.

ln -s /etc/init.d/myscript /etc/rc3.d/S99myscript

Each backward compatible /etc/rc*.d directory has symbolic links to the /etc/init.d/ directory.

Radu Rădeanu
  • 169,590
  • 2
    Note that under Ubuntu Server 14.04, I needed to use /etc/rc2.d, instead of rc3.d as indicated in the example. It seems rc2.d isn't just the graphical run level. – Brad Dec 24 '14 at 03:52
  • 1
    cd /etc/rc\cat /etc/init/rc-sysinit.conf | grep "env DEFAULT_RUNLEVEL" | grep -oP "\d"`.dwill take you to which/etc/rc?.d`directory corresponds to your default runlevel. – Kevin Sep 12 '16 at 04:04
  • 1
    Instead: " If the last script to be run is rc.local and it is named S99rc.local then you need to add your script as S99myscript. ". Did you mean: " If the last script to be run is myscript and it is named S99myscript then you need to add your script as S99myscript. " ??? – Dor Oct 05 '16 at 07:55
  • 11
    Keep in mind that this won't work with systemd (i.e., starting with Ubuntu 16.04). See this instead. – Alexander Revo Nov 24 '16 at 10:31
  • There is no S*rc.local file exist – alper Jul 04 '21 at 09:42
40

Use a crontab option to make your script run after reboot,

You can do this by adding your command prefixed with the @reboot nonstandard predefined scheduling definition in cron.

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:

@reboot path/to/your/executable/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.

hs.chandra
  • 567
  • 3
  • 11
  • Did you mean using command crontab -e, which does not need superuser privileges? I wonder, if it can run scripts that need to be run as root. – jarno Dec 27 '15 at 12:26
  • I tested this. It does not run the script as root. – jarno Jan 11 '16 at 13:43
  • 6
    You have to add the line while running command sudo crontab -e to make it run the script as root during startup. – jarno Apr 14 '16 at 14:12
  • don't forget to remove /var/run/crond.reboot, if not, won't execute the task – Albert Català May 20 '16 at 10:46
  • Only for the record, I'm sure that @jarno will know it already. Each user has his own cron task, so if you add it to your user's one (running crontab -e without sudo) the script will be runned by your user. So you need to run it as root to let it be able to run scripts as root on boot. – eddieferetro Jan 29 '19 at 08:35
  • 3
    no need to remove /var/run/crond.reboot anymore, this sees to be fixed long ago – rubo77 Oct 19 '19 at 22:33
  • 2
    This worked on Ubuntu 20, no need to remove /var/run/crond.reboot, could run sudo -i -u myuser bash -c '...', and is much simpler than creating a systemd script. – Dan Dascalescu Sep 12 '20 at 20:35
30

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".

thefourtheye
  • 4,924
  • 3
    Note: you have to put the command before the last line in /etc/rc.local that contains:

    exit 0

    – rubo77 Oct 14 '14 at 20:02
  • 3
    /etc/rc.local in ubuntu 15.10 has in comment that "This script is executed at the end of each multiuser runlevel." So the script could be run several times during boot, right? – jarno Dec 27 '15 at 13:33
  • @jamo, yes, on each runlevel. I don't knwo how this is related to run "at boot", which usually mean "run once at boot". – m3nda May 22 '17 at 22:07
  • I do not have this file on debian buster – Dimitri Kopriwa Sep 16 '20 at 00:53
20

This worked for me on Ubuntu 17.04:

  1. create a script file like disable_cdrom in convenient for you location. In my case home/yterle/disable_cdrom. In my case:

    #!/bin/sh
    eject /dev/sr0 -i 1
    
  2. make it executable chmod 775 disable_cdrom

  3. navigate to /etc/systemd/system and create there a service file. For example sudo gedit /etc/systemd/system/disable_cdrom.service

My 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

Where ExecStart points to run your script with /bin/sh

Then run systemctl enable disable_cdrom.service to enable the systemd service

YTerle
  • 301
  • 2
  • 5
5

Include your Script file to /etc/init.d/ with Executable permission then set different run level

$ update-rc.d script-name default

It will put your script on boot startup.

M S Parmar
  • 293
  • 1
  • 4
  • 9
4

Create a text file like this in /etc/cron.d/:

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.)

muru
  • 197,895
  • 55
  • 485
  • 740
jarno
  • 5,600
3

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
  • Ctrl+Alt+F#

nothing works.

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.

zx485
  • 2,426
GChuf
  • 495
2

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