Having issues with saving SoX audio recording using cron jobs
by alwayskindalost from LinuxQuestions.org on (#4WXEG)
Hi there I'm new to all of this and I'm not sure if I'm using the wrong approach. I am trying to record the audio that i pipe to SoX recorder at certain times of the day and I'm doing this using a cron job on the Raspberry PI 3b that looks like this:
10 * * * * timeout -s 2 60 sh home/pi/test.sh
test.sh works just fine if i type it into the command line manually and it saves the file. The shell script looks like this:
#!/bin/bash
[some command that plays audio]| rec /home/pi/test.wav
This script runs and saves the audio file if I run it in terminal and stop it with ctrl + C. So i assumed not using ctrl + C in cron this was the issue so I tried pkill and exit as below to terminate the script:
#!/bin/bash
timeout -s 2 50 bash -c "[some command that plays audio]| rec /home/pi/test.wav"
sudo pkill rec
exit
When i run this script in terminal it exits after the 50 seconds and terminates without me manually pressing ctrl + C and i get a saved audi file. However when i run it as a cron job i get the same issues with no audio saving.
I also tried:
#!/bin/bash
timeout -s 2 50 bash -c "[some command that plays audio]| XDG_RUNTIME_DIR=/run/user/1000 rec /home/pi/test.wav"
sudo pkill rec
exit
I saw adding XDG_RUNTIME_DIR=/run/user/1000 solved the issue for some people who used arecord. I get a file while running the cron job using the above script but it is empty and consistently 44 bytes. I even tried it using arecord and got the same issue. I have no idea what i am doing wrong.
Any help will be appreciated. Thank you


10 * * * * timeout -s 2 60 sh home/pi/test.sh
test.sh works just fine if i type it into the command line manually and it saves the file. The shell script looks like this:
#!/bin/bash
[some command that plays audio]| rec /home/pi/test.wav
This script runs and saves the audio file if I run it in terminal and stop it with ctrl + C. So i assumed not using ctrl + C in cron this was the issue so I tried pkill and exit as below to terminate the script:
#!/bin/bash
timeout -s 2 50 bash -c "[some command that plays audio]| rec /home/pi/test.wav"
sudo pkill rec
exit
When i run this script in terminal it exits after the 50 seconds and terminates without me manually pressing ctrl + C and i get a saved audi file. However when i run it as a cron job i get the same issues with no audio saving.
I also tried:
#!/bin/bash
timeout -s 2 50 bash -c "[some command that plays audio]| XDG_RUNTIME_DIR=/run/user/1000 rec /home/pi/test.wav"
sudo pkill rec
exit
I saw adding XDG_RUNTIME_DIR=/run/user/1000 solved the issue for some people who used arecord. I get a file while running the cron job using the above script but it is empty and consistently 44 bytes. I even tried it using arecord and got the same issue. I have no idea what i am doing wrong.
Any help will be appreciated. Thank you