Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Fork exec and not wait?

by dasgar (Deacon)
on Jan 24, 2013 at 19:53 UTC ( #1015220=note: print w/ replies, xml ) Need Help??


in reply to Fork exec and not wait?

If I'm understanding you correctly, you're wanting to have your script spawn something that will go off and do some work. Then have the main script ignore what was spawned off and continue on without any delays. Is that correct?

The thought that popped into my mind was to use threads instead of forking. Just create a new thread and then detach it (or have the thread detach itself).


Comment on Re: Fork exec and not wait?
Re^2: Fork exec and not wait?
by packetstormer (Monk) on Jan 24, 2013 at 20:00 UTC

    If I'm understanding you correctly, you're wanting to have your script spawn something that will go off and do some work. Then have the main script ignore what was spawned off and continue on without any delays. Is that correct?

    This is it exactly - yes. I maybe should have said that it I am looking for something comparable to "&" in Unix. Thanks for the suggestion, I will take a look at threads.

      If you want something comparable to "&" in Unix, I recommend fork and exec, not threads. If you want sound advice from us, rather than random guessing, you will need to provide more background and context of your application and what you are trying to achieve. And you will need to post some code.

        OK, thanks for the replies. I hope my tone wasn't dismissive of anyone's suggestions, I certainly didn't mean to be and I am grateful for anyone taking the time to respond!

        Here is some code. I pass a string from a webpage and I then need to bring up an ssh tunnel in the background (I know I could do this using some existing modules instead of the system call but I stumbled across this issues and I'd like to figure it out this way!

        #!/usr/bin/perl use strict; use CGI; use Frontier::Client; use Template; my $catch = $cgi->param("catch"); my $exec = 'ssh -T -N myname@myhost.myplace.me -i /home/user/.ssh/keys +/login.key -p 2022 -L 1111:192.168.1.57:1111'; my $pid; defined($pid = fork()) or die "We've go some forking trouble - $!\n"; if ($pid == 0) { exec($exec); die "No exec here and here's why: $!\n"; exit(); } my $server = Frontier::Client->new(url => $catch, ); my $name = $cgi->param("title"); my $cat = $cgi->param("category"); my $priority = "1"; my $Top = $server->boolean("0"); my $method = "appenurl"; my $u = $cgi->param("u"); my $i = $cgi->param("i"); my $r = $cgi->param("r"); my $url = "$u&$i&$r"; my $result = $server->call("appendurl",($name,$cat,$priority,$Top,$url +) );
        So, if I pass the CGI param via the command line I get the tunnel up and sits at the remote login prompt (after a sucessful login) it doesn't continue on to the XMLRPC part. In a browser, the headers (which I am calling from a templat e not included in this code) don't get printed, the script just hangs.

        I am starting to think I am doing something really trivially, stupid at this stage?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015220]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-11-01 10:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (229 votes), past polls