perl的后台进程 [英] perl background process
问题描述
我试图运行在Perl的后台进程。我创建了一个子进程,这是用来调用其他perl脚本。我想和这个孩子过程中运行code平行的几行。和子进程后done.I想打印一行code的。
主要脚本
#!的/ usr / bin中/ perl的$ | = 1;子进程之前\\ n打印;我的$ PID =叉();如果(定义$ PID)
{
系统(perl的testing.pl);
}前等待命令\\ n打印;等待();打印后等待\\ N 20秒;
testing.pl
#!的/ usr / bin中/ perl的打印测试\\ n的内部;睡眠(20);
期望的输出
子进程前
等待命令之前,
(应等待20秒,然后打印)
之后等待20秒
有很多问题,你的脚本。始终:
使用严格的;
使用警告;
本地
伊辛特殊变量是一个很好的做法。只有包含特殊值民主基金
变量返回false对定义
。因此,每隔值(甚至是 0
;这是这里的情况),返回真正定义
。在另一个脚本中,家当是错误的。
#!的/ usr / bin中/ perl的使用严格的;
使用警告;当地$ | = 1;打印子进程之前\\ n;除非(叉){
系统(perl的testing.pl);
出口;
}打印wait命令之前\\ n;
等待;
打印后等待\\ N 20秒;
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.
Main script
#!/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";
testing.pl
#!/usr/bin/perl
print "inside testing\n";
sleep(20);
Expected output
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";
这篇关于perl的后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!