Running programs in the background

In summary: 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.
  • #1
timetraveldude
42
0
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
 
Computer science news on Phys.org
  • #2
It might help to tell us what platform you're talking about.

- Warren
 
  • #3
chroot said:
It might help to tell us what platform you're talking about.

- Warren
Windows 2000
 
  • #4
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
 
  • #5
dduardo said:
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
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.
 
  • #6
timetraveldude said:
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.

The only way I know of to do that is with a LowLevelKeyboardProc. You would need to use the Windows API, though.
 
  • #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.
 
  • #8
timetraveldude said:
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.

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.
 
  • #9
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:
#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>
 
  • #10
dduardo said:
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:

When you compile this piece of code with g++ make sure to do it this way:

g++ -lncurses <filename.cpp>

Will that work when the terminal doesn't have focus?
 
  • #11
Of course the terminal window has to be in focus. How else do you differentiate input between multiple programs.
 
Last edited:
  • #12
dduardo said:
Of course the terminal window has to be in focus. How else do you differentiate input between multiple programs

timetraveldude asked if there was a way to catch all input regardless of whether or not his program was in focus.
 
  • #13
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.
 
  • #14
How to make a service:
http://www.codeguru.com/Csharp/.NET/cpp_managed/windowsservices/article.php/c4877/
----------
I'm pretty sure that there is some simple way to get input that wasn't intended for your window, but I forget what it is.
 

1. What does it mean to run a program in the background?

Running a program in the background means that the program is running without being visible on your screen. This allows you to continue using your computer while the program is running.

2. How do I run a program in the background on my computer?

This can vary depending on your operating system, but typically you can run a program in the background by adding an ampersand (&) at the end of the command when launching the program in the terminal. For example: "python my_program.py &" or "my_program.exe &".

3. What are the benefits of running a program in the background?

Running a program in the background allows you to multitask and use your computer for other tasks while the program is running. It also allows you to continue using your computer without any disruptions or pauses caused by the program.

4. Can I run multiple programs in the background at the same time?

Yes, you can run multiple programs in the background at the same time. However, it is important to consider the resources of your computer and make sure it can handle running multiple programs simultaneously.

5. How do I stop a program that is running in the background?

To stop a program that is running in the background, you can use the "fg" command in the terminal to bring the program to the foreground and then use the appropriate command to stop the program. Alternatively, you can use the "kill" command with the process ID of the program to stop it.

Similar threads

Replies
19
Views
1K
Replies
7
Views
213
  • Programming and Computer Science
Replies
7
Views
455
  • Computing and Technology
Replies
6
Views
1K
  • Computing and Technology
Replies
9
Views
1K
  • Programming and Computer Science
Replies
27
Views
3K
Replies
17
Views
931
  • Computing and Technology
Replies
20
Views
685
  • Computing and Technology
Replies
9
Views
1K
  • Computing and Technology
Replies
6
Views
915
Back
Top