Find the answer to your Linux question:
Results 1 to 10 of 10
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    While do test issue


    Hi,

    Please can someone advise what I'm doing wrong with my "while do done" loop ?

    My first script Setup_A.ksh runs and when it has finished and the PID = 0, I need it to start my second script BatchRun.pl. Please see my while do done loop.

    See below for details :


    ./Setup_A.ksh

    typeset -i PID

    PID=`ps -ef | grep Setup_A.ksh | wc -l`

    while [ $PID > 0 ]
    do
    echo "Setup_A PID is running = $PID"
    sleep 60

    PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`

    done

    echo "Setup_A.ksh completed"

    ./BatchRun.pl

    thank you

  2. #2
    Linux User Krendoshazin's Avatar
    Join Date
    Feb 2005
    Location
    London, England
    Posts
    471
    I suspect that part of the problem is that PID will always return 1, and therefore it never reaches the line to execute the bash script.

    Code:
    ps -ef | grep Setup_A.ksh
    1002     24018 21088  0 06:22 pts/10   00:00:00 grep Setup_A.ksh
    If you grep the output for Setup_A.ksh, then grep will return its own process which is currently searching for Setup_A.ksh.

    Try 'ps -ef | grep Setup_A.ksh | grep -v grep | wc -l' instead:

    Code:
    ps -ef | grep Setup_A.ksh | grep -v grep | wc -l
    0

    I made a quick example. On my machine Setup_A.ksh will not be found and therefore execute the scipt I have specified:

    Code:
    PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`
    
    while [ $PID != 0 ]
    do
            echo "Setup_A PID is running = $PID"
            sleep 60
    
            PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`
    done
    
    sh new.sh
    and the output:

    Code:
    krendoshazin@null:~>sh new1.sh
    this is new.sh

  3. #3
    Hi,

    I have just tried this on my environment, and it does not start the second script "BatchRun.pl
    " even when the PID = 0

    This is my code :

    #!/bin/sh

    ./Setup_A.ksh

    typeset -i PID

    PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`

    while [ $PID > 0 ]
    do
    echo "Setup_A PID is running = $PID"
    sleep 60

    PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`

    done

    echo "Setup_A.ksh completed"

    ./BatchRun.pl


    Is there something else I'm doing wrong within this while do done loop ?

    thank you

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User Krendoshazin's Avatar
    Join Date
    Feb 2005
    Location
    London, England
    Posts
    471
    Quote Originally Posted by venhart View Post
    Is there something else I'm doing wrong within this while do done loop ?
    Yes, in my example I changed while [ $PID > 0 ] to while [ $PID != 0 ].

  6. #5
    Does that first script automatically go into the background somehow?

    I'd do it something like this:
    Code:
    #!/bin/sh
    
    echo "running Setup_A script:"
    ./Setup_A.ksh & while ps -p $! >/dev/null 2>&1; do
      echo script still running...
      sleep 1
    done
    echo done
    
    ./Setup_B.ksh

  7. #6
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    If you are going to call a PERL script inside of shell script, you may need to point it to the PERL executable:
    Code:
    /usr/bin/perl /path/to/perl/script
    I cannot speak for Korn Shell, but I know Bash shells have this issue (for me at least).
    linux user # 503963

  8. #7
    @scathefire,
    jc, do u have that issue even when the perl script is executable and it has the correct path to the command line interpreter (e.g., #!/usr/bin/perl)?

  9. #8
    your version does not work for me. As the script run's in backgroud and start's multiple pids. So the first intial pid finishes and goes to the next script. But I need all the pids to complete before it starts the second script.

    Also tried the orginal reply :

    Yes, in my example I changed while [ $PID > 0 ] to while [ $PID != 0 ].

    This did not work for me either ??

    At present my script looks like ;

    #!/bin/sh

    ./Setup_A.ksh

    typeset -i PID

    PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`

    while [ $PID != 0 ]
    do
    echo "Setup_A PID is running = $PID"
    sleep 60

    PID=`ps -ef | grep Setup_A.ksh | grep -v grep | wc -l`

    done

    echo "Setup_A.ksh completed"

    ./BatchRun.pl

  10. #9
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    Quote Originally Posted by atreyu View Post
    @scathefire,
    jc, do u have that issue even when the perl script is executable and it has the correct path to the command line interpreter (e.g., #!/usr/bin/perl)?
    Yeah for some reason it was always easier to execute shell scripts in perl with system() or exec() than it was to execute perl scripts from a shell script.
    linux user # 503963

  11. #10
    Not clear to me: does Setup_A.ksh spawn sub-shells or programs, then?

    Maybe something like this would work...

    Code:
    #!/bin/bash
    ./Setup_A.ksh&
    mainpid=$!
    progpid=$$
    
    get_pids(){
      pids=$(ps -o pid,cmd|grep $0|grep -v $progpid)
    }
    get_pids
    until [ -z "$pids" ]; do
      echo still running..
      sleep 1
      get_pids
      ps -eo pid,ppid,cmd|grep $0|awk "\$1 !~ /$progpid/"|grep -q . || unset pids
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •