0

I have a process which when
executed as below:

process_name > file.txt

will redirect all the output to file.txt and also to the console.

when executed like this

process_name >& file.txt&

will redirect the output to file.txt but will not print the output to console returning the pid on the console.

Now i want to execute the second way of running the process inside a perl script! how could i do this? currently i am usng the below method:

#!/usr/bin/perl

use strict;
use warnings;

my $res = `bsm_adjust_controller -F -ra -d 'subnetwork=NETSim_STD/bsc=0005' -f /tmp/31102012/Task_31102012_1421_0005.log >& /tmp/31102012/Ctrl_31102012_1421_0005.log&`;
print $res;

when i run the script it gives:

 ./temp.pl
sh: /tmp/31102012/Ctrl_31102012_1421_0005.log: bad number

I also tried the below method:

#!/usr/bin/perl

use strict;
use warnings;

my $res = "bsm_adjust_controller -F -ra -d 'subnetwork=NETSim_STD/bsc=0005' -f /tmp/31102012/Task_31102012_1421_0005.log >& /tmp/31102012/Ctrl_31102012_1421_0005.log&";

open(CMD, '-|', $res) || die "couldn't run $res: $!\n";

Even the above script throws the same error. the purpose is to execute the process in background and continue with the next statements without caring what happened to the process and also i donot need the output of the process being run inside to console.

could anybody please help?

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 4
    [How can I run a system command in Perl asynchronously?](http://stackoverflow.com/questions/1752357/how-can-i-run-a-system-command-in-perl-asynchronously) – jshy Oct 31 '12 at 12:23
  • I tried this but it doesnt help.it throws the same error. – Vijay Oct 31 '12 at 12:43
  • I think your command is wrong. Try first removing the & at the end as this no longer makes sense. Also you might want to try the 'system()` with the args array. This may yield more information about any errors also. – jshy Oct 31 '12 at 12:57
  • exatly ...when i removed the ampersan it worked .Thanks Anyways. – Vijay Oct 31 '12 at 12:57

1 Answers1

1

well this worked:

#!/usr/bin/perl

use strict;
use warnings;

my $cmd="bsm_adjust_controller -F -ra -d 'subnetwork=NETSim_STD/bsc=0106' -f /tmp/31102012/Task_31102012_1421_0005.log >/tmp/31102012/Ctrl_31102012_1421_0005.log";

open STDERR, ">/dev/null";
print "Redirected stderr to /dev/null\n";

my $pid=open (FH, "-|",$cmd);
if ($pid) { #parent
    print "/tmp/31102012/Task_31102012_1421_0005.log";
    exit; 
          } 
print "Redirected stderr to /dev/null\n";
close(FH);
Vijay
  • 65,327
  • 90
  • 227
  • 319