LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Having issues with saving SoX audio recording using cron jobs (https://www.linuxquestions.org/questions/linux-newbie-8/having-issues-with-saving-sox-audio-recording-using-cron-jobs-4175666355/)

alwayskindalost 12-21-2019 03:26 PM

Having issues with saving SoX audio recording using cron jobs
 
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

teckk 12-21-2019 03:50 PM

Quote:

10 * * * * timeout -s 2 60 sh home/pi/test.sh
You are missing a /
home/pi/test.sh should be /home/pi/test.sh

Won't test.sh run by making it executable?
Code:

chmod 744 test.sh
Also look at the at command. Needs atd running

https://www.linuxquestions.org/quest...ile%27-719553/
https://unix.stackexchange.com/quest...sa-loop-device

Are you trying to capture the output of the sound device? From some executable running? Only way that you can get it?

alwayskindalost 12-21-2019 04:55 PM

Thanks for your reply
Quote:

Originally Posted by teckk (Post 6070214)

Won't test.sh run by making it executable?
Code:

chmod 744 test.sh

I made it executable already. the script runs in cron since i hear the audio playing. the issue is saving the audio file. The recording is saved when i terminate the script in command line using ctrl+C

Quote:

Originally Posted by teckk (Post 6070214)
Also look at the at command. Needs atd running

I will look into this.


Quote:

Originally Posted by teckk (Post 6070214)
Are you trying to capture the output of the sound device? From some executable running? Only way that you can get it?

I am capturing audio from the sound card of the Raspberry Pi

berndbausch 12-21-2019 10:44 PM

If something works when started manually, and doesn't work when started by cron, the first thought should be "PATH". Your interactive shell usually has a much richer PATH variable than cron.

If that is the problem in your case, the solution is to launch all commands and scripts using their absolute pathname (i.e. starting with slash).

rnturn 12-22-2019 09:45 AM

Quote:

Originally Posted by berndbausch (Post 6070287)
If something works when started manually, and doesn't work when started by cron, the first thought should be "PATH". Your interactive shell usually has a much richer PATH variable than cron.

If that is the problem in your case, the solution is to launch all commands and scripts using their absolute pathname (i.e. starting with slash).

You can place a "PATH=..." line at the top of your crontab file. Set it the same as the PATH used in an interactive session. (Though I would leave out any components in your interactive PATH that aren't needed for cron/batch/at jobs. Is your cron job going to play that FPS game?)

alwayskindalost 12-22-2019 08:00 PM

Quote:

Originally Posted by berndbausch (Post 6070287)
If that is the problem in your case, the solution is to launch all commands and scripts using their absolute pathname (i.e. starting with slash).

I tried this where I put the exact locations of the command instead of the command itself such that where I had rec it would be /usr/bin/rec. I'm still getting the same error with a blank recording



Quote:

Originally Posted by rnturn (Post 6070399)
You can place a "PATH=..." line at the top of your crontab file. Set it the same as the PATH used in an interactive session. (Though I would leave out any components in your interactive PATH that aren't needed for cron/batch/at jobs. Is your cron job going to play that FPS game?)

I'm not familiar with placing paths at the top of scripts, but do I specify multiple paths based on the commands and directories used in the script with this?

berndbausch 12-22-2019 09:35 PM

Quote:

Originally Posted by alwayskindalost (Post 6070550)
II'm still getting the same error with a blank recording

Is there an error message that you could share?

Another guess: the rec command might expect some files (input, output or intermediate files) at relative locations which don't make sense when launched by cron. Can file locations be configured?

You could also launch rec with an option like --verbose or -V4 to get debugging messages.

alwayskindalost 12-23-2019 09:56 PM

Quote:

Originally Posted by berndbausch (Post 6070560)
Is there an error message that you could share?

I get no error messages as far as I know. I just called it an error because the file is blank.
According to syslog it runs:
Dec 23 23:41:01 raspberrypi CRON[3370]: (pi) CMD (timeout -s 2 60 sh /home/pi/test.sh)

I only get a warning when running in terminal manually but that doesn't seem to be the problem since it works

/usr/bin/rec WARN alsa: can't encode 0-bit Unknown or not applicable


Quote:

Originally Posted by berndbausch (Post 6070560)
Another guess: the rec command might expect some files (input, output or intermediate files) at relative locations which don't make sense when launched by cron. Can file locations be configured?

I am unsure. The only file I'm aware of is the one I'm saving the wav file to which is in the home directory.

This is the full script i try running


Code:

#!/bin/bash

NAME=`date +%m-%d-%Y_%H-%M-%S`

timeout -s 2 50 bash -c "sudo /home/pi/rtl-sdr/build/src/rtl_fm -M wbfm -f 100.1M -g 20 |/usr/bin/play -r 32k -t raw -e s -b 16 -c 1 -V1 -  |XDG_RUNTIME_DIR=/run/user/1000 /usr/bin/rec -c 1 -r 20k /home/pi/recordings/$NAME.wav trim 0 30"

sudo pkill -2 /usr/bin/play
sudo pkill -2 /usr/bin/rec
sudo pkill -2 /home/pi/rtl-sdr/build/src/rtl_fm

I had to use a timeout for the piped commands because rtl_fm never stops and the bottom pkill commands would not execute to send a signal similar to ctrrl+C if it was not used

berndbausch 12-24-2019 12:10 AM

Quote:

Originally Posted by alwayskindalost (Post 6070832)
Code:

#!/bin/bash

NAME=`date +%m-%d-%Y_%H-%M-%S`

timeout -s 2 50 bash -c "sudo /home/pi/rtl-sdr/build/src/rtl_fm -M wbfm -f 100.1M -g 20 |/usr/bin/play -r 32k -t raw -e s -b 16 -c 1 -V1 -  |XDG_RUNTIME_DIR=/run/user/1000 /usr/bin/rec -c 1 -r 20k /home/pi/recordings/$NAME.wav trim 0 30"

sudo pkill -2 /usr/bin/play
sudo pkill -2 /usr/bin/rec
sudo pkill -2 /home/pi/rtl-sdr/build/src/rtl_fm


date, bash and sudo are called without full pathname.

Rather than using sudo in a cron script, I would run the script under root's cron table.

alwayskindalost 12-27-2019 12:34 PM

Quote:

Originally Posted by berndbausch (Post 6070849)
date, bash and sudo are called without full pathname.

Rather than using sudo in a cron script, I would run the script under root's cron table.

Thanks for your reply.
I edited the sudo cron table to run the script below:

Code:


#!/bin/bash

NAME=`/bin/date +%m-%d-%Y_%H-%M-%S`

/usr/bin/timeout -s 2 50 /bin/bash -c "/home/pi/rtl-sdr/build/src/rtl_fm -M wbfm -f 100.1M -g 20 |/usr/bin/play -r 32k -t raw -e s -b 16 -c 1 -V1 - |XDG_RUNTIME_DIR=/run/user/1000 /usr/bin/rec -c 1 -r 20k /home/pi/recordings/$NAME.wav sinc 300  silence 1 0.1 -40d 1 00:20 -40d"
/usr/bin/pkill -2 /usr/bin/play
/usr/bin/pkill -2 /usr/bin/rec
/usr/bin/pkill -2 /home/pi/rtl-sdr/build/src/rtl_fm

exit

The empty file unfortunately does not even appear in the recordings folder now. It still works in terminal. I am really unsure of what I am doing wrong

I think I will look into scheduling with the 'at' command

NicoLeBelge 10-08-2021 03:11 AM

same problem ! Still no solution ?
 
everything is in the title. If alwaysaskindalost read this message and have found a solution to his problem, I take !

ondoho 10-08-2021 04:12 AM

Quote:

Originally Posted by NicoLeBelge (Post 6290125)
everything is in the title. If alwaysaskindalost read this message and have found a solution to his problem, I take !

Did you read the detailed posts up to now?
AFAICS they already contain a solution, and more.

NicoLeBelge 10-08-2021 10:05 AM

@odoho
 
Hi Odoho, you must have vision much more efficient than mine. Last post of alwayskindalost was
The empty file unfortunately does not even appear in the recordings folder now. It still works in terminal. I am really unsure of what I am doing wrong

I think I will look into scheduling with the 'at' command
This doesn't sound as if his problem was solved ;-)
Meawhile, I moved ahead. It didn't work cause I connected a USB microphone. Once I have changed by a regular microphone, it works. I guess the computer puts the USB microphone in higher priority than the front socket for regular microphone, and that this priority is lost in the CRON, explaining why it works only in the terminal.
I tried and find out how to select the audio input in the REC command, and didn't find anything, but that's another topic...

alwayskindalost 10-08-2021 03:07 PM

Quote:

Originally Posted by NicoLeBelge (Post 6290125)
everything is in the title. If alwaysaskindalost read this message and have found a solution to his problem, I take !

Hi NicoLeBelge I ended up abandoning cron jobs and went with systemd timers where the files seemed to save properly when the script was run. Hope this helps.

NicoLeBelge 10-11-2021 03:11 AM

Quote:

Originally Posted by alwayskindalost (Post 6290302)
Hi NicoLeBelge I ended up abandoning cron jobs and went with systemd timers where the files seemed to save properly when the script was run. Hope this helps.

Hi alwayskindalost, Thanks for the tip. As long as I don't need to use USB microphone, I can live with CRON. But once it becomes no longer possible, I'll check your lead of timers.


All times are GMT -5. The time now is 08:53 PM.