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)腳本移除;