linux開機自啟動程式設定方法

2021-07-24 07:18:01 字數 3070 閱讀 4740

linux如何開機執行自己的程式呢?

搞了幾天終於搞定。。。。

1.首先在自己的工程檔案裡編寫自啟動shell檔案,這是我的例子如下:檔案名字(testudp.sh),可執行程式為tst_client:

#!/bin/sh

###begin init info

# provides: tst_client

# required-start:

# required-stop:

# default-start: 2 3 4 5

# default-stop: 0 1 6

# description: my test

### end init info

user="root"

dir="/home/usr/test/udp3/tst_client"

cmd="./tst_client"

name=`basename $0`

pid_file="/var/run/$name.pid"

stdout_log="/tmp/$name.log"

stderr_log="/tmp/$name.err"

get_pid()

is_running()

case "$1" in

start)

if is_running; then

echo "already started"

ficd "$dir"

sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &

echo $! > "$pid_file"

;; stop)

kill `get_pid`

;; *)

echo 「auto start failed」

exit 1

;;esac

exit 0

編寫規則如下:1.要有乙個start,一般寫上執行自己程式所需bash指令,就如在終端中執行一樣:

sudo -root ./run

2.其次要有乙個stop,一般都是寫上kill當前自己的程序。

start和stop用於放在/etc/rc.d檔案中,s開頭用於啟動當前程式,k開頭表示kill當前程序。

注:編寫好shell檔案後,chmod +x  shell檔案將其變為可執行檔案,然後一定要現在自己的終端視窗試一下,可以執行後再進行下一步操作。

2.再將剛剛編寫的shell檔案拷貝到/etc/init.d中

3.再將shell檔案的軟鏈結放置在/etc/rc.d檔案中:

rc.d檔案分為rc0~rc6,每個數字表示不同的執行等級,每個等級表示如下:

linux系統有7個執行級別(runlevel)

執行級別0:系統停機狀態,系統預設執行級別不能設為0,否則不能正常啟動

執行級別1:單使用者工作狀態,root許可權,用於系統維護,禁止遠端登陸

執行級別2:多使用者狀態(沒有nfs)

執行級別3:完全的多使用者狀態(有nfs),登陸後進入控制台命令列模式

執行級別4:系統未使用,保留

執行級別5:x11控制台,登陸後進入圖形gui模式

執行級別6:系統正常關閉並重啟,預設執行級別不能設為6,否則不能正常啟動

因此可以將啟動鏈結放置在2,3,4,5,將kill鏈結放置在0,1,6.

開啟自己的linux資料夾中,rc0.d、rc6.d中的檔案一般以k開頭,表示停機時關閉這些程式;

rc1.d中的檔案既有k也有s,

rc2.d、rc3.d、rc4.d、rc5.d中全部為s開頭,表示此等級下啟動程式

軟連線設定shell檔案如下,也可以拆分成bash指令,乙個乙個來執行,但太繁瑣。shell檔案如下:(可將此檔案起名為install.sh)

#!/bin/bash

smtcdir=/home/usr/test/udp3/tst_client

initd="/etc/init.d/testudp.sh"

target="testudp.sh"

install -m 755 -g root -o root $smtcdir/testudp.sh $initd

ln -s $initd /etc/rc0.d/"k01$target"

ln -s $initd /etc/rc1.d/"k01$target"

ln -s $initd /etc/rc2.d/"s05$target"

ln -s $initd /etc/rc3.d/"s05$target"

ln -s $initd /etc/rc4.d/"s05$target"

ln -s $initd /etc/rc5.d/"s05$target"

ln -s $initd /etc/rc6.d/"k01$target"

將此shell檔案chmod +x設定為可執行檔案,在終端執行./shell即可。此時檢視你的/etc/rc.d各個檔案中都有了你的軟連線。

注:一般自己的應用程式設定級別要注意,開啟時要放在系統程式之後,如上述shell檔案中都為s05;關閉時要放在系統程式之前,即最早關閉,如上述shell檔案中都為k01。

linux執行時都是按照序號來進行,序號越小越先執行。

4.取消開機自啟動編寫shell指令碼如下:(可將此檔案起名為unstall.sh)

#!/bin/bash

initd="/etc/init.d/testudp.sh"

target="testudp.sh"

rm -f $initd

rm -f /etc/rc*.d/*$target

好了目前為了實現開機自執行編寫的檔案有:

自啟動shell檔案:testudp.sh

rc.d軟鏈結設定檔案:install.sh

取消開機自執行檔案:unstall.sh

大家按照我的模板來寫就好了。

好了,以上就是我的心得。寫在這裡,怕以後忘記了。因為自啟動檔案寫的也少。

Linux 設定開機自啟動

執行命令 編輯 etc rc.local 新增你想開機執行的命令 執行程式指令碼 然後在檔案最後一行新增要執行程式的全路徑。例如,每次開機時要執行乙個hello.sh,這個指令碼放在 usr下面,那就可以在 etc rc.local 中加一行 usr hello.sh 或者 cd opt hello...

Linux設定開機自啟動

執行程式指令碼 然後在檔案最後一行新增要執行程式的全路徑。例如,每次開機時要執行乙個hello.sh,這個指令碼放在 usr下面,那就可以在 etc rc.local 中加一行 usr hello.sh 或者 cd opt hello.sh 注意,你的命令應該新增在 exit 0 之前 linux在...

linux 開機啟動 自啟動 設定

方法一 編輯rc.loacl指令碼 ubuntu開機之後會執行 etc rc.local檔案中的指令碼。所以我們可以直接在 etc rc.local中新增啟動指令碼。vim etc rc.local方法二 新增乙個開機啟動服務 將你的啟動指令碼複製到 etc init.d目錄下,並設定指令碼許可權,...