Problem
When running exec in perl, I am am unable to redirect the output to a file.
I am running vlc in the exec but since i doubt everyone has it set up I have replaced with echo below, it shows same behaviour for the example.
I am only interested in exec 'command','args' format of exec not the one that spawns a shell since it spawns a subshell with vlc which still prints to screen + other problems with killing it cleanly.
Code
use strict;
use warnings;
my $pid = fork;
if (!defined $pid) {
die "Cannot fork: $!";
}
elsif ($pid == 0) {
exec "/usr/bin/echo","done";
}
Tried
exec "/usr/bin/echo","done",">/dev/null";
As expected just prints ">/dev/null", but was worth a try.
exec "/usr/bin/echo done >/dev/null";
Runs sh which then runs echo, works here, but not in my actual problem with vlc, thought i would include anyway since someone will surely suggest it.
Question
How do I redirect output from this exec when using 'command','args' to a file?
Extra
Any more info needed please ask.
exec '/usr/bin/echo', 'done';>/dev/nullis part of the shell. If you runexec '/usr/bin/echo', 'done'no shell is involved, which is the idea of the argument list. And without a shell, there is no way to redirect. You need to find a different way to change the handles that the program gets. But I don't know how unfortunately.selectstrangely?selectdoes not change STDOUT. It changes the handle that Perl uses to write to by default. I didn't think of that. Well done. :)