I'm trying to figure out how to start a background process in a perl script (on Windows). In other words, I don't want the script to wait for the child process to complete before regaining control. Is there an easy way to do this in just a few lines of code? I've looked at perldoc for the system command, but it doesn't seem to do what I want..
Either external programs or perl subroutines can be launched and controlled as processes in the background. sub mysleep { sleep(shift); } # Define mysleep() $myproc->start(\&mysleep, 10); # Launch it. $myproc->start(sub { sleep(10); });
Use bg to Send Running Commands to the Background You can easily send such commands to the background by hitting the Ctrl + Z keys and then using the bg command. Hitting Ctrl + Z stops the running process, and bg takes it to the background. You can view a list of all background tasks by typing jobs in the terminal.
exec - Perldoc Browser. The exec function executes a system command and never returns; use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system's command shell (see below).
The fork() Function in PerlThe fork() function is used to clone a current process. This call create a new process running the same program at the same point. It returns the child pid to the parent process, 0 to the child process, or under if the fork is unsuccessful.
See system in perlport: "system(1,@args)
spawns an external process and immediately returns its process designator, without waiting for it to terminate...(Win32)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With