LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script to restart again (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-restart-again-4175530249/)

GuitarWizard 01-06-2015 04:16 PM

Shell script to restart again
 
Hi,

I have an audio recording script it that needs to run up to the hour. So I'm trying to get it to start the script again once stopped (at the top of the hour) but it just records the hour and does nothing. Sometimes I even get an illegal number 08 message.

Code:

#!/bin/sh 

DAY=$(date +%d)                         
MO=$(date +%m)                           
YR=$(date +%y)                                   
SE=$(date +%S)                           
H=$(date +%H)                                                         
M=$(date +%M) 

NOW=$(date)

DURATION=$(((60 - M) * 60-SE))

PART=$(find /mnt/hgfs/Audio\ Log/ -type f -name "RBLoggerAudio ${DAY}-${MO}-${YR}\ ${H}*" | wc -l)

reset

echo "Recording for $DAY $MO $YR - $H 00"
echo "Started at $NOW"
echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"

if [ "$PART" -eq 0 ]
then
        arecord -f cd -c 1 -d$DURATION -t raw -v -v | lame -b 64 -q8 -mm -r - /mnt/hgfs/Audio\ Log/"RBLoggerAudio "$DAY-$MO-$YR\ $H"00".mp3   
else
        arecord -f cd -c 1 -d$DURATION -t raw -v -v | lame -b 64 -q8 -mm -r - /mnt/hgfs/Audio\ Log/"RBLoggerAudio "$DAY-$MO-$YR\ $H"00 - Part - "$PART.mp3
fi

pid=$(ps -opid= -C audiolog.sh)
while [ -d "/proc/$pid" ] ; do
    sleep 1
done && ./Desktop/audiolog.sh

Any sugggestion, thanks

unSpawn 01-06-2015 04:39 PM

Quote:

Originally Posted by GuitarWizard (Post 5296640)
(..) I'm trying to get it to start the script again once stopped (at the top of the hour)

Please explain how you made it achieve that? (In other words please don't just dump the script on us but help us help you.)


Quote:

Originally Posted by GuitarWizard (Post 5296640)
Sometimes I even get an illegal number 08 message.

Combat that by making this change only:
Code:

#!/bin/sh

DAY=$(date +%d)
MO=$(date +%m)
YR=$(date +%y)
SE=$(date +%-S)


GuitarWizard 01-06-2015 05:10 PM

Quote:

Originally Posted by unSpawn (Post 5296652)
Please explain how you made it achieve that? (In other words please don't just dump the script on us but help us help you.)

The last bit of the code
Code:

pid=$(ps -opid= -C audiolog.sh)
while [ -d /proc/$pid ] ; do
    sleep 1
done && ./Desktop/audiolog.sh

Once finished recording the code then removes the process waits for a second to do so when done then launches the script again.

It might not work like this but it was my logic.

unSpawn 01-06-2015 06:38 PM

Ah, I see. First of all cron jobs are tasks that are run periodically. So if you want to start a job on the hour then just start it on the hour. I'd seldomly restart such a process from the same cron job. Problem is that you want to ensure the previous job has finished and decide what to do if not. Luckily process 'arecord' has its duration listed on the command line: 'arecord -f cd -c 1 -d$DURATION -t raw -v -v' (BTW ditch the "-v -v" for "-q" as it's a cron job and any stdout/stderr will be mailed unless specified otherwise. Same goes for 'lame': use "-S") so, at the top of the script, before running any new processes:
Code:

[ while `pgrep -f "arecord -f cd -c 1"` ]; do sleep 2s; done
will wait, sleep 2 seconds, before polling again until 'arecord' is finished. If OTOH you don't want to wait but kill arecord instead you simply could
Code:

pkill -15 -f "arecord -f cd -c 1" && sleep 4s; pkill -9 -f "arecord -f cd -c 1"
that is: kill "nicely" allowing it to close the process and clean up its file descriptors, wait 4 seconds then kill it before commencing. Sounds OK to you?

GuitarWizard 01-06-2015 07:10 PM

Thanks for that. I'm not doing a cron job which would be easier but because I want to see the output displayed in terminal hence the -v -v I could only think of manually executing in terminal once and the script will restart on the hour (which is after the recording finishes). This way I can just open up terminal and straight away see it's working.

unSpawn 01-06-2015 07:51 PM

You could turn the script into a cron job and use 'logger' statements to tell you about progress:
Code:

_retVal() { case $? in 0) RETVAL="OK";; *) RETVAL="EPIC FAIL";; esac; }
_logLine() { /usr/bin/logger -t "audiolog.cron" "${MSG}"; }

MSG="Recording for $DAY $MO $YR - $H 00"; _logLine

if [ "$PART" -eq 0 ]; then
        TARGET="/mnt/hgfs/Audio Log/RBLoggerAudio $DAY-$MO-$YR ${H}00.mp3"
        arecord -q -f cd -c 1 -d$DURATION -t raw | lame -S -b 64 -q8 -mm -r - "${TARGET}"; _retVal
        MSG="${TARGET} recorded "${RETVAL}"
else

In the above example you could
Code:

grep audiolog.cron /var/log/messages
and see what the cron job is up to.

Miati 01-06-2015 08:28 PM

Use timeout?

Code:

while true; do timeout 1h ./Desktop/audiolog.sh; done
edit:
Code:

pid=$(ps -opid= -C audiolog.sh)
while [ -d /proc/$pid ] ; do
    sleep 1
done && ./Desktop/audiolog.sh

I'm not sure you are using this correctly.. you're doing something special with the pid variable (pid=$(ps -opid= -C audiolog.sh??) but your while loop is looking for a directory. does ps -opid= -C audiolog.sh output a directory? Seems like it would output a pid file. Might be safer to do -e which just checks for a file. man bash conditional expressions for more.
Basically, while the directory /proc/$pid exists, do sleep 1, then if while completes successfully, do ./Desktop/audiolog.sh

igadoter 01-06-2015 08:38 PM

On my system that 'while' loop never ends. I would solve this by dividing your script into two parts: audiolog.sh - this contains only arecord commands and script to mange the 'audiolog.sh'. say let call this audioctl.sh. Now you put the 'while' loop into audioctl.sh. All the process will by started by running audioctl.sh only.


All times are GMT -5. The time now is 10:17 AM.