Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a script and not wait for it in Perl?

I have a system call in Perl that looks like this:

system('/path/to/utility >> /redirect/to/log file');

But this waits for utility to complete. I just want to trigger this and let the Perl script finish irrespective of whether the utility call finished or not.

How can I do this?

I tried changing the line to

system('/path/to/utility >> /redirect/to/log file &');

but this syntax still waits for the call to finish on Windows. I need to make it work on Linux as well as Windows.

like image 569
Lazer Avatar asked Sep 12 '11 17:09

Lazer


2 Answers

if ($^O eq 'MSWin32') {
   system('start "" \\path\\to\\utility >> \\redirect\\to\\log_file');
} else {
   system('/path/to/utility >> /redirect/to/log_file &');
}

or

if ($^O eq 'MSWin32') {
   system(1, '\\path\\to\\utility >> \\redirect\\to\\log_file');
} else {
   system('/path/to/utility >> /redirect/to/log_file &');
}
like image 53
ikegami Avatar answered Oct 13 '22 22:10

ikegami


You could try looking at the fork keyword, and launch your system command from the forked process. See the perlipc manpage for examples.

like image 45
zigdon Avatar answered Oct 13 '22 22:10

zigdon