linux ubuntu下启动时自动运行程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7221757/
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-05 05:54:59  来源:igfitidea点击:

Run automatically program on startup under linux ubuntu

linuxubuntustartup

提问by Matteo Monti

I'd need a program to be run every time I startup my ubuntu linux. So I'd need to add it to my startup programs list. Just one problem: I'd need to do it via terminal.

每次启动 ubuntu linux 时,我都需要运行一个程序。所以我需要将它添加到我的启动程序列表中。只有一个问题:我需要通过终端来完成。

回答by Dave Lasley

sudo mv /filename /etc/init.d/
sudo chmod +x /etc/init.d/filename 
sudo update-rc.d filename defaults 

Script should now start on boot. Note that this method also works with both hard links and symbolic links (ln).

脚本现在应该在启动时启动。请注意,此方法也适用于硬链接和符号链接 ( ln)。

Edit

编辑

At this point in the boot process PATH isn't set yet, so it is critical that absolute paths are used throughout. BUT, as pointed out in the comments by Steve HHH, explicitly declaring the full file path (/etc/init.d/filename) for the update-rc.d command is not valid in most versions of Linux. Per the manpage for update-rc.d, the second parameter is a script located in /etc/init.d/*. Updated above code to reflect this.

此时引导过程中的 PATH 尚未设置,因此始终使用绝对路径至关重要。但是,正如 Steve HHH 在评论中指出的那样,明确声明/etc/init.d/filenameupdate-rc.d 命令的完整文件路径 ( ) 在大多数 Linux 版本中都无效。根据update-rc.d联机帮助页,第二个参数是位于/etc/init.d/*. 更新了上面的代码以反映这一点。

Another Edit

另一个编辑

Also as pointed out in the comments (by Charles Brandt), /filenamemust be an init style script. A good template was also provided - https://github.com/fhd/init-script-template.

同样如评论中所指出的(由 Charles Brandt),/filename必须是一个 init 风格的脚本。还提供了一个很好的模板 - https://github.com/fhd/init-script-template

Another link to another article just to avoid possible link rot (although it would be saddening if GitHub died) - http://www.linux.com/learn/tutorials/442412-managing-linux-daemons-with-init-scripts

另一个指向另一篇文章的链接只是为了避免可能的链接腐烂(尽管如果 GitHub 死了会令人难过) - http://www.linux.com/learn/tutorials/442412-managing-linux-daemons-with-init-scripts

yetAnother Edit

另一个编辑

As pointed out in the comments (by Russell Yan), This works only on default mode of update-rc.d.

正如评论中所指出的(Russell Yan),这仅适用于 update-rc.d 的默认模式。

According to manual of update-rc.d, it can run on two modes, "the machines using the legacy mode will have a file /etc/init.d/.legacy-bootordering", in which case you have to pass sequence and runlevel configuration through command line arguments.

根据 update-rc.d 的手册,它可以在两种模式下运行,“使用传统模式的机器会有一个文件/etc/init.d/.legacy-bootordering”,在这种情况下,您必须通过命令行参数传递序列和运行级别配置。

The equivalent argument set for the above example is

为上述示例设置的等效参数是

sudo update-rc.d filename start 20 2 3 4 5 . stop 20 0 1 6 .

sudo update-rc.d filename start 20 2 3 4 5 . stop 20 0 1 6 .