自啟動(dòng)腳本
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
: 設(shè)置gpu nvidia-smi -pm 1
. /home/qy/rjsupplicant/rjsupplicant.sh
: 聯(lián)網(wǎng)腳本
Ubuntu 16.10開(kāi)始不再使用 initd 管理系統(tǒng), 改用 systemd
, 使用命令 systemctl
, sudo systemctl start xxx.service
, systemd
默認(rèn)讀取 /etc/systemd/system
下的配置文件, 該目錄下文件鏈接到 /lib/systemd/system
下的文件囤热。
執(zhí)行命令 `ls /lib/systemd/system`可以看到很多啟動(dòng)腳本浪听,
其中就有我們需要的`rc-local.service`爱只,打開(kāi)腳本內(nèi)容如下:
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
一般啟動(dòng)文件需要三個(gè)組成部分:
[Unit]段: 啟動(dòng)順序與依賴關(guān)系
[Service] 段: 啟動(dòng)行為,如何啟動(dòng)朵锣,啟動(dòng)類型
[Install] 段: 定義如何安裝這個(gè)配置文件蝙泼,即怎樣做到開(kāi)機(jī)啟動(dòng)
上面少了 [Install] 段寥粹,把下面 Install 段添上去宋距,加入到 rc-local.service 的最后:
Alias=rc-local.service : Alias
有設(shè)置別名的意思轴踱, 不知道這句話什么意思。
[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.
在這個(gè) rc-local.service 中我們已經(jīng)聲明了我們對(duì)應(yīng)的自啟動(dòng)配置文件為 /etc/rc.local谚赎。然而 ubuntu18 是默認(rèn)沒(méi)有這個(gè)文件的淫僻,所以需要我們手動(dòng)創(chuàng)建該文件。然后再將你需要的開(kāi)機(jī)自啟動(dòng)腳本寫(xiě)入到這個(gè)文件下:
#!/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 文件賦予可執(zhí)行權(quán)限:
sudo chmod +x /etc/rc.local
方法1:?jiǎn)?dòng)服務(wù)并檢查服務(wù)狀態(tài):(試過(guò)壶唤,沒(méi)問(wèn)題)
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
方法2:(可能有問(wèn)題)
ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
重啟后查看兩個(gè)腳本是否啟動(dòng)成功雳灵。