小编rah*_*hra的帖子

杀死在后台运行的 shell 脚本

我已经编写了一个 shell 脚本来使用 inotifyt-tools 的 inotifywait 实用程序来监视目录。我希望该脚本在后台连续运行,但我也希望能够在需要时停止它。

为了让它连续运行,我使用了while true; 像这样:

while true;
do #a set of commands that use the inotifywait utility
end
Run Code Online (Sandbox Code Playgroud)

我已将其保存在一个文件中/bin并使其可执行。为了让它在后台运行,我使用nohup <script-name> &并关闭了终端。

我不知道如何停止这个脚本。我看过的答案在这里和非常密切相关的问题在这里

更新 1: 根据下面@InfectedRoot 的回答,我已经能够使用以下策略解决我的问题。第一次使用

ps -aux | grep script_name
Run Code Online (Sandbox Code Playgroud)

并用于sudo kill -9 <pid>杀死进程。然后我不得不再次pgrep inotifywait使用sudo kill -9 <pid>返回的id。

这有效,但我认为这是一种凌乱的方法,我正在寻找更好的答案。

更新 2: 答案包括杀死 2 个进程。这很重要,因为在命令行上运行脚本会启动 2 个进程,1 个脚本本身和 2 个inotify 进程

shell bash kill inotify

18
推荐指数
1
解决办法
10万
查看次数

标签 统计

bash ×1

inotify ×1

kill ×1

shell ×1