首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果脚本退出代码为1,那么包含shell脚本的incrond进程只能退出吗?

如果脚本退出代码为1,那么包含shell脚本的incrond进程只能退出吗?
EN

Stack Overflow用户
提问于 2018-12-11 11:15:20
回答 1查看 1.1K关注 0票数 4

配置

我在CentOS 7.6上配置了have 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 ...,同样的东西)。

因此,例如,下面有两个创建的进程。

代码语言:javascript
复制
[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正在工作),并检查生成了多少进程。

代码语言:javascript
复制
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中有两个进程,因此命令的预期结果是:

代码语言:javascript
复制
/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

实际效果

实际结果是:

代码语言:javascript
复制
/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

诊断

问题表现为僵尸过程:

代码语言:javascript
复制
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。我现在的状况是:

代码语言:javascript
复制
[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使进程保持活动,而退出1却没有?这个文件在哪里?关于如何进一步调试这一点,有什么建议吗?

更新

  • 2018-12-12:新增诊断(僵尸线程)
EN

回答 1

Stack Overflow用户

发布于 2018-12-28 13:53:16

这似乎是一个更大的问题的一部分,与Inron0.5.12 (英克隆/问题/52英克隆/问题/53)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53722957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档