由于某些軟件并沒有增加開啟啟動的服務杖玲,很多時候需要手工添加蓝厌,一般我們都是推薦添加命令到 /etc/rc.local 文件玄叠,但是 Ubuntu 默認不帶 /etc/rc.local 文件,而 rc.local 服務卻還是自帶的
很多時候有些程序或者腳本都需要添加開機自啟拓提,最簡單的方法就是使用rc.local自啟读恃,不過由于系統(tǒng)版本更替,很多新版本系統(tǒng)都沒有rc.local文件了代态,比如Debian 9寺惫、Ubuntu 17.10、Ubuntu 18.04蹦疑。這時候就需要我們手動設(shè)置下
root@ubuntu ~ # cat /etc/systemd/system/rc-local.service
# 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
并且默認情況下這個服務還是關(guān)閉的狀態(tài)
root@ubuntu ~ # 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)
1西雀、添加rc-local.service
#以下為一整條命令,一起復制運行
cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
EOF
2歉摧、添加一個 /etc/rc.local 文件
#以下為一整條命令判莉,一起復制運行
cat <<EOF >/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
EOF
3、添加權(quán)限并設(shè)置開機自啟
chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local.service
檢查狀態(tài):
systemctl status rc-local.service
返回Active:active信息,則成功憾筏。
再次查看狀態(tài)
root@ubuntu ~ # 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 前面即可真友,并嘗試重啟以后試試是否生效了