user2993025
user2993025

Reputation: 31

Wait for the child process to complete in system command in perl

part of my script looks like this.

my @args = ("/bin/updateServer & ");
system (@args) == 0 or die "system @args failed: $?";
reloadServer;

My requirement is only after the updateServer finishes, reloadServer has to be called. In my case reload server runs immeadiately after update server. UpdateServer runs for around 4 hours and so I have to run it in background with "&"

How can I change my code to run reloadServer only after the updateServer is completed.

Can someone please help me in doing so.

Upvotes: 3

Views: 2215

Answers (2)

RobEarl
RobEarl

Reputation: 7912

Instead of running the system command in the background, create a thread to run it and then reload:

use threads;

my $thread = threads->create(sub {
    my @args = ("/bin/updateServer");
    system (@args) == 0 or die "system @args failed: $?";
    reloadServer;
});
# Store $thread somewhere so you can check $thread->error/is_running for it failing/completing.
# Continue doing other things.

The thread will run in the background and run reloadServer once the (now blocking) system command completes.

Upvotes: 1

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14975

Just:

@args = ("/bin/updateServer");

Remove & from command to avoid start process in background

Upvotes: 1

Related Questions