Is it possible to use the array version of perls system
command (i.e. the version that takes the first element as the command and the rest of the array as the cmd arguments) while also spawning a new process with Linux so that the system
command returns immediately e.g. to run a command like:
mycmd arg1 arg2 &
I've tried using system( ('mycmd', 'arg1', 'arg2', '&') )
but it just interprets the ampersand literally as a third argument.
I know I can just pass the whole command as a scalar to system
but I'm specifically wondering if it's possible to use the array version because the parameters for this command will originate from user options in a CGI script.
Since you have no interest in the fate of the executed program you can use fork/exec. And you're on Linux which allows using
$SIG{CHLD} = 'IGNORE'
to avoid waiting on the child process.The
&
part of the shell command tells the shell to run the process in the background, so bypassing the shell by using the multi-arg form ofsystem
makes no sense.Solution 1: Quote using String::ShellQuote.
Solution 2: Quote using shell interpolation.
Solution 3: Launch the program in the background yourself.