my %pid; # this should have global scope sub start_proc { my $proc = shift; # use one sub for both jobs return if ( $pid{$proc} ); # already set means it's running $pid{$proc} = fork; if ( $pid{$proc} == 0 ) { # true for the child process $path = ( $proc eq "email" ) ? $spam_dir : $check_dir; exec "perl $path/$proc.pl"; } # in the parent, $pid{$proc} is >0 } sub stop_proc { my $proc = shift; return unless ( $pid{$proc} ); `kill -9 $pid{$proc}`; $pid{$proc} = 0; }