?
Solved

Perl script unix cshell: running external app.

Posted on 2005-05-03
10
Medium Priority
?
1,420 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:shamstar
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 900 total points
ID: 13920810
#!/usr/local/bin/perl -w
$IMAGE  = $ARGV[0];
$SUFFIX = $ARGV[1];
$cmd = 'nohup make MAKEFLAG_J=\"-j20 -l10\" BIN_SUFFIX=$SUFFIX $IMAGE \>\& build.log \&';
$PID=fork();
exec($CMD) if ($PID>0);

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

Assisted Solution

by:stefan73
stefan73 earned 900 total points
ID: 13921267
Hi shamstar,
> \>\&

This quite smells like you're expecting csh or tcsh and you're escaping too much. Try this instead:

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




Cheers!

Stefan
0
 
LVL 8

Author Comment

by:shamstar
ID: 13921593
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 13924768
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 &";
0
 
LVL 8

Author Comment

by:shamstar
ID: 13925238
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.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13930142
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.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 13931012
Why bother with a Perl script.

#!/bin/sh
IMAGE=$1
SUFFIX=$2

nohup make MAKEFLAG_J="-j20 -l10" BIN_SUFFIX=$SUFFIX $IMAGE >build.log &

cat <<EOF
--------Build Summary--------

        Image: $IMAGE
        Suffix: $SUFFIX
        Status: $!

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

EOF
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13933319
>  Why bother with a Perl script.
'cause the questioner makes exercises ;-)

But I prefer too:  KISS - keep it simple stupid
0
 
LVL 8

Author Comment

by:shamstar
ID: 13933931
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 :-)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 13941411
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.

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question