How can I run Perl system commands in the background?

Perl’s system function has two modes:

  1. taking a single string and passing it to the command shell to allow special characters to be processed
  2. taking a list of strings, exec’ing the first and passing the remaining strings as arguments

In the first form you have to be careful to escape characters that might have a special meaning to the shell. The second form is generally safer since arguments are passed directly to the program being exec’d without the shell being involved.

In your case you seem to be mixing the two forms. The & character only has the meaning of “start this program in the background” if it is passed to the shell. In your program, the ampersand is being passed as the 5th argument to the xterm command.

As Jakob Kruse said the simple answer is to use the single string form of system. If any of the arguments came from an untrusted source you’d have to use quoting or escaping to make them safe.

If you prefer to use the multi-argument form then you’ll need to call fork() and then probably use exec() rather than system().

Leave a Comment