AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 994428
Accepted
fixit7
fixit7
Asked: 2018-01-11 10:33:15 +0800 CST2018-01-11 10:33:15 +0800 CST 2018-01-11 10:33:15 +0800 CST

停止 inotifywait 实例

  • 772

我制作了一个脚本来调用inotifywait. 它工作正常,但我有时想停止它。

如何停止最后一个inotifywait实例?

我无法理解如何使用inotify_rm_watch我理解的用于关闭它。

7341 ?        S      0:00 inotifywait -m /home/andy/Downloads/ --format %w
inotify
  • 4 4 个回答
  • 5515 Views

4 个回答

  • Voted
  1. Best Answer
    PerlDuck
    2018-01-11T11:00:42+08:002018-01-11T11:00:42+08:00

    您指的inotify_rm_watch是在编写“真实”程序(用 C 或类似语言)时使用的 API(C 函数),而不是脚本。所以它不适用于你的情况。

    如果您想停止inotifywait,您可以像使用任何其他程序一样执行此操作:

    • 无论是问题ps -ef | grep inotifywait,选择PID(在您的示例中大概7341),然后发送一个信号:

      kill 7341

    • 或者使用方便脚本 killall杀死所有具有给定名称的程序。killall通常默认安装。

      killall inotifywait

    • 3
  2. MaXi32
    2020-10-17T12:08:05+08:002020-10-17T12:08:05+08:00

    使用时有什么问题kill <pid>?

    您可能有多个inotifywait进程,因为其他脚本可以inotifywait独立使用。因此,使用此命令ps -ef | grep inotifywait查找正确的 PID 并不是最好的做法,因为您需要很好地假设哪个inotifywait进程属于您的脚本。因此,您最终可能会杀死错误的 PID。此外,该命令killall inotifywait比上一个命令更具侵略性。但是,如果您真的不关心其他系统正在使用 inotifywait,您可以使用积极的命令。

    我最好的方式来杀死每个实例的inotifywait进程inotifywait

    您可以创建一个文件标志来终止特定运行脚本的 inotifywait。下面的脚本是示例如何启动 inotify 脚本、停止它,甚至测试它是否针对特定的 inotify PID 运行。

    #!/bin/bash
    # inotify-test.sh
    test="changes.txt" # Monitoring file
    flag="flag.txt" # For stopping, starting event or whatever flag you need
    REPORT_FILE="report.txt"
    cat /dev/null > $REPORT_FILE
    pid="inotify-test.pid"
    cat /dev/null > $max_pid
    ARGS1="$1"
    
    function start_monitor {
    
    # If file not exist, then write default start
    if [ -f "${flag}" ]; then
            echo "start" > "${flag}"
    fi
    # Do not use -q -q (twice), because it will not output anything after do while loop
    (echo "$BASHPID" > $pid; exec inotifywait -q $flag $test -e modify -m) |
    while read file action
    do
            process_flag=$(head -1 ${flag})
            if [ "${process_flag}" == "stop" ]; then
                    echo "Process stopped" >> $REPORT_FILE
                    kill -- -$$
            elif [ "${process_flag}" == "sleep" ]; then
                    echo "Process sleep: $$" >> $REPORT_FILE
                    sleep 5
                    # Important, after sleep, must write start to start
                    echo "start" > "${flag}"
            elif [ "${process_flag}" == "start" ]; then
                    echo "Process is running" >> $REPORT_FILE
                    # Only run here if flag is start
                    echo "File: $file, action: $action, date `date`" >> $REPORT_FILE
            else
                    echo "Invalid flag $$" >> $REPORT_FILE
            fi
    done & # This symbol & is important, it will run this inotify in background
    
    }
    
    if [ "${ARGS1}" == "start" ]; then
            start_monitor
    fi
    
    if [ "${ARGS1}" == "stop" ]; then
            echo "stop" > "${flag}"
    fi
    
    if [ "${ARGS1}" == "test" ]; then
            echo "sleep" > "${flag}"
    fi
    

    您可以从日志文件中观看此操作$REPORT_FILE

    tail -f report.txt
    

    要开始监控,您可以使用:

    ./inotify-test.sh start
    

    要使用 inotifywait 是否正在运行来测试脚本,请执行以下命令:

    ./inotify-test.sh test
    

    因此,要停止 inotify 运行进程,您只需要运行相同的脚本

    ./inotify-test.sh stop
    

    使用此方法,您无需知道该 inotifywait 进程的进程 ID 是什么。

    它是如何工作的?

    您注意到我有 2 个文件$flag $test要从inotifywait命令中监视,因此如果我对$flag文件进行更改,甚至会立即触发修改,我可以利用这个机会停止inotifywait loop. 此外,您可以看到脚本实际上存储了inotifywaitat的实际 pid pid="inotify-test.pid"。因此,您可以使用这个正确的 pid 手动终止inotifywait进程。

    • 1
  3. Niru
    2019-10-25T06:22:03+08:002019-10-25T06:22:03+08:00

    这会杀死所有实例inotifywait:

    pkill inotifywait
    
    • 0
  4. ramonpie
    2019-12-13T06:44:34+08:002019-12-13T06:44:34+08:00

    使用时,inotifywait --daemon您永远不会获得进程的 ID。

    如果您像我一样不想不加选择地杀死所有 inotifywait 实例,则可以改用nohupand inotifywait --monitor。

    这是我在我的 bash 脚本中使用它的方式。它允许我不打扰其他正在运行的实例inotifywait:

    # Launch with nohup and redirect inputs and outputs
    nohup inotifywait --monitor --outfile /out/file </dev/null >/dev/null 2>&1 &
    
    # Gets process ID of last launched process.
    _INOTIFYWAIT_PID=$!
    
    # later on ...
    
    # Kill only this instance of 'inotifywait'
    kill ${_INOTIFYWAIT_PID}
    

    输入和输出重定向命令 ( </dev/null >/dev/null 2>&1 &) 来自StackOverflow 上的这个答案

    • 0

相关问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve