1. 程式人生 > >Ubuntu 18.04 rc.local systemd設定

Ubuntu 18.04 rc.local systemd設定

ubuntu18.04不再使用initd管理系統,改用systemd。
然而systemd很難用,改變太大,跟之前的完全不同。

使用systemd設定開機啟動
為了像以前一樣,在/etc/rc.local中設定開機啟動程式,需要以下幾步:
1、systemd預設讀取/etc/systemd/system下的配置檔案,該目錄下的檔案會連結/lib/systemd/system/下的檔案。一般系統安裝完/lib/systemd/system/下會有rc-local.service檔案,即我們需要的配置檔案。

連結過來:

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

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

1) [Unit] 區塊:啟動順序與依賴關係。 

2) [Service] 區塊:啟動行為,如何啟動,啟動型別。 

3) [Install] 區塊,定義如何安裝這個配置檔案,即怎樣做到開機啟動。


2、建立/etc/rc.local檔案

touch /etc/rc.local

3、賦可執行許可權

chmod 755 /etc/rc.local

4、編輯rc.local,新增需要開機啟動的任務

#!/bin/bash

echo "test rc " > /var/test.log

5、執行reboot重啟系統,然後檢視test.log