Python脚本计划通过Cron作业失败
上下文 - 我有一个Python脚本,读取音乐文件夹,并打开每个音乐文件一次一个VLC播放器(1首歌曲将播放,VLC将关闭,然后另一个将播放等等等等)。当我从IDE或Terminal执行python脚本时,脚本成功运行。但是,当我通过Cron作业执行它时,它会失败。Python脚本计划通过Cron作业失败
Python脚本 - 请注意,我已禁用循环测试目的。
import os,subprocess
my_path = '/home/tushar/Music/Devotional/'
songs_list = os.listdir(my_path)
song_str = ''
#for song in songs_list:
#subprocess.run(["vlc", my_path+song])
subprocess.run(["vlc", "/home/tushar/PycharmProjects/Morning Devotional Songs/Ganesha.opus"])
crontab -e命令
47 10 * * * python3 /home/tushar/PycharmProjects/Morning\ Devotional\ Songs/main.py >> /var/log/myjob.log 2>&1
cron作业日志 -
> [000055ed64567cf8] core interface error: no suitable interface module
> [000055ed6445a148] core libvlc error: interface "globalhotkeys,none"
> initialization failed [000055ed64567cf8] dbus interface error: Failed
> to connect to the D-Bus session daemon: Unable to autolaunch a
> dbus-daemon without a $DISPLAY for X11 [000055ed64567cf8] core
> interface error: no suitable interface module [000055ed6445a148] core
> libvlc error: interface "dbus,none" initialization failed
> [000055ed6445a148] core libvlc: Running vlc with the default
> interface. Use 'cvlc' to use vlc without interface. [000055ed64567cf8]
> qt4 interface error: Could not connect to X server [000055ed64567cf8]
> skins2 interface error: cannot initialize OSFactory [000055ed64567cf8]
> [cli] lua interface: Listening on host "*console". VLC media player
> 2.2.4 Weatherwax Command Line Interface initialized. Type `help' for help.
>
> Shutting down. [000055ed64567cf8] [cli] lua interface: Requested
> shutdown. [000055ed64567cf8] [cli] lua interface error: Error loading
> script /usr/lib/vlc/lua/intf/cli.luac: lua/intf/modules/host.lua:279:
> Interrupted. [00007f977c0178c8] core stream error: cannot pre fill
> buffer
如何解决这个问题?
您的cron作业日志为您的工作失败提供了线索。
Use 'cvlc' to use vlc without interface. [000055ed64567cf8] qt4 interface error: Could not connect to X server
这意味着什么是Y X服务器没有发现,你必须不喜欢这个界面运行相同的任务:
subprocess.run(["cvlc", "/home/tushar/PycharmProjects/Morning Devotional Songs/Ganesha.opus"])
cvlc
就像VLC没有界面和命令行上。试试吧,让我们知道!
非常感谢。有效。 –
不客气。 – Boggartfly
这是因为通过cron执行的作业没有设置DISPLAY环境变量(换句话说,默认情况下是非GUI模式),并且无法与显示服务器通信。要么设置显示变量(hackish),要么因为错误提示使用'cvlc'。 – SuperSaiyan