LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Running C Program in Background help (https://www.linuxquestions.org/questions/linux-newbie-8/running-c-program-in-background-help-588931/)

ibshar 10-02-2007 01:10 PM

Running C Program in Background help
 
I have recently registered for a free shell account so that i can use it to test C Programs from any Windows PC using the net. Now the problem is that i have made a server program which needs to run in the background, but when i try doing that using & its not working,the program starts running and even pressing Ctrl+Z it wont go to the background! Any idea whats wrong?

kotnik 10-02-2007 02:15 PM

Start it this way:

nohup program &

Or use screen.

ibshar 10-02-2007 02:38 PM

i am using it like this:
./programObject&

And whats screen??

chrism01 10-02-2007 07:45 PM

You need a space between the prog name and the '&'.
Using nohup as well ensures it'll keep running if you logout, otherwise it's still 'attached' to your terminal, even though it's in background mode.
IOW, do as per kotnik's example

indienick 10-03-2007 07:06 AM

Have you tried forking the program? I'm not sure if that's the proper terminology, as I'm not a C or C++ programmer. The method I'm referring to goes like this:

1. Start Program1.
2. Spawn a child process for the main algorithm.
3. The parent process dies, as it is finished, and the child process does what it's supposed to do.

ibshar 10-03-2007 11:26 AM

ok this is whats happeneing:
When i run the program with this command: ./ProgramObject<SPACE>& the program starts running on screen and does not go to the background even by pressing Ctrl+Z. And using nohup also does not make any difference.


But the program is running fine in my college PC which runs Linux. So is the problem with my Shell Account?

indienick 10-03-2007 11:41 AM

If a program produces output while it runs, it will most definitely print the output to the shell is was run from.

To get rid of the output, you need to pipe it to a different source.
Code:

$ ./ProgramObject & >> ProgramObject.log      #For debugging
$ ./ProgramObject & > /dev/null              #If you don't need the output

While I highly doubt that it may be an issue with your shell account, it shouldn't be dismissed as a culprit.

ibshar 10-04-2007 02:41 PM

Quote:

Originally Posted by indienick (Post 2911960)
If a program produces output while it runs, it will most definitely print the output to the shell is was run from.

To get rid of the output, you need to pipe it to a different source.
Code:

$ ./ProgramObject & >> ProgramObject.log      #For debugging
$ ./ProgramObject & > /dev/null              #If you don't need the output

While I highly doubt that it may be an issue with your shell account, it shouldn't be dismissed as a culprit.

Tried it, but still same problem! :(


All times are GMT -5. The time now is 03:24 AM.