ubuntu16.04 python代码自启动和可执行文件自启动

1 python代码自启动

参考 https://blog.csdn.net/qq_38288618/article/details/104096606

准备好python文件 test.py

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

运行

sudo chmod 777 test.py
python3 test.py

准备run.sh 文件

#!/bin/bash
gnome-terminal -x bash -c "sleep 25;cd /home/me/robot/;python3 test.py"

注意:脚本需要实现开放执行权限 sudo chmod 777 run.sh

在搜索栏上角搜索框查找Startup Applications 点击在这里插入图片描述

在这里插入图片描述
点击add
在这里插入图片描述
Name:定义名字,test
Command:gnome-terminal -x sh /home/robot/run.sh
#其中gnome-terminal为开机打开终端。
sh /home/robot/run.sh 运行脚本。
Comment:说明,随意填写,可不填。
点击保存。

reboot测试。

可执行文件自启动

编程成的可执行文件为test
与python相似,只是自启动脚本里面启动方式为./test

#!/bin/bash
gnome-terminal -x bash -c "sleep 25;cd /home/me/robot/;./test"

遇到的坑

1 python3代码开机自启动遇到rospy错误

参考 https://blog.csdn.net/zkk9527/article/details/111353428
报错为:

 Module compiled against API version 0xa but this version of numpy is 0x9
Module compiled against API version 0xa but this version of numpy is 0x9
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
ImportError: No module named rospy

启动包含rospy的python代码前,添加source /opt/ros/kinetic/setup.bash

#!/bin/bash
gnome-terminal -x bash -c "sleep 30;source /opt/ros/kinetic/setup.bash;cd /home/robot/;python3 test.py"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值