Recording Internet Radio Part 2 - or - How to Automate Tasks in DSL

-- June 26, 2006 update. go to newolduser.googlepages.com to copy the scripts. There is a small problem with how they display on this page

Now that we have a nice script to start recording a radio stations
streaming broadcast it would be nice if we could have that script
start up at a predefined date and time. It would be equally nice to
be able to stop that script from recording at a predefined date and
time.

Lets work on the stopping part first.

Basically we need a script that will search for our recording script
from all the other processes that are running. Then get the Process ID
(PID) and issue a KILL command.

I think this script may do just that:


#!/bin/bash
#stopTask.sh
# This script will find all processes matching the argument characters
# it then tries to remove itself from that list and kill the remaining
tasks
#
# CAUTION !!! If you run this script with an argument of 'abc' it
will also
# find process 'abc1', 'abcdefg' etc.
#
# usage: stopTask.sh kill|echo searchString
#
# if first parm is 'kill' then any processes found will be
issued the kill command
#

scriptName="stopTask"
version="0.01"

if [ ! -z "$2" ]; then
PROCESS=$2

PIDS=`ps -ax | grep $PROCESS | sed -e "s/.*$scriptName.*//" -e "s/.*grep $PROCESS*//"|awk '{ print $1 }'`
if [ ! -z "$PIDS" ]; then
if [ $1 == "kill" ]; then
echo KILLING $PROCESS pid=$PIDS
ps -fp $PIDS
kill $PIDS
else
ps -fp $PIDS
fi
else
echo No process $PROCESS in process list
fi

else
echo USAGE : $scriptName "kill|echo searchString"
echo "Caution - unintended tasks may be killed use echo parm
first"
fi
#end-of-script

I don't want to go into this script too deeply but here's how you use
it after you save it as stopTask.sh and make it executable. In a
console you could enter "stopTask.sh echo WMUB" and it would lists all
the running tasks that have the string 'WMUB' in them. The 'echo' as
the first parameter tells the script to just list the processes, not
kill them. Try this with the recordWMUB.sh script running. In a
xshell issue the "ps -ax" command to see what's currently running.
Then issue the "stopTask.sh echo WMUB" command and you'll see
something like:

dsl@DSL22rc1:~$ stopTask.sh echo WMUB
Warning: bad syntax, perhaps a bogus '-'? See
http://procps.sf.net/faq.html
UID PID PPID C STIME TTY STAT TIME CMD
dsl 32230 32221 0 17:45 ttyp1 S 0:00 /bin/sh
./recordWMUB.sh
dsl 32232 32230 0 17:45 ttyp1 S 0:00 /bin/sh
/opt/mplayer/mplayer
dsl 32233 32232 0 17:45 ttyp1 S 0:00
/opt/mplayer/bin/mplayer -pla
dsl@DSL22rc1:~$

To actually kill the recording tasks you would enter "stopTask.sh kill
WMUB" and you'd see something like this:

dsl@DSL22rc1:~$ stopTask.sh kill WMUB
Warning: bad syntax, perhaps a bogus '-'? See
http://procps.sf.net/faq.html
KILLING WMUB pid=32230 32232 32233
UID PID PPID C STIME TTY STAT TIME CMD
dsl 32230 32221 0 17:45 ttyp1 S 0:00 /bin/sh
./recordWMUB.s
dsl 32232 32230 0 17:45 ttyp1 S 0:00 /bin/sh
/opt/mplayer/m
dsl 32233 32232 0 17:45 ttyp1 S 0:00
/opt/mplayer/bin/mplay
dsl@DSL22rc1:~$

Be very careful with this stopTask.sh script. Make sure you give it
enough characters to uniquely identify the task you want to stop.

So now we have a way to stop a process but what about automating
everything?

Many distributions of Linux contain something called Cron. It's used
for scheduling things, like run this script on this day at this time
or run this program on this day at this time. The Damn Small Linux
distribution doesn't have Cron. From what I've been told you can add
it with apt-get but I haven't done that. Instead I've written another
script that is good enough for what I need.

The script called fauxCron.sh runs in the background and every 30
seconds checks to see if something needs to be done at this time. The
list of things that need to be done are kept in /home/dsl/cronIN.txt
file. They have a YYYYmmddHHMM date-time stamp followed by the
command to be issued. So the cronIN.txt file might look like this to
start recording a broadcast from WMUB on March 31, 2006 at 10pm (2200
hours) and end it one hour later, at 2300 hours.


200603312200 recordWMUB.sh
200603312300 stopTask.sh kill WMUB
<end>

The "<end>" needs to be the last line of the cronIN.txt file. You can
enter as many date-time stamped tasks as you like. As each task is
executed by fauxCron.sh it will be logged to the file called
/home/dsl/cronLOG.txt and removed from the file cronIN.txt.

One way to run fauxCron.sh would be to open a shell and then issue the
command "fauxCron.sh" and watch it run. It produces a message each
time it wakes up and checks for work to do. This works fine and is
probably a good way to experiment with creating cronIN.txt tasks.
After you're use to how it works you can add a line to your .xinitrc
(it's a hidden file in your home directory.) to start the fauxCron.sh
script when you start your X window manager. To capture the messages
from the script the line would look something like this:


/home/dsl/fauxCron.sh >/home/dsl/cronMSG.txt &

or for no output messages use:


/home/dsl/fauxCron.sh >/dev/null &

Either way the cronIN.txt and cronLOG.txt files work the same.

Here's the actual fauxCron.sh script. Copy it and make it executable.


#!/bin/bash
version="0.03"
# fauxCron.sh for those without a cron...
#
# Required files:
#
# 1. cronIN.txt
#
# Last line contains "" in cols 1-5
#
# All other lines should have timestamp in col 1-12
# blank in col 13
# command to be
executed starting in col 14
#
#
# 2. cronLOG.txt
#
# As commands are executed they are copied to this file with the
# timestamp when they were executed put in front.
#
#
# Temporary file:
#
# 1. cronOUT.txt - temporary file that becomes cronIN.txt
#
activityThisRun="NO"
while true
do
exec 3< cronIN.txt
thisRun=`date +%Y%m%d%H%M`
echo $thisRun "fauxCron checking..."
read taskTime taskCommand > cronLOG.txt
activityThisRun="YES"
else
echo $taskTime $taskCommand >> cronOUT.txt
fi
read taskTime taskCommand > cronOUT.txt
exec 3>&-
if [ $activityThisRun == "YES" ]; then
rm -f cronIN.txt
cat cronOUT.txt > cronIN.txt
fi
rm -f cronOUT.txt
sleep 30s
done
exit 0
#end-of-script

Well... that's it. With fauxCron.sh you can automate some tasks in
DSL and with stopTask.sh you can kill tasks (and probably shoot
yourself in the foot....). I hope you can use them and get enjoyment
from them. While I've made every attempt to make sure these scripts
work properly it's important to know that they could have errors in
them that may cause data loss or worse. These scripts are not
supported by the makers of Damn Small Linux and should be run at your
own risk.

I welcome questions or comments but I don't check this blog area very
much so you might get a quicker answer by posting your question in the
forum area.

My thanks go out to everyone involved with the creation of Damn Small
Linux and the users who contribute to the DSL extentions and forums.

Good Luck,
newolduser at gmail dot com