I've done a fork
and and then an exec
but I don't know how to start it in the background.
Should I use an argument after the exec
? If so, which is it?
I've done a fork
and and then an exec
but I don't know how to start it in the background.
Should I use an argument after the exec
? If so, which is it?
If you simply want to background a process use daemon()
.
If you want to spawn off a process that then backgrounds itself 1st use fork()
and inside this 1st child call fork()
again letting the 2nd child call exec*()
for the process to be backgrounded. Let the initial parent wait()
for the 1st child.
As the parent of the second child (the process fork()
ed 1st) dies, the exec*()
ed process will be reaped by init
.
Note: The pattern above is sometimes referred to as "double-forking". See also here: Why fork() twice (and links from there)
Another interesting set of answers on this is here: Linux: Difference between forking twice and daemon(ise)