开机shell启动虚拟环境中的python代码

上一篇文章是开机自启动shell脚本。网上很多例子。

我的代码tt.sh

sudo -i
. /root/.virtualenvs/cv/bin/activate
python3 /home/pi/t2.py

1、进入root用户

2、启动虚拟环境,使用绝对路径。

3、运行python代码

经测试可以的运行在root用户上。

t2.py代码

import cv2
import getpass
user_name = getpass.getuser()

while 1:
	print("==========",user_name)
	print(222222222)

主要确认是不是root用户。还有就是虚拟环境有cv2这个包。看能不能到入进去。

确认是可以的。

查看开机是否启动此程序。

1、进入system文件夹中

cd  /etc/systemd/system/

2、查看tpy.service程序是否运行

sudo journalctl -f -u tpy.service

结果会打印python输出的代码

猜你喜欢

转载自blog.csdn.net/weixin_39682177/article/details/84299475