2017-04-15 135 views
1

上下文 - 我有一個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 

如何解決這個問題?

+1

這是因爲通過cron執行的作業沒有設置DISPLAY環境變量(換句話說,默認情況下是非GUI模式),並且無法與顯示服務器通信。要麼設置顯示變量(hackish),要麼因爲錯誤提示使用'cvlc'。 – SuperSaiyan

回答

1

您的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沒有界面和命令行上。試試吧,讓我們知道!

+0

非常感謝。有效。 –

+0

不客氣。 – Boggartfly