I am trying to run a background process in perl. I create a child process, which is used to call another perl script. I want to run few lines of code parallely with this child process. And after the child process is done.I want to print a line of code.
#!/usr/bin/perl
$|=1;
print "before the child process\n";
my $pid = fork();
if (defined $pid)
{
system("perl testing.pl");
}
print "before wait command\n";
wait();
print "after 20 secs of waiting\n";
#!/usr/bin/perl
print "inside testing\n";
sleep(20);
before the child process before wait command (should wait for 20 secs and then print) after 20 secs of waiting
There are many problems with your script. Always:
use strict;
use warnings;
local
ising special variables is a good practice. Only a variable containing the special value undef
returns false for defined
. So, every other value (even a 0
; which is the case here) returns true for defined
. In the other script, the shebang is wrong.
#!/usr/bin/perl
use strict;
use warnings;
local $| = 1;
print "Before the child process\n";
unless (fork) {
system("perl testing.pl");
exit;
}
print "Before wait command\n";
wait;
print "After 20 secs of waiting\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With