0

I am facing a problem running amixer shell command (in python) on crontab. If I run the amixer command on my terminal, it runs. Also works if I run the python file manually using

$ python3 myPythonScript.py

Following is the python snippet:

import subprocess
child = subprocess.Popen(["amixer", "-D", "pulse", "sget", "Master", "|", "grep", "\"[.%]\""],stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
child = child.stdout.read()
logging.info(child)

Receiving the following output in my log file (child):

none
b'ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Connection refused\n\namixer: Mixer attach pulse error: Connection refused\n'

OS: Raspbian

Also tried adding the crontab in sudo crontab -e. Same problem. Why is it breaking only on crontab? Any solutions?

furas
  • 134,197
  • 12
  • 106
  • 148
  • `crontab` can run code with different priveleges, in different folder, with different environment settings. It may need to add user to some `group` to have access to device. – furas Jul 19 '21 at 00:51
  • did you try to use directly in crontab `amixer ... | grep ... >> /full/path/to/file` (without Python)? – furas Jul 19 '21 at 00:53
  • I don't know how this code can work - you forgot `import logging`. – furas Jul 19 '21 at 00:54
  • is this really works for you? I think you should catch output from `amixer` and send it to other `Popen()` with `grep`. Piping `|` should work only with `shell=True`. – furas Jul 19 '21 at 00:58
  • I used console in Linux Mint. When I run`amixer ...` then it works. When I run it as admin `sudo amixer ...` then I get the same error as you. It shows that some users may not have access. – furas Jul 19 '21 at 01:03

0 Answers0