Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

perl and shell

by Anonymous Monk
on Oct 31, 2007 at 11:41 UTC ( #648224=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks, I don't know if my post is appropriate here, but I thought I give it a shot... I have a perl script that calls some C scripts (which I have not written myself). I call the C scripts using the print `` command in my perl script and the C scripts run on the shell.
What I wanted to ask is the following: My scripts (both perl and C) run ok, but, since I have all the programs run on a server machine, I want to put the process in the background so that I can close my terminal. The thing is that the C programs show some output while they are executed and, when I tried to use perl > res & , the output of the C programs kept appearing on my screen.
I found something like 1>&2 on the Internet, is that correct?

Comment on perl and shell
Select or Download Code
Re: perl and shell
by sen (Hermit) on Oct 31, 2007 at 11:54 UTC

    Hi, the redirect options are:

    scriptname >filename redirects the output of scriptname to file filename. Overwrite filename if it already exists.

    command &>filename redirects both the stdout and the stderr of command to filename.

    command >&2 redirects stdout of command to stderr.

    scriptname >>filename appends the output of scriptname to file filename. If filename does not already exist, it is created.

    If you need the ouput, redirect to file, else redirect to null

Re: perl and shell
by moritz (Cardinal) on Oct 31, 2007 at 11:54 UTC
    The usual way is to redirect STDERR to STDOUT, that is 2>&1.

    This works as long as the programs write to STDERR and STDOUT, not to the terminal directly.

    Note that when you launch a program in the shell in background, and then terminate the shell, the program in the background will receive a SIGHUP, so you might have to install a signal handler. <c>Or you might take a look at the great program screen.

      Ahem ... jumps in with both feet expecting tirade of abuse...

      Both of the replies thus far hold true for sh, bash etc. etc. - however the redirection doesn't work in the C shell - which is one of the 10 reasons that Tom Christiansen recommends that the use of C shell, especially for scripting, be forgotten forever.

      At last, a user level that best describes my experience :-))
Re: perl and shell
by halley (Prior) on Oct 31, 2007 at 14:27 UTC
    I'm just wondering why you'd do:
    print `command`;
    Unless you're actually saving the output in some intermediary variable that you didn't mention, this is functionally equivalent to the system("command") call. I could understand using the backticks as an attempt to avoid outputting anything, but the print statement sort of defeats that, doesn't it?

    [ e d @ h a l l e y . c c ]

Re: perl and shell
by tuxz0r (Pilgrim) on Oct 31, 2007 at 14:53 UTC
    Everyone is correct about redirecting STDOUT and STDERR to the same place, but the key here is that he wants to close the terminal. In order to do that he needs to use the nohup command along with the '&' to put it in the background and disconnect from his controlling terminal session. When you use nohup it will write STDOUT and STDERR to a file in the working directory called "nohup.out" which you can then go back and get later (since you mention the C programs actually write to STDOUT and STDERR). You won't need the 2>&1, since nohup should handle that for you.
    $ nohup ./ &

    echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://648224]
Approved by naikonta
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-07-12 13:16 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (240 votes), past polls