gpt4 book ai didi

linux - 仅当脚本退出代码为 1 时,带有 shell 脚本的 incrond 进程才会退出?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:27 26 4
gpt4 key购买 nike

配置

我在 CentOS 7.6 上的 incrond 0.5.12 在 /etc/incron.d/example 中配置如下:

/var/tmp/dir IN_CREATE sh/root/incron_script.sh $@/$#

我的 /root/incron_script.sh 仅包含以下内容:echo "$@">>/tmp/incrond_log.log

这意味着,当我在 var/tmp/dir 中创建文件时,文件完整路径附加到 /tmp/incrond_log.log。就是这样。

问题定义

问题基本上是,如果 incrond 配置为调用 shell 脚本,进程将被创建并且不会被停止,除非该 shell 脚本以非 0 的值退出。我正在查看的是 systemctl status incrond 的输出(或 ps aux | grep ...,同样的事情)。

所以下面,例如,有 2 个创建的进程。

[root@server ~]# systemctl status incrond
● incrond.service - Inotify System Scheduler
Loaded: loaded (/usr/lib/systemd/system/incrond.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2018-12-11 13:39:55 +03; 11min ago
Process: 16746 ExecStart=/usr/sbin/incrond (code=exited, status=0/SUCCESS)
Main PID: 16747 (incrond)
Tasks: 498
Memory: 5.9M
CGroup: /system.slice/incrond.service
├─13687 /usr/sbin/incrond
├─13747 /usr/sbin/incrond

测试

我们创建了 5 个文件,检查它们的名称是否附加到日志中(incrond 正在运行)并检查生成了多少进程。

mkdir -p /var/tmp/dir
rm -f /var/tmp/dir/*
echo -n > /tmp/incrond_log.log
systemctl restart incrond
for i in $(seq 1 5);
do
touch /var/tmp/dir/a$i.txt
sleep 0.5
tail -n1 /tmp/incrond_log.log
systemctl status incrond | grep /usr/sbin/incrond | wc -l
done

预期结果

我希望 incrond 为在此目录中创建的每个文件创建一个进程,但之后立即退出,因为实际上没有什么可做的。如果日志显示文件路径在日志文件中,这意味着 incrond 进程应该已经停止,因为它完成了它的工作。默认情况下,systemctl status incrond中有2个进程,所以该命令的预期结果是:

/var/tmp/dir/a1.txt
2
/var/tmp/dir/a2.txt
2
/var/tmp/dir/a3.txt
2
/var/tmp/dir/a4.txt
2
/var/tmp/dir/a5.txt
2

实际结果

实际结果是:

/var/tmp/dir/a1.txt
3
/var/tmp/dir/a2.txt
4
/var/tmp/dir/a3.txt
5
/var/tmp/dir/a4.txt
6
/var/tmp/dir/a5.txt
7

诊断

问题表现为僵尸进程:

root      1540  0.0  0.0  12784   224 ?        S    19:49   0:00 /usr/sbin/incrond
root 1551 0.0 0.0 12784 672 ? S 19:49 0:00 /usr/sbin/incrond
root 1553 0.0 0.0 12784 224 ? S 19:49 0:00 /usr/sbin/incrond
root 1566 0.0 0.0 12784 224 ? S 19:49 0:00 /usr/sbin/incrond
root 1576 0.0 0.0 12784 224 ? S 19:49 0:00 /usr/sbin/incrond
root 2339 0.0 0.0 12784 224 ? S 19:49 0:00 /usr/sbin/incrond
root 2348 0.0 0.0 12784 224 ? S 19:49 0:00 /usr/sbin/incrond
root 2351 0.0 0.0 12784 224 ? S 19:49 0:00 /usr/sbin/incrond
root 2355 0.0 0.0 12784 224 ? S 19:49 0:00 /usr/sbin/incrond
root 5471 0.0 0.0 0 0 ? Z 19:17 0:00 [incrond] <defunct>
root 5480 0.0 0.0 0 0 ? Z 19:17 0:00 [incrond] <defunct>
root 5483 0.0 0.0 0 0 ? Z 19:17 0:00 [incrond] <defunct>
root 5561 0.0 0.0 0 0 ? Z 19:17 0:00 [incrond] <defunct>
root 8012 0.0 0.0 0 0 ? Z 19:12 0:00 [incrond] <defunct>
root 8023 0.0 0.0 0 0 ? Z 19:12 0:00 [incrond] <defunct>
root 8025 0.0 0.0 0 0 ? Z 19:12 0:00 [incrond] <defunct>
root 8148 0.0 0.0 0 0 ? Z 19:12 0:00 [incrond] <defunct>

这是我所能检查的范围。我不知道如何进一步调查。

修复

如果我exit 1 而不是常规退出,进程将正确退出。所以我的 /root/incron_script 变成:echo "$@">>/tmp/incrond_log.log && exit 1。我的状态现在看起来像:

[root@server ~]# systemctl status incrond
● incrond.service - Inotify System Scheduler
Loaded: loaded (/usr/lib/systemd/system/incrond.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2018-12-11 14:09:04 +03; 16s ago
Process: 7882 ExecStart=/usr/sbin/incrond (code=exited, status=0/SUCCESS)
Main PID: 7888 (incrond)
Tasks: 6
Memory: 220.0K
CGroup: /system.slice/incrond.service
└─7888 /usr/sbin/incrond

Dec 11 14:09:09 server.example.com incrond[7888]: PATH (/var/tmp/dir) FILE (a1.txt) EVENT (IN_CREATE)
Dec 11 14:09:09 server.example.com incrond[7888]: (system::example) CMD (sh /root/incron_script.sh /var/tmp/dir/a1.txt )
Dec 11 14:09:10 server.example.com incrond[7888]: PATH (/var/tmp/dir) FILE (a2.txt) EVENT (IN_CREATE)
Dec 11 14:09:10 server.example.com incrond[7888]: (system::example) CMD (sh /root/incron_script.sh /var/tmp/dir/a2.txt )
Dec 11 14:09:10 server.example.com incrond[7888]: PATH (/var/tmp/dir) FILE (a3.txt) EVENT (IN_CREATE)
Dec 11 14:09:10 server.example.com incrond[7888]: (system::example) CMD (sh /root/incron_script.sh /var/tmp/dir/a3.txt )
Dec 11 14:09:11 server.example.com incrond[7888]: PATH (/var/tmp/dir) FILE (a4.txt) EVENT (IN_CREATE)
Dec 11 14:09:11 server.example.com incrond[7888]: (system::example) CMD (sh /root/incron_script.sh /var/tmp/dir/a4.txt )
Dec 11 14:09:11 server.example.com incrond[7888]: PATH (/var/tmp/dir) FILE (a5.txt) EVENT (IN_CREATE)
Dec 11 14:09:11 server.example.com incrond[7888]: (system::example) CMD (sh /root/incron_script.sh /var/tmp/dir/a5.txt )

问题

那么这是预期的行为吗?为什么 exit 0 使进程保持事件状态而 exit 1 却没有?这在哪里记录?关于如何进一步调试它有什么建议吗?

更新

  • 2018-12-12:添加诊断(僵尸线程)

最佳答案

这似乎是 incron 0.5.12 更大问题的一部分(incron/issues/52incron/issues/53)

关于linux - 仅当脚本退出代码为 1 时,带有 shell 脚本的 incrond 进程才会退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53722957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com