Linux Ubuntu - 在启动时使用“sudo”运行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9683357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:08:38  来源:igfitidea点击:

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

linuxubuntu-11.10

提问by Mark Willis

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

我想sudo在 Ubuntu 启动时(在任何人登录之前)运行一个命令:

sudo searchd

How would I do this?

我该怎么做?

采纳答案by Didier Trosset

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

您可以在/etc/rc.local启动结束时执行的脚本中添加该命令。

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

之前写命令exit 0。之后写入的任何内容exit 0将永远不会被执行。

回答by Nugget

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

/etc/init/tty*.conf使用 shellscript 作为参数编辑 tty 配置:

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

这是假设我们正在编辑 tty1 并且读取输入的脚本是 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.

警告这个脚本是作为 root 运行的,所以当你向它输入内容时,你有 root 权限。还将路径附加到脚本的位置。

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

重要提示:脚本完成后,必须调用 /sbin/login 否则您将无法登录终端。

回答by Javi

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:

很好的答案。您还可以使用“ Crontab”设置作业(即命令)以获得更大的灵活性(它提供了运行脚本、记录输出等的不同选项),尽管它需要更多时间来理解和正确设置:

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

使用“@reboot”,您可以在启动时运行一次命令。