微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Crontab @reboot 和 streamlink PATH 问题

如何解决Crontab @reboot 和 streamlink PATH 问题

我正在尝试使用树莓派在启动时运行 python3 脚本。 python 脚本包含一个给我带来问题的子进程调用。我怀疑问题与 streamlink 调用的路径有关,但我无法解决

脚本:

#!/usr/bin/env python3

import subprocess

def streamlistener(streamname):
        try:
                print('Listening to stream: ',streamname)
                grepOut = subprocess.check_output(['streamlink','-p','omxplayer','-a','--timeout 20','--player-fifo','--retry-strea$
                print(grepOut.decode())
                streamlistener(streamname)
        except subprocess.CalledProcessError as e:
                print("ERRORS: ",e.output.decode())
                streamlistener(streamname)

streamlistener('STREAMNAME')

crontab(没有 sleep 命令的结果相同):

@reboot sleep 60 && sudo python3 /home/pi/Desktop/stream.py 2>&1 | logger -p user.debug -t 'stream'

我收到的错误信息:

Jun 28 17:40:39 raspBerrypi stream: Traceback (most recent call last):
Jun 28 17:40:39 raspBerrypi stream:   File "/home/pi/Desktop/stream.py",line 26,in <module>
Jun 28 17:40:39 raspBerrypi stream:     streamlistener('northernstreaming')
Jun 28 17:40:39 raspBerrypi stream:   File "/home/pi/Desktop/stream.py",line 19,in streamlistener
Jun 28 17:40:39 raspBerrypi stream:     grepOut = subprocess.check_output(['streamlink','--pla$
Jun 28 17:40:39 raspBerrypi stream:   File "/usr/lib/python3.7/subprocess.py",line 395,in check_output
Jun 28 17:40:39 raspBerrypi stream:     **kwargs).stdout
Jun 28 17:40:39 raspBerrypi stream:   File "/usr/lib/python3.7/subprocess.py",line 472,in run
Jun 28 17:40:39 raspBerrypi stream:     with Popen(*popenargs,**kwargs) as process:
Jun 28 17:40:39 raspBerrypi stream:   File "/usr/lib/python3.7/subprocess.py",line 775,in __init__
Jun 28 17:40:39 raspBerrypi stream:     restore_signals,start_new_session)
Jun 28 17:40:39 raspBerrypi stream:   File "/usr/lib/python3.7/subprocess.py",line 1522,in _execute_child
Jun 28 17:40:39 raspBerrypi stream:     raise child_exception_type(errno_num,err_msg,err_filename)
Jun 28 17:40:39 raspBerrypi stream: FileNotFoundError: [Errno 2] No such file or directory: 'streamlink': 'streamlink'

解决方法

问题与用于安装 streamlink 的 PATH pip3 有关,而不是与 subprocess.check_output 调用有关,在 bashrc 和 crontab 开始时正确设置了路径。

在bashrc中设置路径:https://linuxize.com/post/how-to-add-directory-to-path-in-linux/

在 cron 中设置路径:https://unix.stackexchange.com/questions/148133/how-to-set-crontab-path-variable

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。