LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-06-2015, 05:16 PM   #1
GuitarWizard
LQ Newbie
 
Registered: Jan 2015
Posts: 3

Rep: Reputation: Disabled
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
 
Old 01-06-2015, 05:39 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
Quote:
Originally Posted by GuitarWizard View Post
(..) 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 View Post
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)
 
Old 01-06-2015, 06:10 PM   #3
GuitarWizard
LQ Newbie
 
Registered: Jan 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
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.
 
Old 01-06-2015, 07:38 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
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?
 
Old 01-06-2015, 08:10 PM   #5
GuitarWizard
LQ Newbie
 
Registered: Jan 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 01-06-2015, 08:51 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
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.
 
Old 01-06-2015, 09:28 PM   #7
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
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

Last edited by Miati; 01-06-2015 at 09:34 PM.
 
Old 01-06-2015, 09:38 PM   #8
igadoter
Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: slackware 12.2, scientific linux 6.4, knoppix 7.2, salix 14.1
Posts: 854

Rep: Reputation: 79
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.
 
  


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
Restart Apache from cgi-bin shell script url unclesamcrazy Linux - Newbie 3 04-24-2014 12:43 PM
Shell Script to Kill and Restart a process based on CPU load kalisto Programming 4 12-21-2011 06:59 AM
Shell script- how to restart applications? zx712 Programming 8 01-29-2011 05:51 PM
restart iptables from shell script hangs unitl button is pressed saiyen2002 Programming 4 11-01-2010 01:12 PM
restart the shell from within a shell script win32sux Linux - General 4 05-21-2006 05:07 AM


All times are GMT -5. The time now is 02:30 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration