1.權限設置(/etc/rc.local和/etc/rc.d/rc.local都需要賦予執行權限)
chmod +x /etc/rc.d
chmod +x /etc/rc.d/rc.local
2.設置服務啓動腳本。
1)進入路徑
cd /usr/lib/systemd/system
2)編輯服務腳本
vi 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.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
3.使用命令啓動服務(確認服務可用)
systemctl start rc-local
使用systemctl -t service查看服務器所有服務的狀態,確認服務rc-local.service是否運行正常.
4.執行systemctl enable rc-local
systemctl enable rc-local
出現以下提示表示成功添加開機自啓動:Created symlink from /etc/systemd/system/multi-user.target.wants/rc-local.service to /usr/lib/systemd/system/rc-local.service.
如下圖:
完成以上步驟就可以將開機自動執行的命令寫入/etc/rc.d/rc.local中,實現開機自動執行。