Muting sound with cron

I am using the MATE desktop with an RPi4 with 8GB,

I want to automatically mute the sound at night and unmute in the morning.

I have found that the following command toggles mute/unmute when typed at the terminal:

amixer -q -D pulse sset Master toggle

However, when I try to run this command with cron, I get the following error in the log:

amixer: Mixer attach pulse error: Connection refused

I get the same error whether I run the command in the root cron or the user cron.

I use cron for backups, so I don’t think there’s an issue with the cron file.

Any ideas on what could be the problem?

Hi @JonG,

I have no idea as to the capabilities/software available on Raspberry PI, or any arm device for that matter. But have you considered trying a systemd timer?


1 Like

Thanks for the suggestion.

I created a “service”, but when I test it, the same error occurs as I described above, as for cron.
So it looks like the problem is not related to the method of launching the command (cron or systemd timer), but some difference between such an indirect launch, and simply entering the command at the terminal, which works correctly.

Just a suggestion I found here:

Give the full path of the executable. In this case, amixer:

$ which amixer

So change the command to:

/usr/bin/amixer -q -D pulse set Master toggle

And test again. I don’t know if it’ll work, :man_shrugging: but maybe it does.

Hope this helps!


According to this page:

The problem seems to be that the cron job runs without the necessary context, and adding export DISPLAY=:0 to each task is the solution:

lumpy@cheetoserver:~$ crontab -e

# At 10:15 PM every night, mute the volume
15 22 * * * export DISPLAY=:0 && /usr/bin/amixer -q set Master mute

# At 7AM every morning, unmute the volume
0 7 * * * export DISPLAY=:0 && /usr/bin/amixer -q set Master unmute

So change the command to:

DISPLAY=:0 /usr/bin/amixer -q -D pulse set Master toggle

And test again.

Thank you for these suggestions. Unfortunately none of the references solve the problem.

I agree that the problem seems to be “that the cron job runs without the necessary context”.
I’ve checked the differences between environment variables for normal running and cron, and cron has many fewer. However, none of those for normal terminal running seem directly related to sound. I’ve also tried adding the PATH values to cron, without success.

I’m stumped. :unamused:

Try changing the command to:

export DISPLAY=:0 && /usr/bin/amixer -q set Master mute

for muting it, and,

export DISPLAY=:0 && /usr/bin/amixer -q set Master unmute

for unmuting it.

I don’t know if it’ll work, but perhaps it does.

Yes, I also got to that simpler command. As before, works with the terminal, but not with cron. It gives a different error:

amixer: Unable to find simple control ‘Master’,0

This also seems to point to the “necessary context” issue.

I also tried putting the command in a script and running the script in cron. Same result.

That’s what the DISPLAY variable is for. Because, when you run it from the terminal, the variable is set, but when you run it from a cronjob, there’s no such variable because it actually happens in the background. Setting the variable just provides amixer with the correct perspective, where the command needs to be executed that doesn’t exist in a cronjob environment.

So you can just add this to the beginning of the command AFAIK:

DISPLAY=:0 ...

Yes, I tried that also…same error.

Bummer. Than I have no idea. Sorry.


Thanks for your help, anyway.

1 Like

Just came accross this. Please see

We both got to the solution via different routes. Finally, this works:

export XDG_RUNTIME_DIR=/run/user/1000 && /usr/bin/amixer -q set Master mute

I got the hint from here.

Apparently cron runs amixer from root, even when cron is run from the user, and the environment variable above is what is need for it to get the information needed from the user’s configuration.

Thanks again for your help.


This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.