0

I am working on a capstone project and am hoping for some insight.

This is the first time I've worked with Perl and it's pretty much a basic Perl script to automate a few different Unix commands that need to be executed in a specific order. There are two lines throughout the script which executes a Unix command that needs to finish processing before it is acceptable for the rest of the script to run (data will be incorrect otherwise).

How am I able to use Perl (or maybe this is a Unix question?) to print a simple string once the Unix command has finished processing? I am looking into ways to read in the Unix command name but am not sure how to implement a way to check if the process is no longer running and to print a string such as "X command has finished processing" upon it's completion.

Example:

system("nohup scripts_pl/RunAll.pl &");

This runs a command in the background that takes time to process. I am asking how I can use Perl (or Unix?) to print a string once the process has finished.

Locutus
  • 11
  • 2
  • How do you call that command? Asking questions on Stack Overflow works best if you show us what you are talking about. A general description is really hard to debug. Please [edit] your question and add your code, and the output you're getting as well as the expected output. Also see [mcve] and [ask]. – simbabque Mar 28 '18 at 16:06
  • See [system](http://perldoc.perl.org/functions/system.html) – Håkon Hægland Mar 28 '18 at 16:07
  • @simbabque I don't have any code to show you because I do not yet know how to create it. I call the commands using system(). – Locutus Mar 28 '18 at 16:12
  • 2
    @Locutus: If you *"call the commands using system()"* then you have code. Please show it. – Borodin Mar 28 '18 at 16:14
  • @Borodin Added to post. – Locutus Mar 28 '18 at 16:19
  • Why are you running this command in the background if the rest of the script needs to wait for it anyway? – tripleee Mar 31 '18 at 10:47
  • Seems like you posted a very similar question on PerlMonks as [Executing a command after the preceding command finished processing](https://www.perlmonks.org/?node_id=1212005)? – haukex Apr 01 '18 at 10:29

2 Answers2

1

I'm sorry if I didn't understand your asking context. But couldn't you use perl process fork function instead of & if you would like to do parallel process?

# parent process
if (my $pid = fork) {
    # this block behaves as a normal process

    system("nohup scripts_pl/RunAll2.pl"); # you can call other system (like RunAll2.pl)
    wait; # wait for the background processing
    say 'finished both';
}
# child process
else {
    # this block behaves as a background process
    system("nohup scripts_pl/RunAll.pl"); # trim &
}
0

You could try to use IPC::Open3 instead of system:

use IPC::Open3;
my $pid = open3("<&STDIN", ">&STDOUT", ">&STDERR", 'nohup scripts_pl/RunAll.pl');
waitpid( $pid, 0 );

Or, if you need to run nohup through the shell:

my $pid = open3("<&STDIN", ">&STDOUT", ">&STDERR", 'bash','-c', 'nohup scripts_pl/RunAll.pl & wait');

Update: Thanks to @ikegami. A better approach if you would like STDIN to stay open after running the command:

open(local *CHILD_STDIN, "<&", '/dev/null') or die $!;
my $pid = open3("<&CHILD_STDIN", ">&STDOUT", ">&STDERR", 'nohup scripts_pl/RunAll.pl');
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174