3 # ktmux_helper: Run krenew in the background for tmux.
4 # Notes: Doesn't handle multiple sessions properly.
8 use POSIX ":sys_wait_h";
10 my $PROG = $FindBin::Script;
12 # Ensure tmux is our parent and find its PID.
13 my $tmux_pid = &get_tmux_pid;
15 print STDERR "$PROG: Not a child of tmux!\n";
19 # Ensure there isn't already a helper running for this tmux.
20 my $tmux_helper = &get_tmux_helper;
21 exit 0 if $tmux_helper;
26 $SIG{INT} = \&cleanup;
27 $SIG{QUIT} = \&cleanup;
28 $SIG{TERM} = \&cleanup;
30 LOOP: while (&ping_tmux) {
32 die "$PROG: Can't fork: $!\n" unless defined $pid;
36 my $exited = waitpid $pid, WNOHANG;
37 goto LOOP if $exited == $pid || $exited < 0;
41 # tmux is dead so kill krenew.
47 exec "krenew", "-K", "60";
48 print "$PROG: Can't run krenew: $!\n";
55 my $cmd = `/bin/ps -o args= -p $pid`;
56 return $pid if $cmd =~ /\btmux\b/;
62 if (open IN, "pgrep -x -P $tmux_pid $PROG | ") {
76 return kill 0, $tmux_pid;
83 waitpid $pid, WNOHANG;