只针对Debian 9 stretch,通常来说,需要自启动的应用程序路径都会添加在/etc/rc.local这个文件里面,然而搞笑的是,在Debian 9 stretch里/etc路径下,根本就没有rc.local这个文件,但是!!!
通过systemctl status rc-local却能看到,rc.local这个服务却是存在的。紧贴着底部Active状态项,可以看到,此时的rc.local是非活动的服务。
为了解决这个问题,我们需要自建rc.local文件。控制台输入修改两个文件,然后添加如下代码
nano /lib/systemd/system/rc-local.service
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[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
[Install]
WantedBy=multi-user.targetnano /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
##/root/frpc/frpc -c /root/frpc/frpc.ini > /dev/null 2>&1 &
exit 0输入EOF时就代表代码段的结束。除了这种方法外,自己也可以新建文档,通过vi指令,向文档中添加相同代码(此时就不需要输入最下方的EOF了),记得文档名字必须是rc.local就行。
文件保存完毕后,通过chmod +x rc.local给文件加权限,然后移动文件到/etc路径下,
启动服务:systemctl start rc-local
开机自启:systemctl enable rc-local
控制台输入systemctl status rc-local再查看rc.local服务状态,可以看到已经是active了。
此时说明自建的rc.local文件随系统启动已经没什么问题了,这时,将自己需要开机自动运行的程序路径,写到rc.local文件中的exit 0上方,即可完成自定义应用随系统开机自启动。
本文来自:https://blog.csdn.net/natty715/article/details/89816535