Ubuntu 18.04 配置開機運行腳本

今天想給系統(tǒng)配置開機啟動腳本,目的能夠使用腳本實現(xiàn)開機自動啟動synergy,梯子等程序。爬了諸多博客,基本是直接在/etc/rc.loca 里面寫入執(zhí)行的shell命令,但是這樣無法成功。原因就是: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這個文件后,看到的內(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)行添加一個Install段的代碼,這部分的作用就是定義開機啟動后執(zhí)行的動作,內(nèi)容為:

 [Install]                                                                                                                                                 
 WantedBy=multi-user.target                                                                                                                                
 Alias=rc-local.service  

step 3
編輯系統(tǒng)的/etc/rc.local 文件,這里你要寫入的內(nèi)容就是希望開機執(zhí)行的命令,比如我希望開機自動啟動trojan:

cd /usr/bin/trojan && sudo ./trojan &
# /usr/bin/下面的trojan是我自己創(chuàng)建文件夾,存放著trojan及其配置文件

如果沒有/etc/rc.local文件,則自行創(chuàng)建一個即可:

touch /etc/rc.local

注意rc.loca的文件中的必須內(nèi)容,我把系統(tǒng)中默認(rèn)的格式復(fù)制過來,以期日后所需。

#!/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
改變這個文件的權(quán)限,讓它可以被執(zhí)行

sudo chmod +x /etc/rc.local

step 5
在/lib/systemd/system路徑下,創(chuàng)建一個軟鏈接。

多啰嗦兩句:systemd默認(rèn)讀取/etc/systemd/system/下的配置文件,而該目錄下的文件會鏈接/lib/systemd/system/下面的文件。ubuntu 18.04 安裝完后/lib/systemd/system/就會有rc-local.service,這個是我們剛剛編輯過的文本。創(chuàng)建這個軟鏈接是為了讓系統(tǒng)啟動時通過/etc/systemd/system/讀取到rc.loca.service。

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

按照以上步驟,即可實現(xiàn)開機啟動希望的應(yīng)用了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容