樹莓派ubuntu MATE 16.04開機自動啓動python

樹莓派ubuntu MATE 16.04開機自動啓動python

備料


寫個最簡單的python
/home/robot/zzz/zzz.py

import time
c=1
while 1:
    time.sleep(1)
    c=c+1
    print(c)

運行

robot@robot:~/zzz$ python zzz.py
2
3
4
5
6
7
8


 

創建sh文件run.sh,內容如下

#!/bin/sh
python /home/robot/zzz/zzz.py

更改權限

sudo chmod -R 777 zzz

運行sh測試
 

robot@robot:~/zzz$ . run.sh
2
3
4
5
6
7
8
9
10


方法一:修改rc.local文件/etc/rc.local

#!/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.
sh '/home/robot/zzz/run.sh'
exit 0


注意修改權限才能編輯

robot@robot:/etc$ sudo chmod 777 rc.local
[sudo] password for robot:
robot@robot:/etc$ ls -l rc.local
-rwxrwxrwx 1 root root 306 2月  11  2017 rc.local
robot@robot:/etc$ sudo chmod 755 rc.local
robot@robot:/etc$ ls -l rc.local
-rwxr-xr-x 1 root root 306 2月  11  2017 rc.local
robot@robot:/etc$ sudo chmod 777 rc.local
robot@robot:/etc$


方法二,顯示終端的配置方式X-MATE-Autostart  或X-GNOME-Autostart

再創建個sh文件terminal_run.sh

#!/bin/sh
sleep 1
gnome-terminal -x bash -c "sh /home/robot/zzz/run.sh;exec bash;"
exec bash;

 可以參考 下面進行操作

System->ControlCenter->Personal->StartupApplications添加啓動項,命令選擇terminal_run.sh

https://blog.csdn.net/mao0514/article/details/54967215各種桌面環境下設置開機自啓動應用程序的方法

這個操作實際上是在下面這個目錄添加了.desktop的配置文件

robot@robot:~/.config/autostart

autostart目錄下創建文件內容如

#!/usr/bin/env xdg-open
[Desktop Entry]
Type=Application
Exec=/home/robot/zzz/terminal_run.sh
Hidden=false
X-MATE-Autostart-enabled=true
Name[en_US]=zzz
Name=zzz
Comment[en_US]=zzz
Comment=zzz

或者

[Desktop Entry]
Type=Application
Exec=/home/robot/zzz/terminal_run.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=zzz123123
Name=zzz123123
Comment[en_US]=zzz123123
Comment=zzz123123

歸根結底這兩個是一回事,根據系統環境選擇一個咯

reboot一下

彈出了一個終端如圖

ps看看


robot@robot:~/Desktop$ ps xa
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:06 /sbin/init splash
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:00 [ksoftirqd/0]
...
 1085 ?        Ss     0:00 /bin/sh -e /etc/rc.local start
 1096 ?        S      0:00 /usr/bin/dbus-launch --exit-with-session /usr/bin/im-
 1097 ?        S      0:00 sh /home/robot/zzz/run.sh
 1110 ?        Ss     0:00 /usr/bin/dbus-daemon --fork --print-pid 5 --print-add
 1264 ?        S      0:00 python /home/robot/zzz/zzz.py
...
 1656 ?        Sl     0:02 /usr/lib/gnome-terminal/gnome-terminal-server
 1662 pts/1    Ss+    0:00 bash -c sh /home/robot/zzz/run.sh;exec bash;
 1663 pts/1    S+     0:00 sh /home/robot/zzz/run.sh
 1666 pts/1    S+     0:00 python /home/robot/zzz/zzz.py

可見
一個是 rc.local 創建的進程,看不到終端窗口
一個是gnome-terminal創建的進程,能看到終端窗口

網上帖子很多,引用幾篇以供參考
https://ubuntuqa.com/article/1352.html
如何在啓動時運行“rc.local”?
https://blog.csdn.net/davidhzq/article/details/102725116
Ubuntu桌面啓動後自動執行指定的命令或程序的三種方法
https://blog.csdn.net/m0_37827405/article/details/86060816
Ubuntu圖形界面實現開機自動運行python程序(.py)
https://blog.csdn.net/u012899335/article/details/81158849
ubuntu開機自動打開終端並以root權限執行程序


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章