Ubuntu - Run command on start-up with "sudo"

LinuxUbuntu 11.10

Linux Problem Overview


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

sudo searchd

How would I do this?

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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.

Solution 3 - Linux

Nice answers. You could also set Jobs (i.e., 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark WillisView Question on Stackoverflow
Solution 1 - LinuxDidier TrossetView Answer on Stackoverflow
Solution 2 - LinuxB FView Answer on Stackoverflow
Solution 3 - LinuxJaviView Answer on Stackoverflow