上記の私のコメントの非常に単純な例を次に示します...
#!/usr/bin/perl
use strict;
use warnings;
my $lcnt = 0;
if( !$ARGV[0] ) { # If no ARGS on the command line, get user input
print "How many lines do you want to print?";
chomp( $lcnt = <STDIN> );
if( $lcnt > 0 ) {
# when we are sure we have what we need
# call myself.pl and put it in the background with '&'
my $cmd = "./myself.pl ".$lcnt.' &';
system($cmd);
exit(0);
} else { die "Invalid input!\n"; }
} else { # Otherwise, lets do the processing
$lcnt = $ARGV[0];
for( my $x = 0; $x <= $lcnt; $x++ ) {
my $cmd = "echo 'Printing line: $lcnt' >> /tmp/myself.txt";
system($cmd);
sleep(1);
}
}
exit(0);
これを「myself.pl」というファイルに保存したら、それを実行します。コマンド ラインに引数がない場合、スクリプトは数値の入力を求めます。20 と入力し、Enter キーを押します。スクリプトがほぼ瞬時に終了するのがわかります。しかし、あなたがすぐに
tail -f /tmp/myself.txt
バックグラウンド プロセスがまだ実行中で、毎秒新しい行がファイルに出力されていることがわかります。また、Linux システムで「ps」コマンドを入力すると、生成されたプロセスがバックグラウンドで実行されていることが表示されます。
jlb@linux-f7r2:~/test> ps
PID TTY TIME CMD
1243 pts/1 00:00:00 bash
4171 pts/1 00:00:00 myself.pl
4176 pts/1 00:00:00 ps