Solve the crontab script calls mplayer play music without sound problems

Problem Description

Recently to run a timed script on deepin, function text is taken from the point redis every day and take advantage of Baidu API into voice and then play. Scripts written in Python, the general idea is to get the voice file, use the analog module pexpect Python calling mplayer player terminal. It is normal, in the terminal in run directly deepin did not come before running raspberry problem, but actually added to the crontab wrong: the script can be observed without exception executed, but the speaker has been no sound, switch to pygame also unresolved.

Solution

The direct conclusion. In the beginning of the Python script to add two lines:

Import OS 
os.environ [ ' XDG_RUNTIME_DIR ' ] = " / RUN / User / 1000 "  # where the current user uid 1000

Explore the reasons

Familiar with should know crontab, cron in the environment variable is a pit father thing, so our script usually need to manually add the required environment variables, or directly using an absolute path; and Python in the analog terminal, will inherit the run this py script shell environment, so when it is transferred from cron, environment variables Python script is basically empty, it is almost certain that the problem in it.

Eventually be able to determine the environmental variables causing the problem, thanks to https://www.kutu66.com/ubuntu/article_169775 this quiz. The key environmental variables called XDG_RUNTIME_DIR, then what is it?

In https://askubuntu.com/questions/872792/what-is-xdg-runtime-dir , the foreigner is given the answer:

"What is XDG_RUNTIME_DIR?", it is an environment variable that is set automatically when you log in. It tells any program you run where to find a user-specific directory in which it can store small temporary files. 

Based on the answers, this variable specifies the temporary file directory of the current user-specific; this environment variable in the online search, can indeed be found to cause some unusual (usually associated with a graphical interface) when it is not specified, may mplayer will also output audio write something to this folder.

Guess you like

Origin www.cnblogs.com/qjfoidnh/p/12105194.html