Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Running programs in the background

  1. May 22, 2004 #1
    does anyone know how to run a console program in the background. This would be like the DNS or DHCP service that runs on a server waiting for a request.

    thanks
     
  2. jcsd
  3. May 22, 2004 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It might help to tell us what platform you're talking about.

    - Warren
     
  4. May 22, 2004 #3
    Windows 2000
     
  5. May 22, 2004 #4

    dduardo

    User Avatar
    Staff Emeritus

    Good luck. Microsoft's console isn't the most robust thing in the world.

    Unix or Linux:

    ctrl-z the program in the console. It will display a number along with the name of the program. Then you can do "bg [number]" or "fg [number]" depending if you want the program in the background or foreground
     
  6. May 22, 2004 #5
    What I want to do is have a program wait for a keyboard event using the kbhit() function. The problem I am having is that the program will only register a keyboard event if the window the program started from is clicked upon. What I need to do is have the program detect a keyboard press reagardless of whether the windows running the program is in focus.

    I am running this through the DOS console on a windows 2000 machine.
     
  7. May 22, 2004 #6
    The only way I know of to do that is with a LowLevelKeyboardProc. You would need to use the Windows API, though.
     
  8. May 22, 2004 #7
    What about just running an application independently of the console window. For example, under unix or linux you can type 'my_prog &' with the '&' sign. If you close the window the program was launched from the program will still run. Is there a way to do this under windows.

    thanks for all the help.
     
  9. May 22, 2004 #8
    If you want an application to run in the background, you can run it as a service. That can be rather difficult to do, since writing a service involves some rather arcane API calls (at least it used to...maybe things have changed). But that is the standard way in Windows to have programs run in the background.

    I believe there are tools that will let you run an ordinary console or window app as a service. That's probably a lot easier.
     
  10. May 23, 2004 #9

    dduardo

    User Avatar
    Staff Emeritus

    Here is what I suggest. Get cygwin from here: http://www.cygwin.com/
    Make sure to install g++. write a program using the ncurses library. Here is some sample code to take ctrl-c, ctrl-d, etc as characters and you can process them as you like:

    Code (Text):

    #include <ncurses.h>

    int main( void )
    {
            int c ;

            initscr() ; // Intialize Screen
            raw() ; // Disable Line Buffering
            keypad(stdscr, TRUE) ; //Read Function Keys and Arrows
            noecho() ; // Disable printing input to screen without permission

            printw("Type any character\n") ; // Equivalent to printf
            c = getch() ; // Wait for character input
            printw("%c was pressed",c) ; // Equivalent to printf

            refresh() ; // Print information from stdscr to visible window
            getch() ; // Wait for character input
            endwin() ; // Destroy Screen
            return 0 ;
    }
     
    When you compile this piece of code with g++ make sure to do it this way:

    g++ -lncurses <filename.cpp>
     
  11. May 23, 2004 #10
    Will that work when the terminal doesn't have focus?
     
  12. May 23, 2004 #11

    dduardo

    User Avatar
    Staff Emeritus

    Of course the terminal window has to be in focus. How else do you differentiate input between multiple programs.
     
    Last edited: May 23, 2004
  13. May 23, 2004 #12
    timetraveldude asked if there was a way to catch all input regardless of whether or not his program was in focus.
     
  14. May 23, 2004 #13

    dduardo

    User Avatar
    Staff Emeritus

    Here is workaround hack:

    Setup a key-event with your desktop. This is going to differ across desktop environments. When a specific key is pressed, have it run a script that checks to see if your programming is running. If it is, bring the program to the foreground.
     
  15. May 24, 2004 #14
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Running programs in the background
Loading...