简体   繁体   中英

Bash get process ID of a process started in subshell

I am looking at this question here, but it does not seem to be working for me: How to get the process id of a bash subprocess on command line

So I have a command I start in a subshell, like:

(sleep 10 &)

How can I then get the process ID of that sleep command, and wait for it?

If I do ps after none of the process IDs from the link above match what I see.

Thanks.

Parents are the only process that can properly wait for a child to finish, so you'll want to do the waiting from the subshell.

(sleep & wait)
(sleep & pid=$!; wait $pid)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM