raspberry PI 将Python脚本写成服务并设置开机启动

 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 6
10 # Short-Description: Example initscript
11 # Description:       This service is a test
12 ### END INIT INFO
13 
14 
15 case "$1" in 
16     start)
17         echo "Starting service_name"
18         cd /home/pi
19         su pi -c "python /home/pi/pythonCode/test.py"
20         ;;
21     stop)
22         echo "Stopping service_name"
23         killall service_name
24         ;;
25     *)
26         echo "Usage: /etc/init.d/service_name start|stop"
27         exit 1
28         ;;
29 esac
30 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

posted @ 2015-06-23 17:22  uniqueS  阅读(1695)  评论(0编辑  收藏  举报