1

I seriously don't understand how to create a simple cron job. The idea is to make mplayer play a certain file at a certain hour.

Here's my attempt:

MIN HOUR * * * /usr/bin/mplayer PATH_TO_THE_FILE > /dev/null 2>&1

It just causes X server to crash. What am I doing wrong?

Foobard
  • 23
  • cron is not meant to start programs that need X (graphics). Nor are programs started this way able to use Pulseaudio, because in their wisdom the Pulseaudio designers restricted default access to the desktop user. You can cheat in various ways, though. Do you want to play audio, or audio and video? What should happen if no user is logged on? – dirkt Jul 17 '17 at 05:34
  • @dirkt hmmm I didn't know that. So as far as I understand, it's impossible to start mplayer without cheating, right? Well it's a video file actually, but if needed, I can easily convert it into an audio. Answering the second question, I think nothing. – Foobard Jul 17 '17 at 05:50

3 Answers3

1

If you just want audio, definitely convert to a audio file first; that will save you the hassle of dealing both with Pulseaudio and X.

Options:

  • Put cron-job into the Desktop user table and run it as that user, not as root. See crontab -u. You may have to set environment variables (I didn't try). This will fail when the Desktop user is not logged on, and Pulseaudio is not started.

  • Read up on how to allow other users to use Pulseaudio, and configure it according. This will also fail when the Desktop user is not logged on.

  • Run Pulseaudio system-wide.

  • Completely disable Pulseaudio, play using ALSA, and make sure the permissions of the /dev/snd/* devices are comptable with the user/group(s) of your cron job.

Also, consider using a program that is simpler than mplayer (but pay attention to what formats it can play). E.g. mpg123, or really simple paplay (Pulseaudio, WAV only) or aplay (ALSA, WAV only).

dirkt
  • 32,309
0

Jobs launched from cron do not have a controlling tty or display. I'm going to assume that you have real numbers in place of MIN and HOUR?

Perhaps try adding the -display option.

0 15 * * * /usr/bin/mplayer -display localhost.localdomain:0 PATH_TO_THE_FILE > /dev/null 2>&1

You may have to tweak the argument for -display for your particular server.

Deathgrip
  • 2,566
0

I would suggest that you use a command line program to play the file. Something like sox:

http://sox.sourceforge.net/

which is no doubt available as a package. It includes an alias of play, which will just play the file. Note that sox only plays audio file, but you've indicated that is not a problem.

Bob Eager
  • 3,600