Sign up to create your own snipts, or login.

Public snipts » Neo's snipts The latest snipts from Neo.

showing 1-1 of 1 snipts
  • Run Linux command for a designated time
    #!/bin/bash                                           
    # thread - Copyright (C) 2009 Radek Suski <info@sigsiu.net>
    # license see http://www.gnu.org/licenses/lgpl-2.1.html GNU/LGPL.
    # You can use, redistribute this file and/or modify it under the terms of the GNU Lesser General Public License version 2.1
    function usage                                                                                                             
    {                                                                                                                          
            echo                                                                                                               
            echo "Starting command with time limit"                                                                            
            echo "        Usage: thread [COMMAND]... [SECONDS]...[OPTIONS]"                                                    
            echo                                                                                                               
            echo "If the command to execute consist of more then 1 word, embbed it within quotes"                              
            echo "Example: "                                                                                                   
            echo "     thread "sleep 25" 5 "                                                                                   
            echo "will start the sleep process for 25 seconds and kill it after 5 seconds"                                     
            echo                                                                                                               
            echo "Options"                                                                                                     
            echo " -q quiet no countdown output"                                                                               
    }                                                                                                                          
    
    function Sleep 
    {              
            clear  
            if [ $1 -gt 39 ]; then
                    steps=39      
                    step=$[$1/40] 
            else                  
                    steps=$[$1 + 1]
                    step=1         
            fi                     
            for (( c=1; c<=$1; c++ ))
            do                       
                    i=$( ps --ppid $$ | grep $3 )
                    if ! [ "$i" ]; then          
                            echo                 
                            echo                 
                            echo "$4 $3 has been already terminated - exiting"
                            echo                                              
                            echo                                              
                            exit 1                                            
                    fi                                                        
                    if [ "$2" != 1 ]; then                                    
                            echo -ne "\r ["                                   
                            Done=$[$1-$c]                                     
                            Done=$[$Done/$step]                               
                            progress=$[$steps-$Done]                          
                            for (( i=1; i<$progress; i++ ))                   
                            do                                                
                                    echo -n "="                               
                            done                                              
                            todo=$[$steps-$progress]                          
                            if [ $todo -gt $steps ] || [ $todo -lt 0 ]; then  
                                    todo=$steps                               
                            fi                                                
                            for (( i=1; i<=$todo; i++ ))                      
                            do                                                
                                    echo -n "."                               
                            done                                              
                            s=$[$1-$c]                                        
                            p=$[$c*100]                                       
                            p=$[$p/$1]                                        
                            echo -n "] $p % done. $s seconds to forced termination of "$4 $3". "
                    fi                                                                          
                    sleep 1                                                                     
            done                                                                                
    }                                                                                           
    
    for p in "$@"
    do           
            if [ "$p" == "-q" ]; then
                    quiet=1          
            fi                       
    done                             
    
    case $2 in
            *[!0-9]* )
                    echo "Time has to be a numeric value. "$2" is not all numeric" 
                    usage                                                          
                    exit 1;                                                        
            ;;                                                                     
    esac                                                                           
    
    if  [ "$1" ] && [ "$2" ] && [ "$1" != "--help" ] && [ "$1" != "--usage" ]; then
            clear                                                                  
            # start the process                                                    
            exec $1 & &>/dev/null                                                  
            clear                                                                  
            # get program name                                                     
            run=`echo $1 | awk -F " " '{ print $1 }'`                              
            # get proces id                                                        
            pid=`ps --ppid $$ | grep "$run" | awk -F " " '{ print $1 }'`           
            if [ "$quiet" != 1 ]; then                                             
                    echo "Started "$1" for $2 seconds. PID is $pid"                
                    echo                                                           
            fi                                                                     
            # go sleep
            Sleep "$2" "$quiet" "$pid" "$run"
            # after sleep if the process still running
            i=$( ps --ppid $$ | grep $pid )
            if [ "$i" ]; then
                    # display message
                    if [ "$quiet" != 1 ]; then
                            echo
                            echo
                            echo "Process $pid is still running. Killing now"
                    fi
                    # try to kill it
                    while [ "$i" ]
                    do
                            kill -9 $pid
                            i=$( ps --ppid $$ | grep $pid )
                            # if killed, output messgae
                            if ! [ "$i" ]; then
                                    # display message
                                    echo "Process $pid has been killed after $2 seconds"
                                    echo
                                    echo
                                    # exit
                                    exit 1
                            fi
                            i=$( ps --ppid $$ | grep $pid )
                    done
            fi
    else
            echo Unsupported request
            usage
    fi
    

    copy | embed

    0 comments - tagged in  posted by Neo on Jan 08, 2010 at 5:28 a.m. EST
Sign up to create your own snipts, or login.