94

I would like to run a sudo command when Ubuntu starts up (before anyone logs in):

sudo searchd

How would I do this?

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
Mark Willis
  • 1,711
  • 1
  • 14
  • 23

3 Answers3

196

You can add the command in the /etc/rc.local script that is executed at the end of startup.

Write the command before exit 0. Anything written after exit 0 will never be executed.

Nabin
  • 11,216
  • 8
  • 63
  • 98
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • 38
    Before, for sure. Anything written after `exit 0` will never be executed. – Didier Trosset Dec 06 '12 at 14:58
  • Hi I also have and rvmsudo command to be executed in same situation. the command normally would ask for password. Will there be any issues if I place the command directly in /etc/rc.local ? – raviture Jul 09 '15 at 14:39
  • @fmonegaglia It depends. If you want the command to be started, and the script waits for it to finish before continuing to the next command on the script, then no `&`. If you want the following command to be started even if the current one is not finished, add a `&`. – Didier Trosset Jan 25 '16 at 09:12
  • @NoOne Check access rights on this file for the current user. Starting editor command with sudo may do the trick. – Didier Trosset Sep 23 '16 at 08:23
  • could you elaborate more? which program executes that script? seems like i don't have this file on a non-ubuntu (arch) distribution .. does ubuntu use another init system than systemd? or just some other config? – philx_x Feb 20 '17 at 20:13
  • @philx_x Have a look at this post: https://www.linuxbabe.com/linux-server/how-to-enable-etcrc-local-with-systemd – Didier Trosset Feb 22 '17 at 07:47
  • This doesnt seem to work on 18.04. I'm starting a daemon in `rc.local`, but it isn't there after login. – oarfish Apr 13 '19 at 14:32
  • @oarfish, lots of input about this on the Intenet. First check that `/etc/rc.local` does exist, that it has executable permissions and that it starts with a correct hashbang (#!/usr/bin for example). You also may have to run `systemctl enable rc-local.service`, but it is usually already enabled, and the failure to start comes from the previous requirements that are not fulfilled. – Didier Trosset Apr 15 '19 at 09:25
  • 1
    Of course it exists and yes, I did set the executable bit. When trying to enable the service, I only get `The unit files have no installation config (WantedBy, RequiredBy, Also, Alias settings in the [Install] section, and DefaultInstance for template units) […].` Any advice on this? – oarfish Apr 15 '19 at 14:54
9

Edit the tty configuration in /etc/init/tty*.conf with a shellscript as a parameter :

(...)
exec /sbin/getty -n -l  theInputScript.sh -8 38400 tty1
(...)

This is assuming that we're editing tty1 and the script that reads input is theInputScript.sh.

A word of warning this script is run as root, so when you are inputing stuff to it you have root priviliges. Also append a path to the location of the script.

Important: the script when it finishes, has to invoke the /sbin/login otherwise you wont be able to login in the terminal.

BMN
  • 8,253
  • 14
  • 48
  • 80
4

Nice answers. You could also set Jobs (i.e., run commands) with "Crontab" for more flexibility (which provides different options to run scripts, loggin the outputs, etc.), although it requires more time to be understood and set properly:

Using '@reboot' you can Run a command once, at startup.

Wrapping up:
$ sudo crontab -e -u root

And add a line at the end of the file with your command as follows:

@reboot sudo searchd
Javi
  • 913
  • 2
  • 13
  • 15