using popen with background process


 
Thread Tools Search this Thread
Top Forums Programming using popen with background process
# 1  
Old 08-21-2006
using popen with background process

hi,

how to work with a background process without a controlling terminal to make use of popen or system call ?

when ever i use popen or system function call in foreground process, there is no problem with respect to that .. but when the same program is run as a background process without a controlling terminal .. am receiving SIGSEGV to the program,

when i traced the program using truss the program is receiving ENOTTY...

pt to be noted is just as a background process with a controlling terminal there is no problem, only as a background process without any controlling terminal to it causes the problem

i just want to find whether a process is running or not with the above scenario..

could somebody show me the right pointer to proceed with ?

thanks a lot in advance Smilie
# 2  
Old 08-21-2006
I'm confused. Are you trying to call popen() against an already existing process?
Or is the process being created becoming a background process with no controlling terminal?

In either case popen will not work. As you know. You can test to see if popen is still working with a valid stream. popen returns a FILE *ptr, so you can call (on Linux) isastream(fileno(ptr)) or more generally ioctl(fileno(ptr), I_CANPUT,0). These will return errors approiately. Assuming I understand what you're asking...

You may also want to check out the isatty() function.
# 3  
Old 08-21-2006
Quote:
Originally Posted by jim mcnamara
I'm confused. Are you trying to call popen() against an already existing process?
Or is the process being created becoming a background process with no controlling terminal?

In either case popen will not work. As you know. You can test to see if popen is still working with a valid stream. popen returns a FILE *ptr, so you can call (on Linux) isastream(fileno(ptr)) or more generally ioctl(fileno(ptr), I_CANPUT,0). These will return errors approiately. Assuming I understand what you're asking...

You may also want to check out the isatty() function.
this is the situation...
dedicated server program which is a background process without any controlling terminal which forks another server instance to handle client requests...
in that the program handling a client request - it checks whether a particular process is running or not..
the command string is framed it and it is passed to the popen function call and it fails there...
# 4  
Old 08-21-2006
You actually should be using sockets or pipes. popen will fail as you see. fork does not create any stream which was not already there. If there were no tty streams to start with, forking will not create new ones for you unless you deliberately open them in the new process. You will have to use another IPC mechanism. popen requires tty streams.

By creating your own pipes you will be doing exactly what popen does, except that popen connects one pipe via stdout or stdin.
# 5  
Old 08-21-2006
jim,

thanks for the reply..

i can very well understand that forking will not create new streams...

to check out whether process is running or not .. i tried out a simple code

Code:
# include<stdio.h>
int main()
{
  char ps[100];
  char bin[100]="/users/mad/net/small";
  
  sprintf(ps, "sh -c /usr/bin/ps -fu %s | /usr/bin/grep '%s' | /usr/bin/grep -cv 'grep' > two.tmp", getlogin(), bin);
  system(ps);
  system("sh -c \"compress ftplog.log\"");
  return 0;
}

i ran this with controlling terminal - no problem - worked as expected
ran this as background and without any controlling terminal - none of the operation specified in system command is working..

where am i making the mistake ?
# 6  
Old 08-21-2006
I can't try this, but you need stdin, stdout, stderr for that command line to work.
The only thing I can think of is to force creation of a new process with default streams to login on the same box. rexec, ssh, maybe telnet.

I don't know how to set up localhost authentication for ssh or remsh, but assuming it can be done try:
Code:
sprintf(ps,
"remsh localhost - l  %s /usr/bin/ps -fu %s | /usr/bin/grep '%s' | /usr/bin/grep -cv 'grep' > two.tmp", getlogin(), getlogin(), bin);

# 7  
Old 08-22-2006
jim, thanking u once again for the reply...

and the truth really is .. i cannot corelate what you are trying to explain...

to identify whether process is running or not why it should be complicated, a process on running on box A, to identify whether a process B running on the same box A.. why it should force a new process to be created and find out whether process is running or not ...

and i suspect the trap to be in this point .. (coloured one)
/usr/bin/ps -fu %s | /usr/bin/grep '%s' | /usr/bin/grep -cv 'grep' > two.tmp

when the redirection operator is used by a process without controlling terminal running in background, it expects a valid tty and hence throwing out ENOTTY..

could that be the problem underlying?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subprocess.popen() should write to log without waiting for process to complete

Here is my test code process = sp.Popen( + , bufsize=1, universal_newlines=True, stdout=sp.PIPE, stderr=sp.STDOUT, cwd=src_home) output, _ =... (2 Replies)
Discussion started by: ezee
2 Replies

2. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies

3. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

4. AIX

Background process

how to check the process running in background? how can i run a process in background? pls explain with commands (1 Reply)
Discussion started by: udtyuvaraj
1 Replies

5. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

6. Shell Programming and Scripting

background process

i gave a copy process in the background( to copy around 100GB) , while in progress, the session got terminated. when i relogged in and checked the destination folder the copying was in progress... how could it happen(copying) when the shell terminates??? :rolleyes: (2 Replies)
Discussion started by: vinod.thayil
2 Replies

7. Shell Programming and Scripting

background process

Hello, This has probably been answered in other forms, however I would like to confirm this statement before I use it in production. Will this syntax work for a background process? echo "nohup server_process1 >/dir1/nohup.server_process1 2>&1 &" | ksh Please advise. Thanks (1 Reply)
Discussion started by: jerardfjay
1 Replies

8. UNIX for Dummies Questions & Answers

background process

How, can I hide background process's output? (5 Replies)
Discussion started by: zylwyz
5 Replies

9. Shell Programming and Scripting

background process

Hi, In shell script when I use script1 >> filelog the echo statments of script1 gets printed in the filelog but when I try to run script in background i.e, script1 & >> filelog nothing gets printed in the filelog. Anybody knows whats going on here. thanks (3 Replies)
Discussion started by: k_oops9
3 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies
Login or Register to Ask a Question