Linux Shell脚本,用于在特定时间内以进程名称和时间作为输入终止进程
我需要一个shell脚本,通过获取进程名称和时间作为输入,在特定时间后终止特定的运行进程 我使用的是centos机器,我尝试过脚本,但无法在特定时间完成终止进程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作业在特定日期和时间终止进程 如果必须使用脚本: 注: 如果计划永远运
#/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