LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-06-2006, 09:52 AM   #1
aalex77
LQ Newbie
 
Registered: Dec 2005
Posts: 7

Rep: Reputation: 0
[c/c++] Run scripts


Hello everybody, i need to write a little program that allow me to run a linux script.

I did something like that

Code:
#include "string"
#include "iostream"
#include "fstream"

#define PIPE "/tmp/pipe"
using namespace std;

int main() 
{
    FILE *fp;
	ifstream c1;
	char *command;
	while(1)
	{
		c1.open(PIPE);
		sleep(1);
		if(c1.is_open())
		{
			command = new char[256];
			while(!c1.eof())
			{
				c1.getline(command,256,'\n');
				system(command);
			}
			sleep(1);
			c1.close();
			remove(PIPE);

		}
	}	
}
and the main program write the name of the script in a pipe file.

but when i make ps , further my process i can see two line like this
Code:
3405 root        496 S   sh -c /myfolder/myscript
 3406 root        528 S   /bin/sh /myfolder/myscript
anyone can explain me why
Regards
 
Old 05-06-2006, 11:05 AM   #2
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
You should read the command(3) man page.
Quote:
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed.
 
Old 05-06-2006, 12:36 PM   #3
aalex77
LQ Newbie
 
Registered: Dec 2005
Posts: 7

Original Poster
Rep: Reputation: 0
Well, the loop works, but the 2 process don't want to die
Maybe does exist a better way to get the goal??
Thanks
 
Old 05-06-2006, 01:51 PM   #4
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
Does your script terminate if you start it normally?

Also, don't compile and run your sample programs as root. That's too dangerous.
 
Old 05-06-2006, 02:30 PM   #5
aalex77
LQ Newbie
 
Registered: Dec 2005
Posts: 7

Original Poster
Rep: Reputation: 0
Actually is an executable file that's run on an embedded system, and if i start my script with a telnet session works pretty good.
 
Old 05-06-2006, 02:48 PM   #6
jschiwal
Moderator
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,263

Rep: Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562Reputation: 562
I'm guessing that it is because the script is running in the background and expecting input from /dev/stdin or writing output to /dev/stdout which is blocking it. So that is the reason it doesn't finish, is because it uses a command that prints and that is blocking it.
Any command that produces output should have the output redirected to /dev/null or to a file or piped to a command that uses the output.

Last edited by jschiwal; 05-06-2006 at 02:53 PM.
 
Old 05-06-2006, 02:55 PM   #7
aalex77
LQ Newbie
 
Registered: Dec 2005
Posts: 7

Original Poster
Rep: Reputation: 0
thanks, i'll try to redirect the output then!
Regards
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new scripts won't run new2me Linux - Newbie 4 06-09-2005 09:14 AM
How to run .sh scripts piggysmile Linux - Software 2 06-24-2004 06:18 AM
How do i get scripts to run? jihadcman Fedora 3 06-06-2004 10:15 AM
how do you run scripts toejam Linux - General 5 05-24-2004 08:24 AM
cannot run scripts unless su odious1 Linux - General 5 07-19-2003 12:25 AM


All times are GMT -5. The time now is 02:42 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration