当前位置: 代码迷 >> 综合 >> 树莓派ubuntu MATE 16.04开机自动启动python
  详细解决方案

树莓派ubuntu MATE 16.04开机自动启动python

热度:66   发布时间:2023-12-15 02:56:59.0

树莓派ubuntu MATE 16.04开机自动启动python

备料


写个最简单的python
/home/robot/zzz/zzz.py

import time
c=1
while 1:time.sleep(1)c=c+1print(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 xaPID TTY      STAT   TIME COMMAND1 ?        Ss     0:06 /sbin/init splash2 ?        S      0:00 [kthreadd]3 ?        S      0:00 [ksoftirqd/0]
...1085 ?        Ss     0:00 /bin/sh -e /etc/rc.local start1096 ?        S      0:00 /usr/bin/dbus-launch --exit-with-session /usr/bin/im-1097 ?        S      0:00 sh /home/robot/zzz/run.sh1110 ?        Ss     0:00 /usr/bin/dbus-daemon --fork --print-pid 5 --print-add1264 ?        S      0:00 python /home/robot/zzz/zzz.py
...1656 ?        Sl     0:02 /usr/lib/gnome-terminal/gnome-terminal-server1662 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.sh1666 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权限执行程序


 

  相关解决方案