原文:https://www.magentonotes.com/ubuntu-config-autostart-shell-script.html
方法一:將腳本添加到文件/etc/rc.local
/etc/rc.local腳本是一個ubuntu開機后會自動執行的腳本,我們可以在該腳本內添加命令行指令。該文件需要root權限才能修改。該腳本具體格式如下,
注意,一定要將命令添加在 exit 0之前,比如我用VirtualBox啟動Ubuntu虛擬機,虛擬機上配置的 網絡連接是橋接模式,但Ubuntu每次啟動后,網絡
都連接不上,需要啟動后,自己再執行NetworkManager命令,這里,我們就可以將這個命令的執行放在/etc/rc.local里面,
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.
# add by zhj,啟動網絡連接
NetworkManager
exit
0
|
方法二:將腳本文件放在/etc/init.d/目錄下
要注意的是,/etc/init.d/目錄下的腳本的內容是有格式要求的,不能隨便寫,具體格式自己在網上搜吧
1,新建個腳本文件new_service.sh
1
2
3
4
|
#!/bin/bash
# command content
exit
0
|
2,設置權限
1
|
sudo
chmod
755 new_service.sh
|
3,把腳本放置到啟動目錄下
1
|
sudo
mv
new_service.sh
/etc/init
.d/
|
4,將腳本添加到啟動腳本
執行如下指令,在這里90表明一個優先級,越高表示執行的越晚
1
2
|
cd
/etc/init
.d/
sudo
update-rc.d new_service.sh defaults 90
|
移除Ubuntu開機腳本
1
|
sudo
update-rc.d -f new_service.sh remove
|