简体   繁体   中英

Linux get pid of program launched via script with execl

I have a piece of code that launches another program as child process.

pid = execlp("path-to/program", "path-to/program", nullptr);

This returns me the pid of the process i launched so I can access it. Now we need to launch a process via a shell script. Like this:

#!/bin/bash
export LD_LIBRARY_PATH=/path-to/program
/path-to/program
#or:
wine /path-to/program.exe

C++:

pid = execlp("/path-to/shelscript.sh", "/path-to/shellscript.sh", nullptr);

When I now call execl it will return the pid of the shell, not of the launched process. I could check if the the path to the program contains ".sh" and increment the pid by one, but this will not always work. Is there a cleaner & more reliable way to do this?

If you start a process in a shell script, the variable $! contains the pid of the process just created. You could eg output this using echo and start the shell script using popen() and read the pid from there.

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