我怎样才能启动一个新的过程,是不是原来进程的孩子? [英] How can I launch a new process that is NOT a child of the original process?

查看:98
本文介绍了我怎样才能启动一个新的过程,是不是原来进程的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(OSX 10.7),我们使用我们分配脚本的应用程序时某些活动的应用程序内发生的被调用。我已经指派一个bash脚本,它的被称为,问题是什么,我需要做的是执行一些命令,等待30秒,然后执行一些命令。如果我有我的bash脚本做一个休眠30为30秒,整个应用程序冻结而等待我的脚本来完成。

我试图把30秒的等待(第二组命令)到一个单独的脚本,并称之为./secondScript&安培;但应用程序仍然坐在那里30秒无所事事。我假设应用程序正在等待脚本的和所有子进程的终止。

我试过这些变化从主脚本中调用第二个脚本,他们都有同样的问题:


  • 的nohup ./secondScript&安培;

  • ((./secondScript&安培;)及)

  • (./secondScript&安培;)

  • nohup的脚本-q的/ dev / null的secondScript&安培;

我没有更改应用程序,并告诉它推出我的脚本,而不是等待它完成的能力。

我如何启动一个进程(我会preFER的过程是在一个脚本语言),使得新的进程不是当前进程的孩子?

谢谢,
克里斯

P.S。我尝试了不认命令,并没有帮助。我的主要脚本是这样的:

  [初始命令]
呼应启动第二个脚本
./secondScript&安培;
回声寻找就业机会
工作
回声睡1秒
睡眠1
回声调用不认
断绝关系
回声再次寻找工作
工作
回声主要脚本完成

和我所得到的输出是这样的:

 启动第二个脚本
找工作
[1] +运行./secondScript&放大器;
睡1秒
调用不认
再次找工作
主要脚本完成

和在此点的调用应用程序坐在那里45秒,等待secondScript完成

p.p.s

如果在主脚本的顶端,我执行PS它返回的唯一的事情就是交互式的bash会话我有一个单独的终端窗口开放的进程ID。

$ SHELL的值是/斌/ bash的

如果我执行PS -p $$它正确地告诉我,

  PID TTY TIME CMD
26884? 0:00.00 mainScript

如果我执行lsof的-p $$它给了我各种结果(我没有粘贴在这里的所有列假设他们不相关):

  FD类型名称
CWD DIR /私有的/ tmp / blahblahblah
TXT REG /斌/庆典
TXT REG / usr / lib目录/ dyld的
TXT REG /私人的/ var /分贝/ dyld的/ dyld_shared_cache_x86_64
0 PIPE
1钢管 - > 0xffff8041ea2d10
2 PIPE - > 0xFFFF的8017d21cb
3R DIR /私有的/ tmp / blahblah
4R REG /卷/ DATA / blahblah
255r REG /卷/ DATA / blahblah


解决方案

在Unix中这样做的典型方式是双叉。在bash中,你可以做到这一点。

 (休眠30安培;)

(..)创建一个子进程,而&安培; 创建了一个外孙的过程。当孩子进程死亡,孙儿过程是由init继承。


如果这不起作用,那么你的应用程序不等待子进程。

它可能在等待其他的事情包括了会议,并打开锁文件:

要创建一个新的会话,Linux有一个 setsid 。在OS X中,你也许可以做到这一点通过剧本,这亦也创造了一个新的会话:

 #Linux的:
setsid睡眠30#OS X:
nohup的脚本-q -c'睡眠30'的/ dev / null的&安培;

要找到继承文件描述符的列表,你可以使用 lsof的-p yourpid ,它将输出是这样的:

 睡觉22479用户0U CHR 136,32 0t0 35的/ dev / PTS / 32
睡觉22479用户1U CHR 136,32 0t0 35的/ dev / PTS / 32
睡觉22479用户2U CHR 136,32 0t0 35的/ dev / PTS / 32
睡觉22479用户5瓦特REG 252,0 0 1048806的/ tmp /锁文件

在这种情况下,除了标准的文件描述符0,1和2,还具有与该亲本可以等待锁文件打开的Fd 5。

