组态

我在/etc/incron.d/example中按以下方式配置了CentOS 7.6的incrond 0.5.12:
/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为在此目录中创建的每个文件派生一个进程,但由于实际上没有太多操作,因此它会立即退出。
如果日志显示文件路径在日志文件中,则表明被入侵进程自完成工作以来就应该停止了。
默认情况下,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>

据我所能检查。我不知道该如何进一步研究。

解决方法

如果I 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 )



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

更新
  • 2018-12-12:添加了诊断(僵尸线程)
  • 最佳答案

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

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

    10-11 17:32