如何杀死陈旧的进程
How to kill a stale process
我 运行 在 Ubuntu 服务器上进行转码,有时进程会过时并且不提供任何输出,但 ps aux
显示进程仍在 运行ning - 但过时 CPU 用法
我可以通过以下命令获得 CPU 用法(我正在使用的 ffmpeg 进程的进程 ID 为 4416:
ps aux | grep -v grep | grep 4416 | awk '{print }'
我可能会创建一个小脚本来终止特定进程,但我如何创建一个循环来检查每个 ffmpeg 进程并在其过时终止它(运行它会在之后重新启动它)?
我认为它需要执行命令来获取 CPU 使用情况两次,如果 CPU 使用情况相同,则需要使用一分钟的 cron 并终止进程。我会这样做吗?
好的,所以我们做了几个 ps aux
请求,然后我们将检查过时的进程。
#!/bin/bash
# setup, change this if undesired
workdir="/tmp/.stale_process_killing"
[ -d "$workdir" ] || mkdir "$workdir"
# get ps aux outputs, change number of iterations if desired
for ((i=1; i<5; i++)); do
# add file number to file
echo $i > "${workdir}/psaux${i}"
# add ps output to file
ps aux | grep 'ffmpeg' >> "${workdir}/psaux${i}"
# change this timeout to suit your needs
sleep 1
done
# now parse the files using awk
awk '
FNR==1 { ix = }
FNR!=1 { cpu[][ix] = }
END {
for (pid in cpu) {
j=1;
while (cpu[pid][j] == cpu[pid][j+1] && j <= ix) {
if (cpu[pid][j++] == "") {
j=1;
break;
}
}
if (j >= ix) {
system("kill " pid);
}
}
}' "${workdir}/psaux"*
包含调试打印的版本,以供安全检查。
#!/bin/bash
# setup, change this if undesired
workdir="/tmp/.stale_process_killing"
[ -d "$workdir" ] || mkdir "$workdir"
# get ps aux outputs, change number of iterations if desired
for ((i=1; i<5; i++)); do
# add file number to file
echo $i > "${workdir}/psaux${i}"
# add ps output to file
ps aux | grep 'ffmpeg' >> "${workdir}/psaux${i}"
# change this timeout to suit your needs
sleep 1
done
# now parse the files using awk
awk '
FNR==1 { ix = }
FNR!=1 { cpu[][ix] = }
END {
for (pid in cpu) {
j=1;
while (cpu[pid][j] == cpu[pid][j+1] && j <= ix) {
print cpu[pid][j]
if (cpu[pid][j++] == "") {
j=1;
break;
}
}
if (j >= ix) {
print "kill " pid;
} else {
print "no kill " pid;
}
}
}' "${workdir}/psaux"*
我 运行 在 Ubuntu 服务器上进行转码,有时进程会过时并且不提供任何输出,但 ps aux
显示进程仍在 运行ning - 但过时 CPU 用法
我可以通过以下命令获得 CPU 用法(我正在使用的 ffmpeg 进程的进程 ID 为 4416:
ps aux | grep -v grep | grep 4416 | awk '{print }'
我可能会创建一个小脚本来终止特定进程,但我如何创建一个循环来检查每个 ffmpeg 进程并在其过时终止它(运行它会在之后重新启动它)?
我认为它需要执行命令来获取 CPU 使用情况两次,如果 CPU 使用情况相同,则需要使用一分钟的 cron 并终止进程。我会这样做吗?
好的,所以我们做了几个 ps aux
请求,然后我们将检查过时的进程。
#!/bin/bash
# setup, change this if undesired
workdir="/tmp/.stale_process_killing"
[ -d "$workdir" ] || mkdir "$workdir"
# get ps aux outputs, change number of iterations if desired
for ((i=1; i<5; i++)); do
# add file number to file
echo $i > "${workdir}/psaux${i}"
# add ps output to file
ps aux | grep 'ffmpeg' >> "${workdir}/psaux${i}"
# change this timeout to suit your needs
sleep 1
done
# now parse the files using awk
awk '
FNR==1 { ix = }
FNR!=1 { cpu[][ix] = }
END {
for (pid in cpu) {
j=1;
while (cpu[pid][j] == cpu[pid][j+1] && j <= ix) {
if (cpu[pid][j++] == "") {
j=1;
break;
}
}
if (j >= ix) {
system("kill " pid);
}
}
}' "${workdir}/psaux"*
包含调试打印的版本,以供安全检查。
#!/bin/bash
# setup, change this if undesired
workdir="/tmp/.stale_process_killing"
[ -d "$workdir" ] || mkdir "$workdir"
# get ps aux outputs, change number of iterations if desired
for ((i=1; i<5; i++)); do
# add file number to file
echo $i > "${workdir}/psaux${i}"
# add ps output to file
ps aux | grep 'ffmpeg' >> "${workdir}/psaux${i}"
# change this timeout to suit your needs
sleep 1
done
# now parse the files using awk
awk '
FNR==1 { ix = }
FNR!=1 { cpu[][ix] = }
END {
for (pid in cpu) {
j=1;
while (cpu[pid][j] == cpu[pid][j+1] && j <= ix) {
print cpu[pid][j]
if (cpu[pid][j++] == "") {
j=1;
break;
}
}
if (j >= ix) {
print "kill " pid;
} else {
print "no kill " pid;
}
}
}' "${workdir}/psaux"*