skip to Main Content

I need a shell script to kill a particular running process after a specific time by getting the process name and time as input.

I’m using centos machine i’ve tried the script but couldn’t complete on killing the process on particular timing.

#!/bin/bash
read -p 'Process: ' name 
read -p 'Timecontrol: ' time

ps -ef | grep $name | awk '{print $5}'
pkill -9 "$name"

the expected output to be kill the process in specific time which will be given as input.

3

Answers


    • You can use a cron job to terminate the process in specific date and time.

    • If you have to use a script:

        #!/bin/bash
    
        read -p 'Process: ' name
        read -p 'Timecontrol: ' timecontrol
        while :
        do
          now="$(date --date="$(date +%H:%M)" +%s)"
          x="$(date --date="$timecontrol" +%s)"
          if [ $now == $x ]
          then
            ps -ef | grep $name | awk '{print $5}'
          fi
        done
    

    Note:

    • Remove # if you plan to run this script forever.
    Login or Signup to reply.
  1. The script will not kill the process at that particular time which you want to give it as an input as the script will run and die it will not wait for a specific time.

    You can tickle this in two ways.

    • Loop but again it will run in the foreground
    • Cronjob
        #!/bin/bash
        EXIT_ON_KILL=true
        read -p 'Process: ' name
        read -p 'Timecontrol: ' timecontrol        
        while :
        do
        current_time=$(date +"%T")
        echo $current_time "control "
        current_time=$(echo $current_time | cut -d':' -f1-2)    
          if [ "${timecontrol}" == "${current_time}" ]
          then
            echo "killing process"
            kill -9 $(ps -ef | grep $name | awk '{print $2}')
            if [ $? -eq 0 ]; then
                echo "Process killed having name $name and having $pid"
                if [ $EXIT_ON_KILL == true ];then
                exit 0
                fi
            else
                echo "Failed to Kill process having name $name"
                exit 1
            fi
    
          fi
        sleep 2
        done
    

    awk '{print $2}' this return PID in ubuntu, you have to check-in Centos if it returns PID if not then change it to awk '{print $5}'

    So you can run with

    ./kill_process.sh
    Process: node
    Timecontrol: 00:21
    

    With Cron job, you do not need to pass time, just pass the name of the process and the script will run on the specified time and will kill the process.

    #!/bin/bash
    EXIT_ON_KILL=true
    p_name=$1
    kill -9 $(ps -ef | grep $p_name | awk '{print $2}')
      if [ $? -eq 0 ]; then
            echo "Process killed having name $p_name and having $pid"
            if [ $EXIT_ON_KILL == true ];then
            exit 0
            fi
      else
            echo "Failed to Kill process having name $p_name"
            exit 1
      fi
    

    Create-cron-job-on-CentOS

    0 0 * * * /path_to_script/kill_process.sh node

    This will kill process every-day-at-midnight

    Login or Signup to reply.
    • With this script you can kill the running process at a specific time by giving the process name and the time.
      [Note: The input for time must be in seconds only, i.e 120 for 2 minutes]
    #!/bin/bash
    
    LOG=/tmp/kill.log
    EXIT_ON_KILL=true
    read -p 'Process: ' name
    read -p 'killat: ' time
    
    PID=$(ps -ef | grep $name | awk '{print $2}')
    ps -ef | grep $name | awk '{print $2}' &>>$LOG
    
      if [ $? -eq 0 ]; then
      echo -e "n The process details "
      ps -p $PID 
      else
        echo -e "nInvalid Process Name" 
      fi
    
    current=$(date +"%T")
    killat=$(date -d "+"$time" seconds" "+%T")
    echo -e "nCurrent time $current nThe time target is $killat"
    
    while : 
    do
    current=$(date +"%T")
    echo $current  
    
      if [ "${killat}" == "${current}" ]
      then   
        kill -9 $PID   &>>$LOG
        if [ $? -eq 0 ]; then
            echo "Process $name have been successfully killed"  
            if [ $EXIT_ON_KILL == true ];then
            exit 0
            fi
        else
            echo -e "nFailed to Kill process $name" 
            echo -e "nMay be Invalid Process Name" 
            exit 1
        fi
    
      fi
    sleep 2
    done
    

    Sample Input:

    Process: xxxx
    Killat: 120 
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search