Wednesday, January 27, 2016

Play sound with cron

- To play sound with cron we need to export display=:0 and use dbus-launch our player

- but if there's a application is accessing sound device, still cron can not produce sound

to solve this we use script  + enable vlc telnet interface

- cron rule: (of user that can access sound device , root might not be able to access sound device)
 05 23 * * * export DISPLAY=:0 && /home/user1//alarm.py &

- script save to /home/user1/alarm.py

#!/usr/bin/python
import re
import os
import pexpect

port = "14212"
sound = "/home/user1/alarm.mp3"
vlctelnetPassword = 'pass1'
portstat = os.popen("nmap localhost -p " + port).read()
if re.search("open", portstat):
    child = pexpect.spawn('telnet localhost ' + port
)
    child.expect('assword')
    child.sendline(vlctelnetPassword)
    child.expect('>')
    child.sendline('add ' + sound)
    child.sendline('quit')
else:
    os.system("dbus-launch vlc " + sound)










No comments:

Post a Comment