raspberry PI 將Python指令碼寫成服務並設定開機啟動

來源:互聯網
上載者:User

標籤:

 1 #!/bin/bash 2 # /etc/init.d/service_name 3  4 ### BEGIN INIT INFO 5 # Provides:          Provider 6 # Required-Start:    $remote_fs $syslog 7 # Required-Stop:     $remote_fs $syslog 8 # Default-Start:     2 3 4 5 9 # Default-Stop:      0 1 610 # Short-Description: Example initscript11 # Description:       This service is a test12 ### END INIT INFO13 14 15 case "$1" in 16     start)17         echo "Starting service_name"18         cd /home/pi19         su pi -c "python /home/pi/pythonCode/test.py"20         ;;21     stop)22         echo "Stopping service_name"23         killall service_name24         ;;25     *)26         echo "Usage: /etc/init.d/service_name start|stop"27         exit 128         ;;29 esac30 exit 0
View Code

首先在控制台輸入:sudo nano /etc/init.d/service_name,將上面的代碼複製到檔案內。上面的代碼中,“service_name”是你自己為這個服務起的名字,BEGIN INIT INFO 根據需要更改,不會影響程式。

將檔案儲存後,再輸入

1 sudo chmod +x /etc/init.d/service_name

其中service_name是你自己的名字。

輸入

1 sudo update-rc.d /etc/init.d/service_name defaults

即可。

參考:http://raspberrywebserver.com/serveradmin/run-a-script-on-start-up.html

raspberry PI 將Python指令碼寫成服務並設定開機啟動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.