缘由

一些情况下,希望开机自启一些程序,但又不想为每个程序都创建 systemd 服务? 你可直接以脚本形式追加至 rc.local 文件以实现该功能。

步骤

/etc/systemd/system/rc-local.service 追加以下代码

[Install]
 WantedBy=multi-user.target

若该路径文件不存在,则使用以下代码创建。

[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
#StandardOutput=tty
#SysVStartPriority=99

[Install]
WantedBy=multi-user.target
Code language: PHP (php)

创建 rc.local 文件 ,并在首行设置解释器。

#!/bin/bash
# your codes here..Code language: PHP (php)

为其设置执行权限

chmod +x /etc/rc.local

重载服务并启用自启

systemctl daemon-reload 
systemctl enable rc-local

完成

你可在 /etc/rc.local 文件内自由追加代码,系统启动时将随即执行。


0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据