且构网

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

Linux Ubuntu下启动时自动运行程序

更新时间:2023-01-30 11:52:23

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

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

The 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 版本中无效.根据 更新 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/*.

同样在评论中指出(由 Charles Brandt),/filename 必须是一个 init 风格的脚本.还提供了一个很好的模板 - System V init 脚本模板.

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

正如评论中所指出的(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的手册,它可以在两种模式下运行:使用legacy模式的机器会有一个文件/etc/init.d/.legacy-bootordering">",在这种情况下,您必须通过命令行参数传递序列和运行级别配置.

According to the 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 文件名开始 20 2 3 4 5 .停止 20 0 1 6 .