2020-11-12 ubuntu18-自启动脚本

自启动脚本

sudo vim /etc/rc.local
#!/bin/sh 
# 
# 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.
. /home/qy/shell_doc/start_nvidia_pm.sh
. /home/qy/rjsupplicant/rjsupplicant.sh
exit 0

. /home/qy/shell_doc/start_nvidia_pm.sh : 设置gpu nvidia-smi -pm 1

. /home/qy/rjsupplicant/rjsupplicant.sh : 联网脚本

Ubuntu 16.10开始不再使用 initd 管理系统, 改用 systemd , 使用命令 systemctl , sudo systemctl start xxx.service , systemd 默认读取 /etc/systemd/system 下的配置文件, 该目录下文件链接到 /lib/systemd/system 下的文件。

执行命令 `ls /lib/systemd/system`可以看到很多启动脚本,
其中就有我们需要的`rc-local.service`,打开脚本内容如下:
sudo vim /lib/systemd/system/rc-local.service

/lib/systemd/system/rc-local.service

#  SPDX-License-Identifier: LGPL-2.1+
#
#  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
#Alias=rc-local.service

一般启动文件需要三个组成部分:

[Unit]段: 启动顺序与依赖关系
[Service] 段: 启动行为,如何启动,启动类型
[Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动

上面少了 [Install] 段,把下面 Install 段添上去,加入到 rc-local.service 的最后:

Alias=rc-local.service : Alias有设置别名的意思, 不知道这句话什么意思。

[Install]  
WantedBy=multi-user.target
#Alias=rc-local.service
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.

在这个 rc-local.service 中我们已经声明了我们对应的自启动配置文件为 /etc/rc.local。然而 ubuntu18 是默认没有这个文件的,所以需要我们手动创建该文件。然后再将你需要的开机自启动脚本写入到这个文件下:

#!/bin/sh 
# 
# 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.
. /home/qy/shell_doc/start_nvidia_pm.sh
. /home/qy/rjsupplicant/rjsupplicant.sh
exit 0

然后给 rc.local 文件赋予可执行权限:

sudo chmod +x /etc/rc.local

方法1:启动服务并检查服务状态:(试过,没问题)

sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

方法2:(可能有问题)

ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service

重启后查看两个脚本是否启动成功。

你可能感兴趣的:(2020-11-12 ubuntu18-自启动脚本)