in reply to Perl fork pid Not Correcto for Xterm Spawn

  1. Parent creates a forked copy and sleeps for 5 seconds
  2. The forked copy sleeps for a second.
  3. The forked copy launches a shell.
  4. The shell executes xterm in the background and exits.
  5. The forked copy exits.
  6. The sleep ends
  7. 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); } ...