Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Problems w/ system calls

by GaijinPunch (Pilgrim)
on Jun 02, 2004 at 03:48 UTC ( #359119=perlquestion: print w/replies, xml ) Need Help??

GaijinPunch has asked for the wisdom of the Perl Monks concerning the following question:

I've got a script that kicks off an app for a user. The developer likes to release things in "useable by sys admins, and not users" state, so I have to make it useable for the user.

The program is basically in two parts. A server and a GUI. The script takes arguments, and based on these arguments, will export some environment variables, kick off the server, then kick off the GUI.

Up until recently, this worked fine. However, I now need to run two back ends, and two GUI's. They communicate on dfiferent ports, and if I start everything up manually from the terminal, there is no problem. However, if I run the script twice, the second instance will kill the first instance. What I think is happening, is the 2nd server process is killing the first (and in turn, the GUI).

Since I can start two servers and two GUI's manually, all fingers are pointing to my script. I don't know too much about kicking off multiple system calls from a single perl script (like if it's a good or bad idea) so I'm a bit at a loss. Any help is appreciated.

Here's a very dumbed down version of the code. It should suffice.

#!/usr/bin/perl use strict; use warnings; my $server = $ARGV[0]; my $client = $ARGV[1]; my $display = $ARGV[2]; my $variable = $ARGV[3]; $ENV{"DISPLAY"} = $display . ":0.0"; my $bin = "/usr/local/bin/"; my $port = "2222" . ( $variable + 1 ); $ENV{"PORT"} = $port; #Start the server my $server_path = $bin . $server; system "$server_path &"; sleep 2; # Start up the client my $client_path = $bin . $client; system "$client_path";

Replies are listed 'Best First'.
Re: Problems w/ system calls
by Zaxo (Archbishop) on Jun 02, 2004 at 03:56 UTC

    You don't want system for this; it doesn't return until the called process exits. You can do what you're trying with fork.

    Here's one way of writing it,

    $SIG{CHLD} = 'IGNORE'; # or arrange to wait at end # Start the server my $server_path = $bin . $server; my $spid = fork; die $! unless defined $spid; exec $server_path or die $! unless $spid; # Start up the client my $client_path = $bin . $client; my $cpid = fork; die $! unless defined $cpid; exec $client_path or die $! unless $cpid;

    Update: GaijinPunch, 'Killed' is what the kernel prints when it destroys a process for violating something or other - most often seen in perl when you run the system out of memory. I think robartes suggestion of the cause as bad socket allocation is as good as any, though you should look at memory use as well. muntfish, that usage depends on exec calling a utility through the shell instead of directly. That obscures error handling and is often fragile. The code above should have more than one argument to exec (or an indirect object) just to make sure that the server and client are called directly, but I didn't want to confuse the issue with bogus arguments.

    After Compline,
    Zaxo

      Regarding system - I've always used

      system "blah &";

      (rather than fork) to start stuff in the "background". I believe this works because of the & - system recognises this as a shell character and passes the whole string to /bin/sh.

      Any comments/potential problems with this approach? (other than lack of portability, of course...)

      s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&;
      Thanks for the reply. I actually broke off the dust from my Learning Perl book, and noted the brief page on Fork. I thought that might be a solution, but figured I'd check here first.

      I've played around w/ the above code, to no avail. I simly says "Killed" in the terminal after running it. I guess I should dig around a bit more on fork and see what the issue is.
Re: Problems w/ system calls
by robartes (Priest) on Jun 02, 2004 at 09:22 UTC
    What happens if you launch the second app from the prompt with the same port as the first? Does it kill the fisrt app? If it does, I'd suggest taking a long look to make sure your second script invocation does not use the same port as the first one.

    CU
    Robartes-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://359119]
Approved by PodMaster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-09-25 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?