Thread: Starting a detached process in Linux

  1. #1
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69

    Starting a detached process in Linux

    How do I start a program as a detached process in Linux?
    What I mean is I would like to call a function foo("/bin/bash") and it would start bash and then promptly return, with out waiting for bash to quit.

    -------------------------------------------------------------------
    i686-pc-linux-gnu
    Fedora 14, Gnu GCC
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could just fork() and then call your function in the child using one of the exec() functions.
    If you understand what you're doing, you're not learning anything.

  3. #3
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69
    But would that work if my program ended like this:
    Code:
    ...
    foo(argv[0]);
    return 0;
    So that my program would start up a new instance of itself and then exit, without waiting for the new process to quit.
    Ever notice how fast Windows runs?
    Neither did I.
    Which is why I switched to Linux.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Benji Wiebe View Post
    But would that work if my program ended like this:
    Well a sure way to find out Benji is to try it. But yes, the parent can exit before the child, and spawn other processes first.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting A Process
    By Nor in forum Linux Programming
    Replies: 4
    Last Post: 12-15-2008, 12:32 PM
  2. Starting/Ending a Process
    By guitarist809 in forum Windows Programming
    Replies: 14
    Last Post: 03-20-2008, 05:48 AM
  3. Starting a process with no disk file present
    By tomngi in forum Windows Programming
    Replies: 12
    Last Post: 02-05-2008, 01:20 PM
  4. starting linux programming
    By Micko in forum Linux Programming
    Replies: 11
    Last Post: 11-29-2005, 04:45 PM
  5. Linux: starting a prog autamatically w/KDE
    By MathFan in forum Tech Board
    Replies: 5
    Last Post: 06-19-2004, 05:59 PM