Initiating Non-waiting Background Process in Perl

13,314

Solution 1

The first one is designed to work that way - system executes the command and finishes for it to end.

The last two are also designed that way - exec specifically is designed to never return. It basically replaces the parent process with the child process.

However, the second one should do the trick: the command launched from the system call is shell, which is given your string to execute. Since the string ends with "&", that means the shell will launch your command as a background process, and finish its own execution after that launch.

Can you please post more code illustrating how #2 didn't work?

Also, see what happens if you try backticks or qx:

my $output = qx|perl /util/script.pl $id &|;
print $output;

Also, as a way of reducing unknowns, can you please run the following and tell me what prints:

my $output = qx|(echo "AAAAAAA"; /bin/date; sleep 5; /bin/date; echo "BBBBBBB") &|;
print $output;

Solution 2

Are you calling fork() before calling system or exec?

my $pid = fork();
if (defined($pid) && $pid==0) {
    # background process
    my $exit_code = system( $command );
    exit $exit_code >> 8;
}


my $pid = fork();
if (defined($pid) && $pid==0) {
    # background process
    exec( $command );
    # doesn't^H^H^H^H^H^H shouldn't return
}

Solution 3

You need to disassociate the child from the parent. See perldoc -q daemon. Or Proc::Daemon

Share:
13,314
Russell C.
Author by

Russell C.

Updated on June 05, 2022

Comments

  • Russell C.
    Russell C. almost 2 years

    I have a Perl script that needs to kick off another process in the background and exit without waiting for the other script to finish. There are lots of threads on StackOverflow that cover how to wait in Perl or how to not wait for other programming languages, but I can't seem to find the right answer for Perl.

    I've read up quite a bit and thought I was doing the right things but none of my attempts seem to be working correctly. Here are all the variations I've tried so far to no avail:

    system(qq|perl /util/script.pl $id|);
    
    system(qq|perl /util/script.pl $id &|);
    
    exec(qq|perl /util/script.pl $id|);
    
    exec(qq|perl /util/script.pl $id &|);
    

    With each of these the parent process continues to wait for the child to finish before exiting. Please let me know what I'm doing wrong and the right way to fork the background process.

    Thanks in advance for your help!


    Full code to help with debugging. Note that the API->Function() calls are object oriented modules our code base uses for specific functions that database interactions, etc:

    sub Add {
        my $self = shift;
        my $domain = shift;
    
        if(!$self->IsValid($domain)) { return; }
    
        my $SQL = qq| insert into domains set added=NOW(),domain=| . API->Database->Quote($domain);
        my $sth = API->DatabaseQuery($SQL);
    
        $SQL = qq| select last_insert_id() |;
        $sth = API->DatabaseQuery($SQL);
        my $id = $sth->fetchrow_array();
    
        my $command = qq|perl /home/siteuser/util/new.pl $id &|;
        system($command);
    
        return {'id'=>$id,'domain'=>$domain};
    }