Question

I have written a script using perl comnsole Term::ReadLine::Gnu. How can I run while I'm in the console external commands in the background? I've managed to support various external commands such as ls -l etc. but I also wish to support running commands in the background such as emacs &, but I can't seem to be able to get a process to run in the background. Is there a solution?

Was it helpful?

Solution

To execute something in the background, you can use system:

#!/usr/bin/env perl

use strict;
use warnings;

system q(emacs &);
print "hello";

This will open emacs in the background and print "hello" straight away. If you removed the & in the command it would wait for emacs to close before printing "hello".

As pointed out by @Brad in the comments, note that this will only work on systems where the shell understands the &. If you were running on Windows for example, you would have to change the system line to something like this:

system q(start /b program.exe);

See the system documentation for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top