如何终止在CentOS中某个目录下运行的进程?

huangapple go评论76阅读模式
英文:

How to kill a process which is running form a directory in CentOS?

问题

我们有一个要求,我们有同一个 Spark 应用程序(基本上是一个 .jar 文件)的副本,从不同的文件夹运行。我正在寻找一个使用 shell 命令的方法,可以在不终止其他 Spark 作业的情况下终止在一个文件夹中运行的 Spark 应用程序。

例如:

/home/user/Folder1 - CapStoneSpark.jar
/home/user/Folder2 - CapStoneSpark.jar
/home/user/Folder3 - CapStoneSpark.jar

在 jar 文件中的主类是 "CapStoneSparkMain"。假设,我想要终止仅在 Folder2 中运行的 CapStoneSpark.jar,而不影响或终止从 Folder1 和 Folder3 运行的 CapStoneSpark.jar,我应该怎么做?

我已经尝试过:

kill $(ps aux | grep 'CapStoneSparkMain' | awk '{print $2}')

但是,这会终止所有具有 "CapStoneSparkMain" 类的进程。

我只想终止从单个目录启动的进程,并且不想触及从其他目录运行的进程的副本。

英文:

We have a requirement where we have copies of the same Spark application (basically a .jar file) running from different folders. I am looking for a shell command using which I can kill the Spark app running in one folder without killing other spark jobs.

Ex:

> /home/user/Folder1 - CapStoneSpark.jar
> /home/user/Folder2 - CapStoneSpark.jar
> /home/user/Folder3 - CapStoneSpark.jar

The main class in the jar file is "CapStoneSparkMain". Suppose, I want to kill the CapStoneSpark.jar running in Folder2 only without touching or killing the CapStoneSpark.jar running from Folder1 and Folder3, then what should I do?

I have already tried:

kill $(ps aux | grep 'CapStoneSparkMain' | awk '{print $2}')

But, it kills all the process which have "CapStoneSparkMain" class in them.

I only want to kill the process originating from a single directory and don't want to touch the copies of the processes running from other directories.

答案1

得分: 1

可以找到正在使用这些文件夹的所有进程ID:

lsof | grep /home/user/Folder | awk '{print "kill " $2}';

然后执行它:

lsof | grep /home/user/Folder | awk '{print "kill " $2}' | sh
英文:

You can find all Proccess ID's which using these folders:

lsof | grep /home/user/Folder | awk '{print "kill " $2}'

And execute it:

lsof | grep /home/user/Folder | awk '{print "kill " $2}'|sh

答案2

得分: 1

以下是翻译好的部分:

不清楚作业是如何启动的,但假设每个作业都在不同的工作目录中启动,那么有可能终止特定的作业。考虑到可以通过/proc/$$/cwd(指向作业文件夹的符号链接)找到每个进程的工作目录。基于上面建议的命令/循环:

kill_folder=/home/user/Folder2
for pp in $(ps aux | grep 'CapStoneSparkMain' | awk '{print $2}') ; do
    if [[ /proc/$$/cwd -ef "$kill_folder" ]]  && kill $$
done

该代码将检查符号链接/proc/$$/cwd是否与命名文件夹(kill_folder)匹配,并且只会对位于该文件夹中的进程发出终止命令。

英文:

It's not clear how the jobs are started, but assuming that each job is started with a different working direectory, it is possible kill the specific job. Given that it's possible to find the working directory of each process via the /proc/$$/cwd (symlink to the job folder). Building on the commands/loop suggested above:

kill_folder=/home/user/Folder2
for pp in $(ps aux | grep 'CapStoneSparkMain' | awk '{print $2}') ; do
    if [[ /proc/$$/cwd -ef "$kill_folder" ]]  && kill $$
done

The code will check if the symlink /proc/$$/cwd matches the named folder (kill_folder), and will only issue the kill to processes in that folder.

答案3

得分: 0

你好

一个用于检查用户的 for 循环...

for i in $(pidof application); do grep -o ${user} /proc/${i}/cmdline; done

并且如果符合你的要求,就在循环中对 ${i} 执行 kill 命令。

英文:

Hello

A for loop that check for the user in...

for i in $(pidof application); do grep -o ${user} /proc/${i}/cmdline; done

And if it fits your desire then put kill command on ${i} in loop.

huangapple
  • 本文由 发表于 2020年7月27日 21:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63116330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定