I have this script:

#!/bin/sh

echo "Start: " $(date +%s)
(time hive --hiveconf mapreduce.job.reduce.slowstart.completedmaps=0.5 --hiveconf mapred.reduce.tasks=10 --hiveconf mapreduce.reduce.shuffle.parallelcopies=15 --hiveconf hive.execution.engine=mr -f sample-queries-tpcds/query50.sql --database tpcds_text_db_1_10) 2> output/tpcds_query_2c_50_mr.out
echo "End: " $(date +%s)

How can I add some code inside it so that it will kill itself say after 5 seconds of execution? (like after 5 seconds of ./script.sh )?

share|improve this question
up vote 1 down vote accepted

Just run hive in the background, then go to sleep for 5 seconds and kill hive when sleep exits. If hive has already completed, kill will print an error message, but you can redirect it to /dev/null to ignore it.

hive --hiveconf mapreduce.job.reduce.slowstart.completedmaps=0.5 \
     --hiveconf mapred.reduce.tasks=10 \
     --hiveconf mapreduce.reduce.shuffle.parallelcopies=15 \
     --hiveconf hive.execution.engine=mr \
     -f sample-queries-tpcds/query50.sql \
     --database tpcds_text_db_1_10) \
     2> output/tpcds_query_2c_50_mr.out & hive_pid=$!

sleep 5 & wait
kill $hive_pid 2> /dev/null
share|improve this answer

This script will kill itself after 5 seconds:

#!/bin/sh
echo "Start: " $(date +%s)
sleep 5s && kill $$ &
while sleep 1; do echo Working; done
echo "End: " $(date +%s)

The pipeline sleep 5s && kill $$ is run in the background and issues a kill after 5 seconds.

Your command, which could be hive, runs in the foreground. In the example above, I used a while-loop because it allows easy demonstration that the concept works. Substituting in your hive command results in:

#!/bin/sh
echo "Start: " $(date +%s)
sleep 5s && kill $$ &
(time hive --hiveconf mapreduce.job.reduce.slowstart.completedmaps=0.5 --hiveconf mapred.reduce.tasks=10 --hiveconf mapreduce.reduce.shuffle.parallelcopies=15 --hiveconf hive.execution.engine=mr -f sample-queries-tpcds/query50.sql --database tpcds_text_db_1_10) 2> output/tpcds_query_2c_50_mr.out
echo "End: " $(date +%s)
share|improve this answer
    
adding on to add, how can I run a specific command (task) after say 5 seconds? – Mona Jalal Sep 21 '15 at 21:08
    
@MonaJalal Just replace kill $$ with whatever command you want to run. Or, do you want to both kill and run a command? If so, in what order? – John1024 Sep 21 '15 at 21:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.