Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Shell脚本,用于在特定时间内以进程名称和时间作为输入终止进程_Linux_Bash_Kill Process - Fatal编程技术网

Linux Shell脚本,用于在特定时间内以进程名称和时间作为输入终止进程

Linux Shell脚本,用于在特定时间内以进程名称和时间作为输入终止进程,linux,bash,kill-process,Linux,Bash,Kill Process,我需要一个shell脚本,通过获取进程名称和时间作为输入,在特定时间后终止特定的运行进程 我使用的是centos机器,我尝试过脚本,但无法在特定时间完成终止进程 #/bin/bash 读取-p'进程:'名称 读取-p“Timecontrol:”时间 ps-ef | grep$name | awk'{print$5}' pkill-9“$name” 在特定时间内终止进程的预期输出,该输出将作为输入提供。 您可以使用cron作业在特定日期和时间终止进程 如果必须使用脚本: 注: 如果计划永远运

我需要一个shell脚本,通过获取进程名称和时间作为输入,在特定时间后终止特定的运行进程

我使用的是centos机器,我尝试过脚本,但无法在特定时间完成终止进程

#/bin/bash
读取-p'进程:'名称
读取-p“Timecontrol:”时间
ps-ef | grep$name | awk'{print$5}'
pkill-9“$name”
在特定时间内终止进程的预期输出,该输出将作为输入提供。

  • 您可以使用cron作业在特定日期和时间终止进程

  • 如果必须使用脚本:

注:

  • 如果计划永远运行此脚本,请删除
    #

脚本不会在您希望将其作为输入的特定时间终止进程,因为脚本将运行并终止,它不会等待特定时间

你可以用两种方法来挠痒痒

  • 循环,但它将再次在前台运行
  • 克朗乔
awk'{print$2}'
此返回值
PID
在ubuntu中,如果它返回
PID
则必须签入Centos,如果没有,则将其更改为
awk'{print$5}

所以你可以和我一起跑

./kill_process.sh
Process: node
Timecontrol: 00:21
使用Cron作业,您不需要花费时间,只需传递进程的名称,脚本将在指定的时间运行并终止进程

#!/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

0***/path\u to\u script/kill\u process.sh节点

这将在每天午夜终止进程

  • 使用此脚本,您可以通过提供进程名称和时间在特定时间终止正在运行的进程。
    [注:时间输入必须仅以秒为单位,即120表示2分钟]
样本输入:
./kill_process.sh
Process: node
Timecontrol: 00:21
#!/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
#!/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
Process: xxxx
Killat: 120