For example, system("sh /mydir/some-script.sh &")
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
executes
system
will return as soon as outer shell returns, which will be immediately after it launches the inner shell. Neither shell will wait forsome-script.sh
to finish.Yes, the shell will fork the script and return immediately, but you don't have an easy way of knowing how and whether the script has ended.
The "proper" way to run such an asynchronous command would be to
fork(2)
your process, callexecve(2)
in the child with the binary set to/bin/sh
and one of the arguments set to the name of your script, and poll the child periodically from the parent using thewaitpid(2)
system call with theWNOHANG
option. Whenwaitpid
returns -1, you know that the script has ended and you can fetch its return code.In fact, what
system(3)
does is almost the same with the only exception that the call towaitpid
blocks until the process terminates.