Suspending a running shell command in a Perl script

Viewed 120

I have a Perl script that executes a shell command as follows

system("command");

I need to suspend the execution of that command for a certain amount of time then resume it again from the script itself. I tried to do the following

#!/usr/bin/perl

$pid = fork();
if( $pid == 0 ) {
   print "This is child process\n";
   exec('command') or die($!);
   print "Child process is exiting\n";
   exit 0;
}

print "This is parent process and child ID is $pid\n";
kill 'TSTP',$pid;  
sleep 60;
kill 'CONT',$pid;
while (wait() != -1) {}
print "Parent process is exiting\n";
exit 0;

But this is the output I get

This is child process
Output of command
This is parent process and child ID is 15606
Parent process is exiting

The parent process suspends the child process after the command finsh execution which is not what I need. I checked the answer for that question also

How to Ctrl-Z in Perl script

but I am not sure how to use

kill 'TSTP' => $$;

to suspend the running command and then resume it again.

Thanks in advance.

0 Answers
Related