-1

I am running script with arguments inside the perl script. script and arguments are enclosed with backticks. When I use '&' symbol at last after arguments, the command is not running on background, it is running on foreground. can someone find the mistake in my program. I need to save the output and redirect the same to one variable and then to log.

Below is the code of mine:

open (MYSM, "> /logs/${SM}.smlog");
open (MYSP, "> /logs/${SM}.splog");
$SM_LOG_VAR = ` ./sm.sh  $SE_VER  $SMS_VERSION   & ` ;
$SP_LOG_VAR = ` ./sp.sh  $SE_VER  $SMS_VERSION ` ;
print MYSM  $SM_LOG_VAR ;
print MYSP  $SP_LOG_VAR ;
close(MYSM);
close(MYSP);

The line :

$SM_LOG_VAR = ` ./sm.sh  $SE_VER  $SMS_VERSION   & ` ;  

is not running in background which is completely enclosed with backticks.

Jeevan T
  • 5
  • 1
  • 6

3 Answers3

2

The backticks themselves will complete only when the command completes. You want something like fork + exec or perhaps dup2. Anyway, how would the program proceed with a value for $SM_LOG_VAR if the command to obtain that information has not yet finished?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • ok. is there a way like redirecting output as below: `./sm.sh $SE_VER $SMS_VERSION ` > "/logs/${SM}.smlog 2>&1 & " (this also not running in background ) – Jeevan T Feb 16 '12 at 13:12
  • 1
    Your question still doesn't make any sense. If you need the output in your program, it will not be available until the command finishes. And again, if you want something to run in the background, backticks are not the ticket. – tripleee Feb 16 '12 at 13:20
1

I think your asking the wrong question, from reading your code let me guess at your real problem. You have a Perl script that does some work not shown to set some variables that are then used to run 2 external programs. You want to run both programs simultaneously and store the output from each to it's own log file.

The simplest way to accomplish this is to run both programs in the background and have the shell do the redirection.

system("./sm.sh  $SE_VER  $SMS_VERSION > /logs/${SM}.smlog &");
system("./sp.sh  $SE_VER  $SMS_VERSION > /logs/${SM}.splog &");

This will return to your script without waiting for either program to finish, if you have code later in the program that requires that the commands be completed then you will may need a more complex solution.

Ven'Tatsu
  • 3,565
  • 16
  • 18
0

I would look at: Perl Backticks

If you want to run it in the background I would try something like open3: IPC::Open3

Community
  • 1
  • 1
macduff
  • 4,655
  • 18
  • 29