No backtick shell return value

Discussion in 'Perl Misc' started by Derek Basch, Jan 22, 2007.

  1. Derek Basch

    Derek Basch Guest

    I have a very simple Perl (1) script that calls a bash shell script (2)
    and prints the shell STDOUT.

    However, It just hangs forever after the shell script (2) returns. I
    cannot figure out what I am doing wrong. Any one have an idea?

    Here is the Perl script (1):
    ------------------------------------------------------------------------
    open(COMMAND, "$proxy_dir/proxyctl restart |");
    while (my $line=<COMMAND>) {
    print $line;
    };
    close(COMMAND);

    print "I won't print";
    ------------------------------------------------------------------------

    Here is the bash shell script:
    ------------------------------------------------------------------------
    #!/bin/bash

    THEDIR=/home/proxy/

    case "$1" in
    start)
    echo "Starting $THEDIR proxy"
    cd $THEDIR
    $THEDIR/proxy2 &
    ;;
    stop)
    echo "Shutting down $THEDIR proxy"
    PID=`/bin/ps auxwww | /usr/bin/grep "$THEDIR/proxy2 -c
    $THEDIR/proxy.conf" | /usr/bin/grep -v grep | awk '{print $2}'`
    #echo "PID: $PID"
    kill $PID
    ;;
    restart)
    $0 stop $THEDIR
    $0 start $THEDIR
    ;;
    *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
    esac
    ------------------------------------------------------------------------

    Thanks,
    Derek Basch
     
    Derek Basch, Jan 22, 2007
    #1
    1. Advertising

  2. Derek Basch

    Joe Smith Guest

    Derek Basch wrote:

    > However, It just hangs forever after the shell script (2) returns.


    perl -le 'print "start";print `sleep 5 & echo OK`'
    perl -le 'print "start";print `sleep 5 </dev/null >/dev/null 2>&1 & echo OK`'

    The first one waits 5 seconds before printing OK.
    The second one prints OK immediately.
    -Joe
     
    Joe Smith, Jan 22, 2007
    #2
    1. Advertising

  3. Derek Basch

    Derek Basch Guest

    Michele Dondi wrote:
    > On 22 Jan 2007 10:07:11 -0800, "Derek Basch" <> wrote:
    >
    > >Subject: No backtick shell return value

    >
    > But you're *not* using backticks below...
    >
    > >However, It just hangs forever after the shell script (2) returns. I
    > >cannot figure out what I am doing wrong. Any one have an idea?
    > >
    > >Here is the Perl script (1):
    > >------------------------------------------------------------------------
    > >open(COMMAND, "$proxy_dir/proxyctl restart |");

    >
    > Have you tried testing open() for success as is usually recommended?
    >
    > Anyway I don't see anything terribly wrong, except that I would use a
    > lexical filehandle and the three args form of open():
    >
    > open my $cmd, '-|', "$proxy_dir/proxyctl", 'restart'
    > or die "Can't run proxyctl: $!\n";
    >
    > This way you won't fire up a shell, but directly run the script
    > passing to it the 'restart' cmd line argument.
    >
    > BTW: (do a favour to yourself and)
    >
    > use strict;
    > use warnings;
    >
    > >Here is the bash shell script:

    > [snip]
    >
    > I see this is moderately complex. Have you tried using the above with
    > a script that simply does e.g.
    >
    > echo 'foo bar baz';
    >
    > ? If it works, then you may have a problem with your shell script, not
    > with the perl one.
    >
    >
    > Michele
    > --
    > {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    > (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    > .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    > 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


    Shoot. Sorry. My bad. I posted the wrong code. Thanks for the tips
    though. I will try them all out and report the results.

    Derek
     
    Derek Basch, Jan 23, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leszek Dubiel
    Replies:
    3
    Views:
    9,824
  2. Carl Banks

    Backtick Virgins

    Carl Banks, Aug 21, 2003, in forum: Python
    Replies:
    1
    Views:
    329
    Peter Hansen
    Aug 21, 2003
  3. Greenhorn
    Replies:
    15
    Views:
    833
    Keith Thompson
    Mar 6, 2005
  4. buildmorelines

    Win2k CMD shell variables and backtick execution

    buildmorelines, Oct 5, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    173
    buildmorelines
    Oct 8, 2004
  5. Replies:
    8
    Views:
    608
Loading...

Share This Page