天天看點

ubuntu18mysql服務開機啟動_ubuntu18.04 設定開機自啟指令

sudo systemctl start mysqld.service

systemd 預設讀取 /etc/systemd/system 下的配置檔案,該目錄下的檔案會連結/lib/systemd/system/下的檔案。

執行 ls /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=0RemainAfterExit=yes

一般正常的啟動檔案主要分成三部分

[Unit] 段: 啟動順序與依賴關系

[Service] 段: 啟動行為,如何啟動,啟動類型

[Install] 段: 定義如何安裝這個配置檔案,即怎樣做到開機啟動

可以看出,/etc/rc.local 的啟動順序是在網絡後面,但是顯然它少了 Install 段,也就沒有定義如何做到開機啟動,是以顯然這樣配置是無效的。 是以我們就需要在後面幫他加上 [Install] 段:

[Install]

WantedBy=multi-user.target

Alias=rc-local.service

這裡需要注意一下,ubuntu-18.04 預設是沒有 /etc/rc.local 這個檔案的,需要自己建立

sudo touch /etc/rc.local

然後把你需要啟動腳本寫入 /etc/rc.local ,我們不妨寫一些測試的腳本放在裡面,以便驗證腳本是否生效.

echo "this just a test" > /usr/local/text.log

做完這一步,還需要最後一步 前面我們說 systemd 預設讀取 /etc/systemd/system 下的配置檔案, 是以還需要在 /etc/systemd/system 目錄下建立軟連結

ln-s /lib/systemd/system/rc.local.service /etc/systemd/system/

接下來,重新開機系統,然後看看 /usr/local/text.log 檔案是否存在就知道開機腳本是否生效了。

rc.local腳本

rc.local腳本是一個ubuntu開機後會自動執行的腳本,我們可以在該腳本内添加指令行指令。該腳本位于/etc/路徑下,需要root權限才能修改。

該腳本具體格式如下:

1 #!/bin/sh -e

2 #3 #rc.local

4 #5 #This script is executed at the end of each multiuser runlevel.

6 #Make sure that the script will "exit 0" on success or any other

7 #value on error.

8 #9 #In order to enable or disable this script just change the execution

10 #bits.

11 #12 #By default this script does nothing.

13

14 exit 0

注意:一定要将指令添加在 exit 0之前