Bash trick of the day

David Kramer david at thekramers.net
Sun Mar 25 17:12:05 EDT 2007


Along with the MythTV thing now working, I have a need to record certain 
things from the capture card outside of MythTV (because you need to 
navigate menus to get to the On Demand stuff, etc).  I also record stuff 
from the line in connected to my Sirius radio.  What I needed was a way 
to run a command for a specific length of time, then kill it.  This is 
what I came up with:


--------------------------------------------------------------------
#!/bin/bash
COMMAND=$1
WAIT_MINUTES=$2

function usage {
	echo $1
	echo "Usage:"
	echo "${0} <command to run> <minutes to run for>"
	exit 1
}

if [[ -z "${COMMAND}" ]] ; then
	usage "No command specified"
fi

if [[ -z "${WAIT_MINUTES}" || $WAIT_MINUTES -lt 1 ]] ; then
	usage "No time specified"
fi

echo "Starting command '${COMMAND}'"
${COMMAND} &
NEWPID=$!
echo "PID is ${NEWPID}"
ps -elf | grep ${NEWPID}
date
WAIT_SECONDS=$(($WAIT_MINUTES * 60))
echo "Sleeping ${WAIT_SECONDS} seconds"
sleep ${WAIT_SECONDS}
date
echo "Killing command"
kill ${NEWPID}
echo "Done."
--------------------------------------------------------------------
Yes, I *do* put help screens on small bash scripts I write just for myself.

I can use this in conjunction with "dd if=/dev/video0 
of=mycaptured.mpeg" to capture video for a given time, for instance.

You likee?

Oh, BTW, did I ever mention the great Bash tips site 
http://bashcurescancer.com/
I think that claim is kinda overrated, but it's a good source of 
information on cool bash programming nonetheless.



-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the Discuss mailing list