====== A script which can run now, or later ======
It's sometimes useful to write a script which can do something, but you then want to run it at a pre-determined time in the future. My example here is recording live television using, for example, a [[DVBSky T982]] PCIe card.
A command to record from such a card can be pretty trivial:mplayer dvb://channelname -dumpstream -dumpfile filename.mpg
Turning this into a script which will record for a specified time and then stop is not much more complicated:#!/bin/bash
# Record something from DVB for a fixed duration
# $1 is the channel to record, $2 is the duration to record for
seconds=`date +%s -d "1970-01-01 $2 +1 hour"`
now=`date +%F_%H%M%S`
mplayer dvb://"$1" -dumpstream -dumpfile ~/DVB/"$1.$now.mpg" &>/dev/null &
sleep 5
mpid=`ps ax | grep "mplayer.*$1.$now.mpg" | grep -v grep | cut -c1-5`
sleep $seconds
echo "Recorded $1.$now.mpg - now going to kill $mpid"
kill -KILL $mpid
You can't tell mplayer to record for a specified time, so you just tell it to start recording and then kill its process ID after the allotted time.
However, you then have to use **cron** or more likely **at** to set this script to run when you want to start recording. It would be nice to have the script accept a(n optional) third parameter to tell it when to start recording. If this parameter is present, it sets up an **at** job to run itself (without the extra parameter) at the specified time. If the parameter is not present, it just starts recording immediately for the specified duration.
#!/bin/bash
# Record something from DVB for a fixed duration
# $1 is the channel to record, $2 is the duration to record for, or:
# $1 is the channel to record, $2 is the date/time to start, $3 is the duration to record for
if [ -z "$2" ]
then
echo "Please supply both channel name (quoted if it contains spaces) and duration (hh:mm:ss)"
exit 1
fi
if [ -n "$3" ]
then
# We have a start time as well as a duration, so set up an at job for it
echo "$0 \"$1\" \"$3\"" | at `date +"%H:%M %F" -d "$2"`
echo "Recording $1 at `date +\"%T %F\" -d \"$2\"` for `date +%T -d \"$3\"`"
exit 0
fi
seconds=`date +%s -d "1970-01-01 $2 +1 hour"`
now=`date +%F_%H%M%S`
mplayer dvb://"$1" -dumpstream -dumpfile ~/DVB/"$1.$now.mpg" &>/dev/null &
sleep 5
mpid=`ps ax | grep "mplayer.*$1.$now.mpg" | grep -v grep | cut -c1-5`
sleep $seconds
echo "Recorded $1.$now.mpg - now going to kill $mpid"
kill -KILL $mpid
----
[[.:|Go up]]\\
Return to [[:|main index]].