Reputation: 3026
If I run the command
nohup ./run > /dev/null 2>&1 & disown
in my terminal I get back something along the lines of [1] 1234
which I understand to be the PID.
However, when I run the following in Perl, it returns an error about disown
not being defined or something, but that's not the point. When I remove disown
, the terminal returns the same thing but Perl returns nothing. The variable it was assigned to is just blank.
my $command = `nohup ./run > /dev/null 2>&1 &`;
print("a " . $command); // "a " to check if it's actually printing anything.
Output:
a
Expected output:
[1] 1234
How do I get Perl to display the PID of the command which I can then parse with
@ar = split(/\s+/, $process);
$pid = $ar[1];
Which was provided by another Stackoverflow user in my previous question.
Upvotes: 3
Views: 305
Reputation: 385655
[1] 1234
is only output by bash for interactive shells (i.e. one started with the -i
option).
my $command = `sh -ic 'nohup ./run > /dev/null 2>&1 &' 2>&1`;
die "Can't execute child: $!\n" if $? < 0;
die "Child killed by signal ".($? & 0x7F)."\n" if $? & 0x7F;
die "Child exited with error ".($? >> 8)."\n" if $? >> 8;
or better yet,
use IPC::System::Simple qw( capture );
capture(q{sh -ic 'nohup ./run > /dev/null 2>&1 &' 2>&1});
Another option is to "daemonize" using Daemon::Daemonize. Unfortunately, it only returns the PID via a pid file.
Upvotes: 8