Ubuntu從16.04開始不再使用 initd 管理系統,改用 systemd。 和 Centos 一樣,升級到 Centos7 之后使用 systemd 替代 init.d
為了像以前一樣,在/etc/rc.local中設置開機啟動程序,需要以下幾步:
1、鏈接文件
systemd 默認讀取 /etc/systemd/system 下的配置文件,該目錄下的文件會鏈接 /lib/systemd/system/ 下的文件。一般系統安裝完成之后會在/lib/systemd/system/形成rc-local.service文件,即我們需要的配置文件。
開機時,Systemd只執行 /etc/systemd/system 目錄里面的配置文件。這也意味著,如果把修改后的配置文件放在該目錄,就可以達到覆蓋原始配置的效果
鏈接過來:ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
使用systemctl enabled rc-local.service 同樣可以達到效果,
systemctl is-enabled unit #顯示某個unit服務是否建立了啟動鏈接
systemctl enabled unit ? ?#為某個unit服務創建啟動鏈接
查看rc-local.service內容:
cd /etc/systemd/system/
vim 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[Install]
WantedBy=multi-user.target
Alias=rc-local.service
2、創建/etc/rc.local文件
touch /etc/rc.local
3、賦可執行權限
chmod 755 /etc/rc.local
4、編輯rc.local,添加需要開機啟動的任務
#!/bin/bash
echo "test test " > /var/test_boot_up.log
5、執行reboot重啟系統驗證OK。
最后,說一下/etc/systemd/system/下的配置文件(XXXX.service),其中有三個配置項,[Unit] / [Service] / [Install]?
- [Unit] 區塊:啟動順序與依賴關系。
- [Service] 區塊:啟動行為,如何啟動,啟動類型。
- [Install] 區塊,定義如何安裝這個配置文件,即怎樣做到開機啟動。
ubuntu版本為17.10,如果不成功,使用命令
systemctl enable rc-local.service