Page 1 of 1

Timer for cat /dev/video0 command ?

Posted: Fri Feb 22, 2008 1:35 am
by hellonorman
I capture things manually from time to time from my dvr through my wintv 150 with the manual command

Code: Select all

cat /dev/video0 > filename.mpg
Anyone know the simplest way I could have this command run for a certain number of minutes and then terminate?

Re: Timer for cat /dev/video0 command ?

Posted: Fri Feb 22, 2008 2:50 am
by adam
you could do something like:

Code: Select all

cat /dev/video0 > filename.mpg &
sleep 100
kill $!
just replace 100 with the amount of seconds you want to run. you can also do

Code: Select all

#for 10 minutes
sleep 10m

#for 1 hour
sleep 1h

#or even 1 day
sleep 1d
and FYI, bash replaces $! with the process id of the cat command.

Re: Timer for cat /dev/video0 command ?

Posted: Fri Feb 22, 2008 10:34 am
by snarkout
I've used at for this sort of thing in the past.

Re: Timer for cat /dev/video0 command ?

Posted: Fri Feb 22, 2008 3:11 pm
by hellonorman
Hey thanks I had found this little script last night which is basically the same thing you wrote:

Code: Select all

#!/bin/bash
sleep 1h # Instead of using sleep here, you can start the script by using
# the at command, see 'man at'.

cat /dev/video0 > /home/mythtv/video/myvideo.mpg &

CAT_PID=$!
# $! is PID of last job running in background.

sleep 30m
# You should add some check here to make sure cat is still running,
# otherwise you might accidentally kill some other process.
kill $CAT_PID
I took out the beginning sleep because I didn't need it. I also added some variables so I could just pass in the filename and time:

Code: Select all

#!/bin/bash

cat /dev/video0 > ~/$1.mpg &

CAT_PID=$!
# $! is PID of last job running in background.

sleep $2m
# You should add some check here to make sure cat is still running,
# otherwise you might accidentally kill some other process.
kill $CAT_PID
I saw some at command examples but I am generally manually starting a recorded program from my dvr so it's easier to just use the script.
The comments speak of checking for the cat command so you don't kill some other process.
Would the cat PID really get reused? How would the some other process get that same pid?

Re: Timer for cat /dev/video0 command ?

Posted: Fri Feb 22, 2008 6:20 pm
by adam
In such a small script, you don't even need to store the pid in an external variable. Only when a program is run in the backgroud is its pid stored in $!. Not that it hurts to do so anyway :mrgreen:.
hellonorman wrote:The comments speak of checking for the cat command so you don't kill some other process.
Would the cat PID really get reused? How would the some other process get that same pid?
If cat dies for some reason, that pid number is freed up and can be randomly selected again. Not likely, but possible.