V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
lilifenghao44
V2EX  ›  Linux

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

  •  
  •   lilifenghao44 · 2016-07-16 21:07:11 +08:00 · 3502 次点击
    这是一个创建于 2833 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前写了个脚本监测进程,并自动恢复.但是进程恢复后,进程在脚本执行完就消失了,加了 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 条回复    2016-07-17 18:25:15 +08:00
    pc10201
        1
    pc10201  
       2016-07-16 22:07:04 +08:00
    建议写成一个 shell 脚本,然后在 rc.local 中 bash xx.sh 这个脚本
    另外脚本中的命令最好是用全路径,环境变量也要 export 导入一下,不然会出现有些命令找不到的情况
    chzyer
        2
    chzyer  
       2016-07-16 22:47:01 +08:00
    建议将 stderr 和 stdout 都重定向到文件,这样就可以看 log 了,有了 log 就能知道为什么出错了,一般是环境不一致的问题
    kaneg
        3
    kaneg  
       2016-07-16 23:13:41 +08:00   ❤️ 1
    用 lsb_release -a
    billlee
        4
    billlee  
       2016-07-16 23:39:04 +08:00   ❤️ 1
    如果有 init 是 systemd 或者 upstart, 建议还是不要用 rc.local. Sys V init 的环境控制和日志输出都比较麻烦,难调试
    matthewgao
        5
    matthewgao  
       2016-07-16 23:39:05 +08:00   ❤️ 1
    你监控的那个 Daemon 有没有 fork 两次,听起来可能是 session leader 的事情,你用脚本运行的 session leader 是你的脚本的 shell , session leader 退出会导致这个 session 下的进程全部退出
    lilifenghao44
        6
    lilifenghao44  
    OP
       2016-07-17 12:01:20 +08:00
    @matthewgao 加了&不是会被放到 pstree 的 init 下面么?不应该因为脚本关闭,而关闭被打开的进程啊.
    lilifenghao44
        7
    lilifenghao44  
    OP
       2016-07-17 12:02:04 +08:00
    @billlee 那我现在的问题应该怎么解决呢..
    matthewgao
        8
    matthewgao  
       2016-07-17 15:18:09 +08:00   ❤️ 1
    @lilifenghao44 &只是放在后台,但是运行它的 shell 是它的父进程,并且父进程是 session leader ,你试下用& 打开一个窗口的程序,然后你关掉那个 shell ,窗口会跟着一块退出的
    nohup 也只是忽略 HUP 的信号,所以也打不到你那个效果
    nisbme
        9
    nisbme  
       2016-07-17 16:08:56 +08:00   ❤️ 1
    (ss-redir xxxxx &) 这样就可以了
    lilifenghao44
        10
    lilifenghao44  
    OP
       2016-07-17 17:11:06 +08:00
    @matthewgao 感谢帮助,可否推荐 linux 相关书籍,我最近在学.
    lilifenghao44
        11
    lilifenghao44  
    OP
       2016-07-17 17:19:31 +08:00
    @nisbme 加上(命令),在 rc.local 执行完后依旧没有进程
    matthewgao
        12
    matthewgao  
       2016-07-17 17:52:37 +08:00
    @lilifenghao44
    UNIX 环境高级编程(第 3 版) [Advanced Programming in the UNIX Environment, Third Edition]
    matthewgao
        13
    matthewgao  
       2016-07-17 17:53:55 +08:00
    @lilifenghao44 我隐约记得 rc.local 里应该用 exec (命令)?
    billlee
        14
    billlee  
       2016-07-17 18:02:48 +08:00
    @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
        15
    billlee  
       2016-07-17 18:04:06 +08:00
    @matthewgao 应该不是 exec 吧,这是调用 execve (2) 的,用了当前 shell 进程就被替换掉了
    matthewgao
        16
    matthewgao  
       2016-07-17 18:25:15 +08:00
    @billlee 是的,我记不太清了,反正有些是用 exec 的,忘记 rc 里面是怎么配的了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5279 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:50 · PVG 13:50 · LAX 22:50 · JFK 01:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.