树莓派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权限执行程序


 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值