http://www.cnblogs.com/defifind/p/9285456.html
ubuntu-server-18.04 設(shè)置開機(jī)啟動(dòng)腳本
?ubuntu-16.10 開始不再使用initd管理系統(tǒng)料睛,改用systemd
systemd is now used for user sessions. System sessions had already been provided by systemd in previous Ubuntu releases.
快速看了 systemd 的使用方法痊焊,發(fā)現(xiàn)改動(dòng)有點(diǎn)大努酸, 包括用 systemctl 命令來替換了 service 和 chkconfig 的功能营曼。
比如以前啟動(dòng) mysql 服務(wù)用:
sudo service mysql start
現(xiàn)在用:
sudo systemctl start mysqld.service
sudo?systemctl?daemon-reload
其實(shí)這個(gè)改動(dòng)到不是算大养铸,主要是開機(jī)啟動(dòng)比以前復(fù)雜多了狐赡。systemd 默認(rèn)讀取 /etc/systemd/system 下的配置文件奈籽,該目錄下的文件會(huì)鏈接/lib/systemd/system/下的文件巾表。
執(zhí)行 ls /lib/systemd/system 你可以看到有很多啟動(dòng)腳本,其中就有我們需要的rc.local.service
打開腳本內(nèi)容:
#? 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 CompatibilityConditionFileIsExecutable=/etc/rc.localAfter=network.target[Service]Type=forkingExecStart=/etc/rc.local startTimeoutSec=0RemainAfterExit=yes
一般正常的啟動(dòng)文件主要分成三部分
[Unit] 段: 啟動(dòng)順序與依賴關(guān)系
[Service] 段: 啟動(dòng)行為,如何啟動(dòng)史隆,啟動(dòng)類型
[Install] 段: 定義如何安裝這個(gè)配置文件魂务,即怎樣做到開機(jī)啟動(dòng)
可以看出,/etc/rc.local 的啟動(dòng)順序是在網(wǎng)絡(luò)后面泌射,但是顯然它少了 Install 段粘姜,也就沒有定義如何做到開機(jī)啟動(dòng),所以顯然這樣配置是無效的熔酷。 因此我們就需要在后面幫他加上 [Install] 段:
[Install]WantedBy=multi-user.targetAlias=rc-local.service
這里需要注意一下孤紧,ubuntu-18.04 默認(rèn)是沒有 /etc/rc.local 這個(gè)文件的,需要自己創(chuàng)建
sudo touch /etc/rc.local
然后把你需要啟動(dòng)腳本寫入 /etc/rc.local 拒秘,我們不妨寫一些測(cè)試的腳本放在里面号显,以便驗(yàn)證腳本是否生效.
echo"this just a test"> /usr/local/text.log
做完這一步,還需要最后一步前面我們說 systemd 默認(rèn)讀取 /etc/systemd/system 下的配置文件, 所以還需要在 /etc/systemd/system 目錄下創(chuàng)建軟鏈接
ln-s /lib/systemd/system/rc.local.service /etc/systemd/system/
?接下來躺酒,重啟系統(tǒng)押蚤,然后看看 /usr/local/text.log 文件是否存在就知道開機(jī)腳本是否生效了。
rc.local腳本
rc.local腳本是一個(gè)ubuntu開機(jī)后會(huì)自動(dòng)執(zhí)行的腳本羹应,我們可以在該腳本內(nèi)添加命令行指令揽碘。該腳本位于/etc/路徑下,需要root權(quán)限才能修改园匹。
該腳本具體格式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/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
注意:?一定要將命令添加在 exit 0之前