今天想給系統(tǒng)配置開機(jī)啟動(dòng)腳本玖翅,目的能夠使用腳本實(shí)現(xiàn)開機(jī)自動(dòng)啟動(dòng)synergy,梯子等程序。爬了諸多博客义钉,基本是直接在/etc/rc.loca 里面寫入執(zhí)行的shell命令,但是這樣無(wú)法成功规肴。原因就是: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.
解決辦法:
step 1
cd /lib/systemd/system
sudo vim rc-local.service
使用vim編輯器打開rc-local.sevice這個(gè)文件后拖刃,看到的內(nèi)容如下:
1 # SPDX-License-Identifier: LGPL-2.1+
2 #
3 # This file is part of systemd.
4 #
5 # systemd is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; either version 2.1 of the License, or
8 # (at your option) any later version.
9
10 # This unit gets pulled automatically into multi-user.target by
11 # systemd-rc-local-generator if /etc/rc.local is executable.
12 [Unit]
13 Description=/etc/rc.local Compatibility
14 Documentation=man:systemd-rc-local-generator(8)
15 ConditionFileIsExecutable=/etc/rc.local
16 After=network.target
17
18 [Service]
19 Type=forking
20 ExecStart=/etc/rc.local start
21 TimeoutSec=0
22 RemainAfterExit=yes
23 GuessMainPID=no
step 2
我們需要在文件的下方進(jìn)行添加一個(gè)Install段的代碼删壮,這部分的作用就是定義開機(jī)啟動(dòng)后執(zhí)行的動(dòng)作,內(nèi)容為:
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
step 3
編輯系統(tǒng)的/etc/rc.local 文件兑牡,這里你要寫入的內(nèi)容就是希望開機(jī)執(zhí)行的命令央碟,比如我希望開機(jī)自動(dòng)啟動(dòng)trojan:
cd /usr/bin/trojan && sudo ./trojan &
# /usr/bin/下面的trojan是我自己創(chuàng)建文件夾,存放著trojan及其配置文件
如果沒(méi)有/etc/rc.local文件均函,則自行創(chuàng)建一個(gè)即可:
touch /etc/rc.local
注意rc.loca的文件中的必須內(nèi)容亿虽,我把系統(tǒng)中默認(rèn)的格式復(fù)制過(guò)來(lái),以期日后所需苞也。
#!/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 chagne the execution
# bits.
#
# By default this script does nothing.
# to start the sslocal service when PC is open
# /home/vincent/.local/bin/sslocal -c /etc/shadowsocks.json &
cd /usr/bin/trojan && sudo ./trojan &
exit 0
step 4
改變這個(gè)文件的權(quán)限洛勉,讓它可以被執(zhí)行
sudo chmod +x /etc/rc.local
step 5
在/lib/systemd/system路徑下,創(chuàng)建一個(gè)軟鏈接如迟。
多啰嗦兩句:systemd默認(rèn)讀取/etc/systemd/system/下的配置文件收毫,而該目錄下的文件會(huì)鏈接/lib/systemd/system/下面的文件。ubuntu 18.04 安裝完后/lib/systemd/system/就會(huì)有rc-local.service殷勘,這個(gè)是我們剛剛編輯過(guò)的文本此再。創(chuàng)建這個(gè)軟鏈接是為了讓系統(tǒng)啟動(dòng)時(shí)通過(guò)/etc/systemd/system/讀取到rc.loca.service。
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
按照以上步驟玲销,即可實(shí)現(xiàn)開機(jī)啟動(dòng)希望的應(yīng)用了输拇。