satish.rpr has asked for the wisdom of the Perl Monks concerning the following question:

I am launching an external program through perl script using backticks. But I am facing an issue. Although the external program gets launced, the control is not given back to the perl script (a print statement immediately after this does not execute). I used system instead of backticks but still the issue persists. When I use the same backtick command in another perl script , it works fine. Any help greatly appreciated. Here is the code
Here is the code: sub start_CM_Synergy() { my ($database_name) = @_; my $dbData = `grep $database_name /common/gm/scripts/dbInfo`; if($dbData eq "") { return 1; } my @Fld = split(/\s+/, $dbData); $dbName = $Fld[0]; $dbId = $Fld[1]; $ccm_home = $Fld[2]; $dbEnghost = $Fld[3]; my $dbPath = $Fld[4]; $home_dir = $ENV{HOME}; $ENV{CCM_ENGLOG} = "$home_dir/ccm_eng_" . "$dbId" . ".log"; $ENV{CCM_HOME} = "$ccm_home"; $ENV{CCM_UILOG} = "$home_dir/ccm_ui_" . "$dbId" . ".log"; $ENV{PATH} = $ENV{PATH} . ":" . "$ccm_home/bin"; `/common/gm/scripts/ccm_ini_init $dbId`; my $res=`$ccm_home/bin/ccm start -nogui -f $home_dir/.ccm_$dbId.ini +-d $dbPath/$dbName -m -q -h $dbEnghost`; print LOG $res; # Nothing is being printed if ($res ne "") { $ENV{CCM_ADDR}=$res; return 0; } else { return 1; } return 0; # subroutine not even returning to the caller with an inva +lid status }

Replies are listed 'Best First'.
Re: backticks, system not returning back control to script
by moritz (Cardinal) on Oct 28, 2009 at 10:00 UTC

    system and qx are supposed to return to the script as soon as the external program quits. If it keeps on running, you have to launch it in the background, or maybe fork and exec (depending on he operating system, which you sadly did not tell us).

    Perl 6 - links to (nearly) everything that is Perl 6.
      No, the program does not run continuously, when it it launched using the command line, it launches and returns a string (some address). I am capturing the returned value in the script. When I use the same function in another perl script, it is working fine.. . I am running the script in linux.
        So it's a problem specific to a script you don't show us. If you want us to help you, provide a (minimized) version of your scripts.
        Perl 6 - links to (nearly) everything that is Perl 6.
Re: backticks, system not returning back control to script
by jakobi (Pilgrim) on Oct 28, 2009 at 11:12 UTC

    Without a cut-down and running code example of your problem, we can only guess.

    If you'd ask me for a blind guess, I'd suggest to prepend/append an echo redirected to a file to the command being executed - funny filenames can have interesting effects. Secondly, your print statement after system() might indeed execute but get buffered (instead of being flushed) while your script hangs _after_ returning from system().

    For debugging, consider something like warn("system returned with output: ...\n") if $o_debug. For further ideas, consider Debugging and Optimization

    cu & HTH, Peter -- hints may be untested unless stated otherwise; use with caution & understanding.
Re: backticks, system not returning back control to script
by jakobi (Pilgrim) on Oct 28, 2009 at 16:57 UTC
    Things I'd double check:
    • your ccm_ini_init. Would be the first time I see an init script that doesn't try to set some environment variables for subsequent application invocations.
      However, mere shell variables certainly don't survive from your invocation of ccm_ini_init to your invocation of ccm. Make this my primary bet for your problem.

      (btw, why `` w/o assignment instead of system()?) Prepend the init to the real ccm invocation. Or if the init properly exports variables, consider running it before invoking your perl program, so Perl will inherit the settings via %ENV.
    • are your variable contents really suitable as bare-words in the shell?
      $cmd="$ccm_home/bin/ccm start -nogui -f $home_dir/.ccm_$dbId.in +i -d $dbPath/$dbName -m -q -h $dbEnghost"; warn "cmd is $cmd\n"; my $res=`$cmd`;

      If your $ENVIRONMENT does emphasize strict departmental isolation of permissions/IDs/tasks, it might not be the best of ideas to trust their sense of naming blindly, as this might be an attack vector tested during e.g. security assessment "projects". Check e.g. the %ENV trick as a Unix-shell-safe idiom).

    • still quoting: while you lack some quoting in the system/``, which may or may not be harmless*, you use them without need in perl assignments (perl!=shell).
    cu & HTH, Peter -- hints may be untested unless stated otherwise; use with caution & understanding.
Re: backticks, system not returning back control to script
by gmargo (Hermit) on Oct 28, 2009 at 16:37 UTC