Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
lilifenghao44
V2EX  ›  Linux

为什么将 命令 放到 sh 中执行,会被终止

  •  
  •   lilifenghao44 · Jul 16, 2016 · 4593 views
    This topic created in 3762 days ago, the information mentioned may be changed or developed.
    之前写了个脚本监测进程,并自动恢复.但是进程恢复后,进程在脚本执行完就消失了,加了 nohup 或&都不行...


    现在又遇到类似问题,ss-redir***& 放到 rc.local 中(,开机运行完后没有进程.但是确定被执行过了.手动执行一遍就一直能跑.

    这是什么鬼啊,




    root@lovit:~# lsb_release
    No LSB modules are available.

    root@lovit:~# uname -a
    Linux lovit 2.6.32-042stab116.2 #1 SMP Fri Jun 24 15:33:57 MSK 2016 i686 i686 i686 GNU/Linux

    root@lovit:~# cat /proc/version
    Linux version 2.6.32-042stab116.2 ([email protected]) (gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) ) #1 SMP Fri Jun 24 15:33:57 MSK 2016
    16 replies  •  2016-07-17 18:25:15 +08:00
    pc10201
    pc10201
    Jul 16, 2016
    1
    建议写成一个 shell 脚本,然后在 rc.local 中 bash xx.sh 这个脚本
    另外脚本中的命令最好是用全路径,环境变量也要 export 导入一下,不然会出现有些命令找不到的情况
    chzyer
    chzyer
    Jul 16, 2016
    2
    建议将 stderr 和 stdout 都重定向到文件,这样就可以看 log 了,有了 log 就能知道为什么出错了,一般是环境不一致的问题
    kaneg
    kaneg
    Jul 16, 2016❤️1
    3
    用 lsb_release -a
    billlee
    billlee
    Jul 16, 2016❤️1
    4
    如果有 init 是 systemd 或者 upstart, 建议还是不要用 rc.local. Sys V init 的环境控制和日志输出都比较麻烦,难调试
    matthewgao
    matthewgao
    Jul 16, 2016❤️1
    5
    你监控的那个 Daemon 有没有 fork 两次,听起来可能是 session leader 的事情,你用脚本运行的 session leader 是你的脚本的 shell , session leader 退出会导致这个 session 下的进程全部退出
    lilifenghao44
    lilifenghao44
    OP
    Jul 17, 2016
    6
    @matthewgao 加了&不是会被放到 pstree 的 init 下面么?不应该因为脚本关闭,而关闭被打开的进程啊.
    lilifenghao44
    lilifenghao44
    OP
    Jul 17, 2016
    7
    @billlee 那我现在的问题应该怎么解决呢..
    matthewgao
    matthewgao
    Jul 17, 2016❤️1
    8
    @lilifenghao44 &只是放在后台,但是运行它的 shell 是它的父进程,并且父进程是 session leader ,你试下用& 打开一个窗口的程序,然后你关掉那个 shell ,窗口会跟着一块退出的
    nohup 也只是忽略 HUP 的信号,所以也打不到你那个效果
    2026
    2026
    Jul 17, 2016❤️1
    9
    (ss-redir xxxxx &) 这样就可以了
    lilifenghao44
    lilifenghao44
    OP
    Jul 17, 2016
    10
    @matthewgao 感谢帮助,可否推荐 linux 相关书籍,我最近在学.
    lilifenghao44
    lilifenghao44
    OP
    Jul 17, 2016
    11
    @nisbme 加上(命令),在 rc.local 执行完后依旧没有进程
    matthewgao
    matthewgao
    Jul 17, 2016
    12
    @lilifenghao44
    UNIX 环境高级编程(第 3 版) [Advanced Programming in the UNIX Environment, Third Edition]
    matthewgao
    matthewgao
    Jul 17, 2016
    13
    @lilifenghao44 我隐约记得 rc.local 里应该用 exec (命令)?
    billlee
    billlee
    Jul 17, 2016
    14
    @lilifenghao44 我也不知道你的系统是 Sys V init 还是新的的 init. 你可以 man init 看以下,如果是 systemd, 我可以帮你写一个。如果是 upstart, 你要自己参考 man page 或者 /etc/init 下面的其它服务写一个(我手上没有用 upstart 的机器)。如果是 Sys V init, 我建议你使用 supervisor.
    古老的 Sys V init 确实很难用,如果是自己部署,不是需要考虑兼容发布出去给别人用的情况,我都不建议使用 Sys V init.
    billlee
    billlee
    Jul 17, 2016
    15
    @matthewgao 应该不是 exec 吧,这是调用 execve (2) 的,用了当前 shell 进程就被替换掉了
    matthewgao
    matthewgao
    Jul 17, 2016
    16
    @billlee 是的,我记不太清了,反正有些是用 exec 的,忘记 rc 里面是怎么配的了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1547 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 17:44 · PVG 01:44 · LAX 09:44 · JFK 12:44
    ♥ Do have faith in what you're doing.