until loop fails.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting until loop fails.
# 1  
Old 09-16-2009
until loop fails.

i have a script like below, i am trying to print "...." until a background process that was executed before is running,, the below does work and teh loop keeps on printing "..", can someone help where i am wrong.


/tmp/test/abc.sh &
until [ -z $! ]
do
echo "..\c"
done


abc.sh is the scipt that i am running in bg
$! capturing the process id of abc.sh[runnig in bg]
trying to print ".." until the process exist..

Please help.
# 2  
Old 09-16-2009
$! captures the PID of the last process run in the background, that's correct. But that won't reset the value. Consider this: If you'd happen to start more than one background process, which one should reset $! ? What if you'd want to wait for the other process?

It's better to check the process list for the PID of the background job, and act based on that:
Code:
abc.sh &
PID=$!
while ps $PID > /dev/null
do
    echo '..\c'
    sleep 1    # So we don't hog the CPU
done

# 3  
Old 09-16-2009
Thanks pluid for ur response,,

the one you ried doesn't work for me,, the "ps" itself stucks here. when i add it to my script that throws error taht the usage of ps is wrong..

and moreover,, i am simple trying to print ".." until the bg process runs and then continue with the rest part of the script, simple like a status display on the screen,,

running abc.sh script in the bg and capturing the $! is all for the same,,
# 4  
Old 09-16-2009
Quote:
Originally Posted by yesmani
the one you ried doesn't work for me,, the "ps" itself stucks here. when i add it to my script that throws error taht the usage of ps is wrong..
My reply was based on what you've told us. Since you didn't tell us you OS, I can only guess, and used GNU ps.

Quote:
Originally Posted by yesmani
and moreover,, i am simple trying to print ".." until the bg process runs and then continue with the rest part of the script, simple like a status display on the screen,,
So you want something like "Launching abc.sh..."? That's not what your original code would have done (by intention, had it even compiled).

Putting a process into the background using '&' is a very very quick operation, and the PID will exist as soon as the shell finished processing the line. Meaning, no matter how big or long running abc.sh might be, the line 'abc.sh &' will return as soon as fork/exec is done (a tiny fraction of a second), and by that point there's a PID assigned to $!
# 5  
Old 09-16-2009
Ambiguous.
You wrote:
Quote:
trying to print ".." until the process exist..
i am simple trying to print ".." until the bg process runs and then continue with the rest part of the script
I assume you mean:
Quote:
trying to print ".." until the process exits..
i am simple trying to print ".." until the bg process completes and then continue with the rest part of the script
Or do you actually mean:
Quote:
trying to print ".." until the process exists..
i am simple trying to print ".." until the bg process starts running and then continue with the rest part of the script


Contrary to previous posts, try "jobs" not "ps" to check whether your background job is running.
Assuming you want to wait for the background process to complete.

Code:
/tmp/test/abc.sh &
while true
do
       if [ -z "`jobs`" ]
       then
              # Background job has finished
              break
       else
              # Background job is still running
              echo "..\c"
              sleep 5
       fi
done

# 6  
Old 09-16-2009

Code:
/tmp/test/abc.sh &
while ps $! > /dev/null
do
  printf "."
  sleep 1 ## or .5; adjust to taste
done

# 7  
Old 09-16-2009
dots while waiting....

I took a different approach. I made two scripts. One to do the work. The other to make dots. I figure the worker script may want to do more than one thing while the dots are moving. And I minimize the load on the system...

Dots.sh script:

Code:
while [ 1 ]
do
 echo -n  "."
 sleep 1
done

The worker script:

Code:
# start the dots going
./poker.sh &
DOTS=$!

# do the work work behind the dots
/tmp/test.sh &
wait $!

# kill off the dots
kill  $DOTS
echo "done"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

2. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

3. Shell Programming and Scripting

While loop not reading all files if ssh fails

The below while loop is in ksh on a SunOs server: SPARC-Enterprise 5.10 The ksh version is: Version M-11/16/88i The intention of the below while loop is to read through a list of file names in files.txt and delete each file from a server, one at a time. The delete works, the problem is that if... (6 Replies)
Discussion started by: LES2013
6 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Programming

realloc() fails

Not sure in which forum to post this. I'm trying here, in Programming. I'm working on a PC with Intel Duo processor & 2GB of ram. OS is Ubuntu 10.04. I'm having problems with a C++ program that makes extensive use of realloc(). It happens that as soon as the overall memory allocated(OS +... (14 Replies)
Discussion started by: mamboknave
14 Replies

6. Programming

realloc fails in C : what next ?

Hi all I'm trying to use someone else's software, which has a realloc that fails in it. This is probably due to memory limitations, as it only happens when I use this software on huge datasets. First question : how to diagnose if it's a soft or hard limitation? I mean, if it's due to my... (10 Replies)
Discussion started by: jossojjos
10 Replies

7. Shell Programming and Scripting

Null Handling in Until loop. . .loop won't stop

Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have: ... (4 Replies)
Discussion started by: brandono66
4 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Programming

system() fails

Hi, I have the following code which fails with return value 1. sprintf(tmp, "rm -rf %s/* 2>/dev/null\n", dest); rc = system( tmp ); rc is 1 The files exist and the paths are correct. I can do a manual copy but the application fails. All the calls to system() function fail with the same... (6 Replies)
Discussion started by: mspaper
6 Replies

10. AIX

SCP fails sometimes

Hi, I got a unix script which copies a file from my AIX machine to a file server using SCP command. I am calling this script 3 times in my process after a time interval say 5mins. My issue here is like most of the time its not sending the file at the first time run while other 2 will work fine.... (10 Replies)
Discussion started by: vipinc
10 Replies
Login or Register to Ask a Question