#!/usr/bin/perl use strict; use warnings; my $pid = fork err die "Failed to fork: $!"; unless ($pid) { exec "xterm" or die "Failed to exec: $!"; } # $pid is the PID of 'xterm', but we want the PID of the shell # that's run by the xterm. # First we need to give the child time to spawn the shell. sleep 1; my $shell_pid; open my $ps => "ps -ef |" or die "Failed to open pipe: $!\n"; while (<$ps>) { my ($uid, $proc_pid, $proc_ppid) = split; if ($proc_ppid && $proc_ppid eq $pid) { $shell_pid = $proc_pid; last; } } # Open the pseudo-terminal of the shell, and print something. open my $fh => "> /proc/$shell_pid/fd/1" or die "Failed to open terminal: $!"; print $fh "Hello!\n"; close $fh; __END__