简体   繁体   English

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

[英]Ubuntu - Run command on start-up with “sudo”

I would like to run a sudo command when Ubuntu starts up (before anyone logs in):我想在 Ubuntu 启动时(在任何人登录之前)运行sudo命令:

sudo searchd

How would I do this?我该怎么做?

You can add the command in the /etc/rc.local script that is executed at the end of startup.您可以在启动结束时执行的/etc/rc.local脚本中添加该命令。

Write the command before exit 0 .exit 0之前编写命令。 Anything written after exit 0 will never be executed.exit 0之后写入的任何内容将永远不会被执行。

Edit the tty configuration in /etc/init/tty*.conf with a shellscript as a parameter :使用 shellscript 作为参数编辑/etc/init/tty*.conf的 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.警告这个脚本是作为 root 运行的,所以当你向它输入内容时,你有 root 权限。 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.重要提示:脚本完成后,必须调用 /sbin/login 否则您将无法登录终端。

Nice answers.很好的答案。 You could also set Jobs (ie, 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”,您可以在启动时运行一次命令。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM