Bash 脚本,通过从 PID 文件中提取来终止进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11672682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:52:45  来源:igfitidea点击:

Bash Script, Kill process by pulling from PID file

linuxbashshellglassfishpid

提问by Joshua Sutton

This is what I have right now in the bash script:

这就是我现在在 bash 脚本中所拥有的:

ps aux | grep glassfish | grep domain1 | gawk '{print }' | xargs kill -9

The problem with this is that if someone else is logged in and pulling something related to glassfish, it wil pull that PID as well. Thus resulting in killing the wrong PID.

这样做的问题是,如果其他人登录并提取与 glassfish 相关的内容,它也会提取该 PID。从而导致杀死错误的PID。

So My question is how do I fix what I have to only pull the correct PID, and how do I rewrite it to pull the PID from the PID file that glassfish generates.

所以我的问题是如何解决我必须只提取正确的 PID 的问题,以及如何重写它以从 glassfish 生成的 PID 文件中提取 PID。

回答by Thor84no

Edit the script that starts glassfish and place something like echo $$ > /path/to/PID-file(this can contain ~for home directory or some other mechanism like $USERto make user specific) on the line immediately following the line starting the process. You can then kill the correct process using kill $(cat /path/to/PID-file).

编辑启动 glassfish 的脚本并在紧跟在启动进程的行之后的行中放置类似的内容echo $$ > /path/to/PID-file(这可以包含~用于主目录或某些其他机制,例如$USER使用户特定于用户)。然后,您可以使用kill $(cat /path/to/PID-file).

回答by okobaka

ps aux | grep ^$USER | grep glassfish | grep domain1 | gawk '{print }' | xargs kill -9

Below i did mistake with psswitches, so above grep should be fine.

下面我用ps开关弄错了,所以上面的 grep 应该没问题。



ah it is not working, pscould be use like this ps -ao pid,tty,comm -u $USER, this grep above should be fine ...

啊它不工作,ps可以这样使用ps -ao pid,tty,comm -u $USER,上面的这个grep应该没问题......

someone else is logged in ...

其他人已登录...

If so, add switch -u

如果是这样,添加开关 -u

ps aux -u $USER | grep glassfish | grep domain1 | gawk '{print }' | xargs kill -9

$USER is user name that will be selected and listed, by default should be already set in OS environment. Multiple users could be selected by comma ps aux -u root,$USER

$USER 是将被选择和列出的用户名,默认情况下应该已经在操作系统环境中设置。可以通过逗号选择多个用户ps aux -u root,$USER

Take a note:If there is no specific username in the system, ps will throw ERROR: User name does not exist.

Read man psfor more.

注意:如果系统中没有特定的用户名,ps会抛出ERROR:用户名不存在。

阅读man ps更多。

-u userlistSelect by effective user ID (EUID) or name. This selects the processes whose effective user name or ID is in userlist. The effective user ID describes the user whose file access permissions are used by the process (see geteuid(2)). Identical to U and --user.

-u userlist按有效用户 ID (EUID) 或名称选择。这将选择其有效用户名或 ID 在用户列表中的进程。有效用户 ID 描述进程使用其文件访问权限的用户(请参阅 geteuid(2))。与 U 和 --user 相同。