Killing a program in shell script

I followed the directions here
Free Twitter Source Code ? Twitter Database Server: Install

and created a php script that enters twitter keyword searches into a MySQL DB.

When you execute the files outlined in the above link, a script starts indefinitely.

I've noticed that the scripts will inevitably fail for one reason or another. So, I want to manually kill the script after x amount of hours while scheduling it to execute every x hours in crontab. That way it will run as long crontab runs.

What's the best way to do this?

is there a way to do this without modifying those php files? (there are two of them that have to run concurrently)

Thinking aloud, would it be possible to run the php file from a shell script, and then execute some sort of kill command in that same shell script after x amount of hours?

You mean the shell script eventually hangs, because it runs an I/O command that eventually hangs?
Then, in the shell script, define a timeout function like this:

timeout () {
to=$1
shift
perl -e "alarm $to; exec @ARGV" "$@"
}

and run the critical I/O commands like this example

timeout 300 critical_command ... # kill if running >300 seconds

The call to perl is needed to install an alarm signal handler.

No need for perl if on GNU system: GNU coreutils already contain such functionality:

man timeout

would something like this also work?

some_command some_arg1 some_arg2 &
TASK_PID=$!
sleep 15
kill $TASK_PID

Yes, it would, absolutely.

If command finishes straight away you probably dont want to sit around for 15 seconds.
Try:

some_command some_arg1 some_arg2 &
TASK_PID=$!
LOOP=1
while [ $LOOP -le 15 -a -d /proc/$TASK_PID ]
do
   sleep 1
   let LOOP=LOOP+1
done
[ -d /proc/$TASK_PID ] && kill $TASK_PID

With a 15, yes.
With bigger values, there is a chance that the background process terminated and, as the system PIDs cycle, there is another process with the old PID. And you don't want to kill that :o

If you have a shell with job control, you can use the safer kill %1 .

---------- Post updated at 04:49 AM ---------- Previous update was at 04:31 AM ----------

Unix standard shells do not have job control.
I am just thinking about another background process:

# fire the background command, cut standard connections to this shell
some_command some_arg1 some_arg2 </dev/null >/dev/null 2>&1 &
task_pid=$!
# fire a watchdog, again cut the connections
(sleep 900; kill $task_pid) </dev/null >/dev/null 2>&1 &
watchdog_pid=$!
wait $task_pid
kill $watchdog_pid