LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-02-2007, 01:10 PM   #1
ibshar
Member
 
Registered: Aug 2006
Posts: 40

Rep: Reputation: 15
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?
 
Old 10-02-2007, 02:15 PM   #2
kotnik
Member
 
Registered: Nov 2004
Location: Novi Sad, Serbia
Distribution: Debian, Slackware, Gentoo, openSuSE
Posts: 254

Rep: Reputation: 31
Start it this way:

nohup program &

Or use screen.
 
Old 10-02-2007, 02:38 PM   #3
ibshar
Member
 
Registered: Aug 2006
Posts: 40

Original Poster
Rep: Reputation: 15
i am using it like this:
./programObject&

And whats screen??
 
Old 10-02-2007, 07:45 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 10-03-2007, 07:06 AM   #5
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 10-03-2007, 11:26 AM   #6
ibshar
Member
 
Registered: Aug 2006
Posts: 40

Original Poster
Rep: Reputation: 15
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?
 
Old 10-03-2007, 11:41 AM   #7
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.

Last edited by indienick; 10-03-2007 at 11:42 AM. Reason: Pointing fingers
 
Old 10-04-2007, 02:41 PM   #8
ibshar
Member
 
Registered: Aug 2006
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by indienick View Post
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running a program as a background daemon? Vor Kragresh Slackware 8 04-20-2007 10:48 AM
running java program at startup in background Maverick1182 Linux - Newbie 4 09-19-2006 06:09 PM
running a program in background of X using & Dachy Linux - General 1 09-13-2005 11:38 AM
running rsync from a background c program BassJunkie Programming 2 08-26-2005 05:38 AM
Running a program in the background - SSH ziggo0 Linux - Newbie 4 03-05-2005 01:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration