Put application in background



  • Hi all,
    I'm working in Linux.
    If I need to execute a non-gui application in background I start it with:
    @
    ./myapp &
    @

    But what if I want the application to go in background itself ?



  • Hi, im working on ubuntu linux too.
    the way
    @./myapp &@
    is general and does not work only with non-gui applications, especially when you redirect you output to /dev/null so as to have a clear output, and, if in an interactive terminal to be able to access the terminal again by giving new commands.
    So, what do you actually mean 'go in background itself'?
    When you execute it to just launch and not append to it or what?
    Be more specific please :)
    yeee, my first post :)



  • I know there are some applications that starting without & character from a console go in background by itself.
    But I can be wrong... :-)



  • In no particular order:

    • fork(2), then make the parent exit(2)
    • chdir(2) to "/" or some other safe working directory
    • close(2) all inherited file descriptors, including stdin, stdout and stderr. Eventually, reopen them on /dev/null using open(2) + dup2(2)
    • become a session leader with setsid(2)
    • create a new process group with setpgid(2)
    • detach from the controlling terminal by open(2) "/dev/tty", then using ioctl(2) TIOCNOTTY on it

    Much more details are available in any Unix programming book. See f.i. Advanced Programming in the Unix Environment by R. Stevens.



  • [quote author="peppe" date="1307354561"]In no particular order:

    • fork(2), then make the parent exit(2)
    • chdir(2) to "/" or some other safe working directory
    • close(2) all inherited file descriptors, including stdin, stdout and stderr
    • become a session leader with setsid(2)
    • create a new process group with setpgid(2)
    • detach from the controlling terminal by open(2) "/dev/tty", then using ioctl(2) TIOCNOTTY on it

    Much more details are available in any Unix programming book. See f.i. Advanced Programming in the Unix Environment by R. Stevens.[/quote]

    Ok, so I think Qt can't do that... :-)

    I only used some times ago the "fork" solution.

    Thanks.



  • Give me an example.
    For example, if you want gedit (GUI) to go in background, give
    @gedit& > /dev/null 2> /dev/null@
    If a program goes itself to the background, it just launches another instance of itself with other arguments but without appending to it and it just exits.
    For example, if you run myapp and it just launches, then it could be something
    @if(!QCoreApplication::arguments.count()>1){//no arguments
    //execute the same app here but with &
    //system() executes system commands and is more C than C++ but anyways
    system("myapp --justlaunched&");
    exit(2);
    }
    if(QCoreApplication::arguments.at(1)=="--justlaunched")
    mplamplampla@

    This is certainly a way to do it, but there may be a more efficient way for it, because this way cannot be used if you don't allow multiple instances of your program.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.