[solved] No sound when bash script is launched via crontab (Debian, KDE, AppImage)

Hello,

how do You run an .AppImage application using bash script so the sound/audio works in it?

I have seen across 2 Linux distributions and 2 different apps (AppImages) that sound not worked in it and also in media player apps launched from this AppImage.

Cron runs under my username, crontab -l shows: * * * * * /path/to/script
Script header: #!/usr/bin/bash
Inside bash script I am launching like /path/to/file.AppImage &

My friend tols me:

I suspect the problem lies in the fact that the shell started by cron is not a login shell, so no parsing of /etc/profile, ~/.profile, ~/.bash_profile, etc. takes place. You are probably missing some environment set-up. Try adding -l to the shebang line to make bash run as a login shell.

I have added this line to script: echo “$(whoami)” >> $HOME/who
it shows “user” as output of “cat $HOME/who”

GNU bash, version 5.1.4(1)-release

Does sound works when you start application from the CLI instead of the cron job? Also state AppImage name.

Yes, sound is OK, when i launch LBRY_0.53.7.AppImage via command line (GUI terminal emulator) or when i launch that bash script like that. But sound fails when it is launched via cron.

Try setting the XDG_RUNTIME_DIR variable. Here is how to test it with aplay. First, download a test sound file. For example:

cd /tmp/
wget https://www2.cs.uic.edu/~i101/SoundFiles/CantinaBand3.wav

Then use aplay (or any other player installed on system). For example, add the following to your cron to play sound every minutes:

* * * * * XDG_RUNTIME_DIR=/run/user/$(id -u) /usr/bin/aplay /tmp/CantinaBand3.wav

If this works, then next add it as follows:

* * * * * XDG_RUNTIME_DIR=/run/user/$(id -u)  /path/to/script
2 Likes

Indeed. This layman unfriendly “hack” worked. After setting cronjob like that, sound/audio started playing! And not only that. It also fixed GUI issues i was seeing last like 2 years due to launching apps using crontab - not working mouse scrolling, some missing icons, missing checkboxes, launching system media player instead of MPV in qBittorrent (qt). I was asking on other forum regarding GUI issues and no one had idea about the cause and i had no idea it can be related to launching via cronjob, unfortunately. I wish developers consider making this XDB RUNTIME by default or something to prevent issues like this one.

This was a great help @nixcraft and you helped me (and future readers) several times, thank you very much.

2 Likes

Hey @postcd / @nixcraft - I never thought about it, but it is a good thread. Any idea which man contains information about $XDG_RUNTIME_DIR? I found the following https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html:

$XDG_RUNTIME_DIR defines the base directory relative to which user-specific non-essential runtime files and other file objects (such as sockets, named pipes, …) should be stored. The directory MUST be owned by the user, and he MUST be the only one having read and write access to it. Its Unix access mode MUST be 0700.

I also used, but nothing is displayed on my screen:

man -k XDG_RUNTIME_DIR