Creating a detached process in perl cgi

Creating a detached process in perl cgi

am 13.07.2011 15:32:46 von Ramprasad Prasad

--001636b2bf58b12b2504a7f373fe
Content-Type: text/plain; charset=ISO-8859-1

I am trying a http API that would fork a background script and immediately
return

I tried using fork() and setsid() or using Proc::Background
But the problem is my API hangs until the background script is complete


Is there a way I can avoid this


My code is here ( uses both fork and background .. neither works :-( )


#!/usr/bin/perl
#
#
use strict;
use CGI;
use Proc::Background;

$process = "/path/script"; #
print "Content-Type: text/plain\n\n";
$SIG{CHLD}='IGNORE';
my $pid = fork();
setsid();
print "STARTED\n";
exit if($pid);
my $proc1 = Proc::Background->new($process);
exit 0;







--
Thanks
Ram





n

--001636b2bf58b12b2504a7f373fe--

Re: Creating a detached process in perl cgi

am 13.07.2011 16:37:57 von Shlomi Fish

Hi Ram,

On Wed, 13 Jul 2011 19:02:46 +0530
Ramprasad Prasad wrote:

> I am trying a http API that would fork a background script and immediately
> return
>=20
> I tried using fork() and setsid() or using Proc::Background
> But the problem is my API hangs until the background script is complete
>
>=20
> Is there a way I can avoid this
>=20
>=20
> My code is here ( uses both fork and background .. neither works :-( )
>=20
>=20
> #!/usr/bin/perl
> #
> #
> use strict;
> use CGI;
> use Proc::Background;
>=20
> $process =3D "/path/script"; #

This will not compile with "use strict;" above. You need to add a "my". So I
guess your code fails previously.

> print "Content-Type: text/plain\n\n";

Please use CGI.pm's ->header() function for that.

> $SIG{CHLD}=3D'IGNORE';
> my $pid =3D fork();
> setsid();

I don't think you should call setsid() in both the parent *and* child.

> print "STARTED\n";
> exit if($pid);
> my $proc1 =3D Proc::Background->new($process);

What's going on here?

> exit 0;
>=20

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

Bill Gates, CEO of Microsoft decides to use Richard Stallmanâ€=99s Emac=
s as the
basis of his companyâ€=99s stateâ€=90ofâ€=90theâ€=90art pro=
duct Microsoft Editing Macrosâ„=A2
Enterprise Edition XP .NET Professional.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Creating a detached process in perl cgi

am 14.07.2011 08:54:27 von Ramprasad Prasad

--000e0cd32a9a07b86004a80201ae
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Ok I did get that working but not in pure perl.

I put a system("nohup $process > /dev/null 2>&1 &");
instead of all the fork and setsid



On 13 July 2011 20:07, Shlomi Fish wrote:

> Hi Ram,
>
> On Wed, 13 Jul 2011 19:02:46 +0530
> Ramprasad Prasad wrote:
>
> > I am trying a http API that would fork a background script and
> immediately
> > return
> >
> > I tried using fork() and setsid() or using Proc::Background
> > But the problem is my API hangs until the background script is complete
> >
> >
> > Is there a way I can avoid this
> >
> >
> > My code is here ( uses both fork and background .. neither works :-( )
> >
> >
> > #!/usr/bin/perl
> > #
> > #
> > use strict;
> > use CGI;
> > use Proc::Background;
> >
> > $process =3D "/path/script"; #
>
> This will not compile with "use strict;" above. You need to add a "my". S=
o
> I
> guess your code fails previously.
>
> > print "Content-Type: text/plain\n\n";
>
> Please use CGI.pm's ->header() function for that.
>
> > $SIG{CHLD}=3D'IGNORE';
> > my $pid =3D fork();
> > setsid();
>
> I don't think you should call setsid() in both the parent *and* child.
>
> > print "STARTED\n";
> > exit if($pid);
> > my $proc1 =3D Proc::Background->new($process);
>
> What's going on here?
>
> > exit 0;
> >
>
> Regards,
>
> Shlomi Fish
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish http://www.shlomifish.org/
> Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/
>
> Bill Gates, CEO of Microsoft decides to use Richard Stallmanâ€=99s Em=
acs as the
> basis of his companyâ€=99s stateâ€=90ofâ€=90theâ€=90art p=
roduct Microsoft Editing Macrosâ„=A2
> Enterprise Edition XP .NET Professional.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply =
..
>



--=20
Thanks
Ram





n

--000e0cd32a9a07b86004a80201ae--