Link to home
Start Free TrialLog in
Avatar of shamstar
shamstar

asked on

Perl script unix cshell: running external app.

Hi Guys,

Ive started to mess around with some perl scripting in unix and am wanting to make a script which I can give
two parameters, an image name, and a suffix.  The script will then run a command and hopefully tell me the PID of the process that was1 run.
As an example of the manual way I am doing things, I type in:
   shell>nohup make MAKEFLAG_J"-j20 -l20" BIN_SUFFIX="some_suffix" some_image >& build.log &
   [1] 12679
   shell>
So the thing to note here is that when the above command is run, it returns the PID for the new process.. 12679 in this case.  I would like my script to capture this, but more importantly run the exact command as if it were typed in manually.  Also, all output from the make is redirected into a file called "build.log".

My script could be called like this:  script_name <some_image> <some_suffix>

So far, my file is like this:
------------------------------

#!/usr/local/bin/perl -w

$IMAGE  = $ARGV[0];
$SUFFIX = $ARGV[1];

$PID = `nohup make MAKEFLAG_J=\"-j20 -l10\" BIN_SUFFIX=$SUFFIX $IMAGE \>\& build.log \&`;
system($PID);

print "\n---------- Build Summary ----------";
print "\n\tImage:  $IMAGE\n\tSuffix: $SUFFIX";
print "\n\tStatus: $PID";
print "\n-----------------------------------\n\n";

-----------------------------------------------------------------------------------------------------------------

The above doesnt quite seem to work.. I can get the actual build to happen when I remove the redirect
to build.log.  Without this bit, I get a message saying "Sending output to nohup.out".

Any help is appreciated.
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shamstar
shamstar

ASKER

Hi Guys,

Thanks for your replies.  I have some comments for both of you below.

@ahoffmann:
I have copied and pasted the code you gave, however it doesnt seem to get anything building.
Output is as follows:
-----------------------------------------------------
com>
com>build.script dflt_image .test

Your request is being processed...

---------- Build Summary ----------
        Image:  unix-p-ms
        Suffix: .test
        Status: 0
-----------------------------------

Sending output to nohup.out

---------- Build Summary ----------
        Image:  unix-p-ms
        Suffix: .test
        Status: 4297
-----------------------------------
com>
com>
-----------------------------------------------------
So a few points to note about the above output is that firstly the "build summary" does display more than once althoug it only appears in the code once [and not in any loops either :-) ].
The second thing is that right after the "Sending output to nohup.out" is displayed there is about a 10 second pause before the 2nd build summary is displayed.  I also see that the build.log file is not created.
Any ideas?


@stefan73:
You're method worked and kicked off a build as well as redirecting the output to the correct file.  The only small
issue is that I'm not able to get the process ID, instead the output displays the entire command.  Would you know
how I can get this easily (without having to trawl through "ps -ef" if possible) ?


Thanks for your help.
stefan73, good eyes, missed to correct the csh syntax
please correct $cmd as follows:

$cmd = "nohup make MAKEFLAG_J='-j20 -l10' BIN_SUFFIX=$SUFFIX $IMAGE > build.log 2>&1 &";
Thanks - didnt think of mixing both of your suggestions, so I now have:
==============================
$cmd = "nohup make MAKEFLAG_J='-j20 -l10' BIN_SUFFIX=$SUFFIX $IMAGE > build.log 2>&1 &";
$PID=fork();
system($cmd) if ($PID>0);

#$PID = qq[nohup make MAKEFLAG_J="-j20 -l20" BIN_SUFFIX=$SUFFIX $IMAGE > build.log 2>&1 &];
#system($PID);

print "\n---------- Build Summary ----------";
print "\n\tImage:  $IMAGE\n\tSuffix: $SUFFIX";
print "\n\tStatus: $PID";
print "\n-----------------------------------\n\n";
==============================

This works but still has a problem that the build summary is displayed twice (possibly due to the fork()?)
/shamrahm-sa57779_8t>~/perl.script unix-p-ms .test
Outuput:
com>
com>build.script dflt_image .test

Your request is being processed...

---------- Build Summary ----------
        Image: dflt_image
        Suffix: .test
        Status: 0
-----------------------------------


---------- Build Summary ----------
        Image: dflt_image
        Suffix: .test
        Status: 23293
-----------------------------------
com>

So above, I would only need the second build summary since the first provies no real information (i.e the pid).
Is it possible to stop this bit of code being run twice?

Thanks.
change as follows:

$PID=fork();
if ($PID>0){
  exec($CMD);
  exit 0;
} else {
print "\n---------- Build Summary ----------";
print "\n\tImage:  $IMAGE\n\tSuffix: $SUFFIX";
print "\n\tStatus: $PID";
print "\n-----------------------------------\n\n";
}


but keep in mind that your "Build Summaryy just reports the child's PID, and not if the command succeds or what result it produced.
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>  Why bother with a Perl script.
'cause the questioner makes exercises ;-)

But I prefer too:  KISS - keep it simple stupid
There are a number of reasons I've chosen to use perl, I originally did what is shown above using csh.  The three main reasons that I decided to change to perl were:
1. I wanted to start learning perl again (I had a tiny bit of experience a long time ago).
2. The small bit of code above is only a part of the overall functionality that I'm developing, so I believed perl would offer me the greatest flexibility for doing the things I need, and I may also find help easily for when I get stuck.
3. The unix environment the script will be used it is mixed, so people are using different shells, like bash etc.

I will increase the points for this question and then split them between you.
Thanks to all of you for resonding and helping :-)
Point 1 - Learning which is the most appropriate tool to use for the job is a better skill.  It's great to learn Perl, but you are better off using Perl for something that it is more appropriate for.  If you find your Perl script is full of system functions or backticks, then you either aren't using Perlisms, or it should be written as a shell script.

Point 2 - It's quite OK to break up the functionality into modules that can be a mixed programming environment, eg:  Perl frontend and shell scripts for doing system stuff.

Point 3 is irrelevant.  It doesn't matter what shell you use when running a script, so long as it has a valid hash bang (#!) line at the top of the script.