要关闭FD 5,你可以使用 EXEC 5 GT;&安培; - 。如果你想锁定的文件可能是标准输入/输出/标准错误本身,你可以使用的nohup 来重定向到别的东西。

(OSX 10.7) An application we use let us assign scripts to be called when certain activities occur within the application. I have assigned a bash script and it's being called, the problem is that what I need to do is to execute a few commands, wait 30 seconds, and then execute some more commands. If I have my bash script do a "sleep 30" the entire application freezes for that 30 seconds while waiting for my script to finish.

I tried putting the 30 second wait (and the second set of commands) into a separate script and calling "./secondScript &" but the application still sits there for 30 seconds doing nothing. I assume the application is waiting for the script and all child processes to terminate.

I've tried these variations for calling the second script from within the main script, they all have the same problem:

  • nohup ./secondScript &
  • ( ( ./secondScript & ) & )
  • ( ./secondScript & )
  • nohup script -q /dev/null secondScript &

I do not have the ability to change the application and tell it to launch my script and not wait for it to complete.

How can I launch a process (I would prefer the process to be in a scripting language) such that the new process is not a child of the current process?

Thanks, Chris

p.s. I tried the "disown" command and it didn't help either. My main script looks like this:

[initial commands]
echo Launching second script
./secondScript &
echo Looking for jobs
jobs
echo Sleeping for 1 second
sleep 1
echo Calling disown
disown
echo Looking again for jobs
jobs
echo Main script complete

and what I get for output is this:

Launching second script
Looking for jobs
[1]+ Running ./secondScript &
Sleeping for 1 second
Calling disown
Looking again for jobs
Main script complete

and at this point the calling application sits there for 45 seconds, waiting for secondScript to finish.

p.p.s

If, at the top of the main script, I execute "ps" the only thing it returns is the process ID of the interactive bash session I have open in a separate terminal window.

The value of $SHELL is /bin/bash

If I execute "ps -p $$" it correctly tells me

PID   TTY TIME    CMD
26884 ??  0:00.00 mainScript

If I execute "lsof -p $$" it gives me all kinds of results (I didn't paste all the columns here assuming they aren't relevant):

FD   TYPE   NAME
cwd  DIR    /private/tmp/blahblahblah
txt  REG    /bin/bash
txt  REG    /usr/lib/dyld
txt  REG    /private/var/db/dyld/dyld_shared_cache_x86_64
0    PIPE   
1    PIPE   -> 0xffff8041ea2d10
2    PIPE   -> 0xffff 8017d21cb
3r   DIR    /private/tmp/blahblah
4r   REG    /Volumes/DATA/blahblah
255r REG    /Volumes/DATA/blahblah

解决方案

The typical way of doing this in Unix is to double fork. In bash, you can do this with

( sleep 30 & )

(..) creates a child process, and & creates a grandchild process. When the child process dies, the grandchild process is inherited by init.


If this doesn't work, then your application is not waiting for child processes.

Other things it may be waiting for include the session and open lock files:

To create a new session, Linux has a setsid. On OS X, you might be able to do it through script, which incidentally also creates a new session:

# Linux:
setsid sleep 30

# OS X:
nohup script -q -c 'sleep 30' /dev/null &

To find a list of inherited file descriptors, you can use lsof -p yourpid, which will output something like:

sleep   22479 user    0u   CHR 136,32      0t0       35 /dev/pts/32
sleep   22479 user    1u   CHR 136,32      0t0       35 /dev/pts/32
sleep   22479 user    2u   CHR 136,32      0t0       35 /dev/pts/32
sleep   22479 user    5w   REG  252,0        0  1048806 /tmp/lockfile

In this case, in addition to the standard FDs 0, 1 and 2, you also have a fd 5 open with a lock file that the parent can be waiting for.

To close fd 5, you can use exec 5>&-. If you think the lock file might be stdin/stdout/stderr themselves, you can use nohup to redirect them to something else.

这篇关于我怎样才能启动一个新的过程,是不是原来进程的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