LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-21-2019, 04:26 PM   #1
alwayskindalost
LQ Newbie
 
Registered: Dec 2019
Posts: 5

Rep: Reputation: Disabled
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
 
Old 12-21-2019, 04:50 PM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 3,088

Rep: Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866Reputation: 866
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?
 
Old 12-21-2019, 05:55 PM   #3
alwayskindalost
LQ Newbie
 
Registered: Dec 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for your reply
Quote:
Originally Posted by teckk View Post

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 View Post
Also look at the at command. Needs atd running
I will look into this.


Quote:
Originally Posted by teckk View Post
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
 
Old 12-21-2019, 11:44 PM   #4
berndbausch
LQ Guru
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 5,692

Rep: Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717
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).
 
Old 12-22-2019, 10:45 AM   #5
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Older: Coherent, MacOS, Red Hat, Big Iron IXs: AIX, Solaris, Tru64
Posts: 2,185

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
Quote:
Originally Posted by berndbausch View Post
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?)
 
Old 12-22-2019, 09:00 PM   #6
alwayskindalost
LQ Newbie
 
Registered: Dec 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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 View Post
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?
 
Old 12-22-2019, 10:35 PM   #7
berndbausch
LQ Guru
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 5,692

Rep: Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717
Quote:
Originally Posted by alwayskindalost View Post
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.
 
Old 12-23-2019, 10:56 PM   #8
alwayskindalost
LQ Newbie
 
Registered: Dec 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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 View Post
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

Last edited by alwayskindalost; 12-23-2019 at 11:09 PM.
 
Old 12-24-2019, 01:10 AM   #9
berndbausch
LQ Guru
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 5,692

Rep: Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717Reputation: 1717
Quote:
Originally Posted by alwayskindalost View Post
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.
 
Old 12-27-2019, 01:34 PM   #10
alwayskindalost
LQ Newbie
 
Registered: Dec 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sox audio recording on a VOX like basis TheGlenn88 Linux - Software 5 09-25-2018 09:57 AM
Show/stop currently running cron jobs; enable/disable jobs in various crontabs leniviy Linux - Software 1 10-27-2017 06:59 AM
sox package, Slackware 13.1, fails on sox 1.wav -e stat burbabro Slackware 1 05-04-2011 03:26 AM
recording radio using sox CrashedAgain Linux - Software 14 05-12-2008 01:43 AM
syntax with SoX & lame audio recording rotezecke Linux - Newbie 0 02-25-2008 08:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration