Ubuntu 下添加開機(jī)啟動(dòng)腳本

Ubuntu下添加開機(jī)啟動(dòng)腳本
本文介紹在Ubuntu下添加開機(jī)啟動(dòng)腳本的兩種方法:
1.編輯 /etc/rc.local文件
Ubuntu 會(huì)在啟動(dòng)時(shí)自動(dòng)執(zhí)行 /etc/rc.local文件中的腳本,默認(rèn)該文件中有效的腳本代碼為空,把需要執(zhí)行的腳本添加到該文件的exit 0 之前即可,舉例如下:

#!/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.
cd /home/ubuntu
echo 'hello,world' >> rc.local.log
exit 0

2.通過(guò) update-rc.d命令添加開機(jī)自啟動(dòng)腳本
Ubuntu 服務(wù)器在啟動(dòng)時(shí)會(huì)自動(dòng)執(zhí)行 /etc/init.d目錄下的腳本,所以我們可以將需要執(zhí)行的腳本放到/etc/init.d目錄下,或者在該目錄下創(chuàng)建一個(gè)軟件鏈接指向其他位置的腳本路徑,然后通過(guò)update-rc.d將腳本添加到開機(jī)自啟動(dòng)。啟動(dòng)腳本必須以 #!/bin/bash 開頭。舉例如下:
新建開機(jī)啟動(dòng)腳本start_when_boot,放置到/etc/init.d 目錄

#!/bin/bash
### BEGIN INIT INFO
# Provides:          steven_qin
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: self define auto start
# Description:       self define auto start
### END INIT INFO
wget -q -O- https://oa.xxxx.net/cron/index

修改權(quán)限sudo chmod 755 /etc/init.d/start_when_boot
執(zhí)行 update-rc.d start_when_boot defaults將上述腳本添加為開機(jī)啟動(dòng);
執(zhí)行update-rc.d -f start_when_boot remove將上述開機(jī)啟動(dòng)腳本移除;

轉(zhuǎn)載文章

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

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

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