且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-30 12:11:22

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

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

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

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

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.

也如注释中所指出的那样(查尔斯·布兰德(Charles Brandt)),/filename必须是一个初始化样式脚本.还提供了一个很好的模板- https://github.com/fhd/init-script-template .

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

到另一篇文章的另一个链接只是为了避免可能的链接腐烂(尽管如果GitHub死了,这会令人伤心)-

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

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

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

根据update-rc.d的手册,它可以在两种模式下运行,使用传统模式的计算机将具有文件/etc/init.d/.legacy-bootordering",在这种情况下,您必须通过命令行传递序列和运行级别配置论点.

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.

上面示例中设置的等效参数是

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 .