2021年4月12日星期一

配置rc-local开机自启动功能

一、适用于Debian10+:

某些软件并添加开启启动的服务,很多时候需要手动不添加,一般我们都是推荐添加到/etc/rc.local文件,但是Debian 11默认不带/etc/rc.local文件,而rc.local服务却还是自带的

查看默认rc.local服务文件:cat /lib/systemd/system/rc.local.service

#  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
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

并且默认情况下这个服务还是关闭的状态

systemctl status rc-local   查看状态显示如下:

● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: inactive (dead)

为了解决这个问题,我们需要手动添加一个vi /etc/rc.local文件

sudo touch /etc/rc.local

编辑相应文件vi /etc/rc.local输入以下内容:

#!/bin/bash
#
# 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.

exit 0

设置权限    chmod +x /etc/rc.local

启动rc-local服务     systemctl start rc-local

再次查看状态     systemctl status rc-local
显示如下:

● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: active (exited) since Thu 2017-08-03 09:41:18 UTC; 14s ago
  Process: 20901 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

Aug 03 09:41:18 xtom-hk systemd[1]: Starting /etc/rc.local Compatibility...
Aug 03 09:41:18 xtom-hk systemd[1]: Started /etc/rc.local Compatibility.


然后你就可以把需要启动的命令放到/etc/rc.local中了,(exit 0之前)

尝试重启作为命令生效。


二、适用于Ubuntu18+:

1.手动创建rc-local.service文件:

sudo touch /etc/systemd/system/rc-local.service
编辑rc-local.service
sudo vim /etc/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.target

保存。

或利用系统默认文件建立软连接
ln -s /usr/lib/systemd/system/rc-local.service    /etc/systemd/system/rc-local.service
编辑rc-local.service
sudo vim /etc/systemd/system/rc-local.service
加入字段[Install]
[Install]
WantedBy=multi-user.target

保存。

2、终端一键写入方式创建rc-local.service文件:

sudo echo "
#  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.target
" >> etc/systemd/system/rc-local.service

3.接下来就是创建一个rc.local:

sudo touch /etc/rc.local

编辑 rc.local
sudo vi /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.

exit 0


在 exit 0 行前面添加待开机启动的命令:

# 延时30秒后执行命令
sleep 30

# 示例:后台启动chfs服务:
nohup /usr/local/app/chfs/chfs --file /usr/local/app/chfs/chfs.ini &

# 示例:挂载Samba共享
mount -t cifs -o username=User,password=Admin-1113,gid=0,uid=0, //192.168.1.52/数据盘  /mnt/SMB


exit 0


给rc.local 添加执行的权限
sudo chmod +x /etc/rc.local


4.接下来是启动服务:


①.刷新systemctl配置
systemctl daemon-reload

②.设置开机启动
systemctl enable rc-local.service

③.启动服务
sudo systemctl start rc-local.service

④.测试服务的状态
sudo systemctl status rc-local.service

⑤.重启服务
sudo systemctl restart rc-local.service