- Parent creates a forked copy and sleeps for 5 seconds
- The forked copy sleeps for a second.
- The forked copy launches a shell.
- The shell executes xterm in the background and exits.
- The forked copy exits.
- The sleep ends
- You try to kill a process that exited almost 4 seconds earlier.
You want the shell to stick around until the xterm exits. Do so by removing the "&".
Since we don't need to keep the forked copy around since at all it does is exit (with a wrong code), use exec instead of system.
#!/usr/bin/perl
my $fname = "/tmp/help.txt";
system("echo help me > $fname");
my $pid = fork();
if ($pid == 0)
{
exec("xterm -T hello -e tail -f $fname");
exit($! || 255);
}
...
Even better: Avoid the shell and use a safer interface.
#!/usr/bin/perl
...
my $pid = fork();
if ($pid == 0)
{
exec('xterm', '-T', 'hello', '-e', 'tail', '-f', $fname);
exit($! || 255);
}
...
|