While Loop Exiting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While Loop Exiting
# 1  
Old 04-22-2013
While Loop Exiting

We are trying to design a flow so that an ETL job shouldn't start until the previous job completes. The script we have written is

Code:
while [ `ps -ef|grep "phantom DSD.RUN job_jobName"|grep -iv -e "grep" -e "SH -c"|wc -l` -ne 0  ]; do sleep 2; done

The loop however exits even when the process is actually running. Why could this be happening?
# 2  
Old 04-22-2013
If you have pgrep, I would recommend using it instead:
Code:
while pgrep -f "phantom DSD.RUN job_jobName"
do
   sleep 2
done

Note that -f is used to match pattern against process name. If you know the actual process name, you can get rid of this option and pass the actual process name as argument.
# 3  
Old 04-22-2013
We do not have pgrep. How does pgrep help? Why could this be happening?
# 4  
Old 04-22-2013
Quote:
Originally Posted by jerome_rajan
We do not have pgrep. How does pgrep help?
The reason why pgrep is much preferred is because you don't have to worry about excluding grep command itself.
Quote:
Originally Posted by jerome_rajan
Why could this be happening?
It is difficult to say without seeing the command output. So can you please post the output of below command:
Code:
ps -ef|grep "phantom DSD.RUN job_jobName"

Or you can debug your script by setting xtrace and verbose to understand what exactly is going on..
Code:
#!/bin/your_shell -xv

# 5  
Old 04-22-2013
Where can I see the trace logs after running the script with the -xv tags?

---------- Post updated at 02:50 AM ---------- Previous update was at 01:52 AM ----------

The trace doesn't seem to help much. We saw the point where the loop exited. Went back to the shell and fired a ps and we could still see the same process running.
# 6  
Old 04-22-2013
The trace often does not help that much, in my experience. Too much output, and too hard to wade through. On the other hand, it basically does show everything, if you know how to use it.

The first step is to get your script into normal appearance, instead of all glommed together on one line. The second step is to add some targeted diagnostic statements in your script.

ps combined with grep, as you are doing, will work fine, once you find the glitch.

Can you show the output of:
Code:
ps -ef | grep "phantom DSD.RUN job_jobName"

# 7  
Old 04-22-2013
I've tried this simplified version of your command, and it works perfectly:
Code:
while ps | grep '[p]hantom DSD.RUN job_jobName'; do sleep 1; done

The grep command will give an exit status of 1 or 0 on found/not found that while can evaluate; the [p]... will find exactly p... but rule out the grep with its parameter line itself
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop exiting

Hi , I am processing some files using below shell script the problem for loop exit after processing some files even though it exist.After modifying file.txt and rerunning the script and its running .Any Advise for i in `cat /xx/file.txt |tr -s "," '\n' ` ; do echo $i... (3 Replies)
Discussion started by: mohan705
3 Replies

2. Solaris

Exiting signal 6

Hello all, I have a problem when installing Solaris 10 on Enterprise 450. I booted from dvd, then the installation was started. The error appeared after determining the installation method, F2-Standard, F?-Flash...... The error was Exiting signal 6. Please, need help. Thank you (4 Replies)
Discussion started by: Hardono
4 Replies

3. Shell Programming and Scripting

Problem exiting a WHILE loop in ksh

Hi I am having a problem exiting a WHILE loop. I am on a Sun server using ksh. I am running a Veritas Cluster Software (High Availablity) command to obtain a group status and grepping the command output for status "G" which means that the filesystem is frozen and therefore not available to... (3 Replies)
Discussion started by: bigbuk
3 Replies

4. Shell Programming and Scripting

Loop Forever Script Strangely Exiting

Hi, I have a really simple script which I want to run forever, inside the loop it runs a C application which if it exits should restart. #!/bin/sh while true do ./SCF scf.conf >> scf.log sleep 2 done For some reason the SCF C application coredumps and the script is exiting.... (3 Replies)
Discussion started by: marvinwright
3 Replies

5. Shell Programming and Scripting

Problem in exiting a loop

Hi my code looks like: if test $STEP -le 10 then . . ls -1d AM*-OUT|while read MYDIR do cd $MYDIR ls |tail -n1| while read MYFILE do . . if test -s $MYFILE then sqlldr .... rc=$? if test $rc -ne 0 (3 Replies)
Discussion started by: anijan
3 Replies

6. UNIX for Advanced & Expert Users

"while read ..." loop exiting after reading only one record

Greeting, The following script completes after reading only one record from the input file that contains many records. I commented out the "ssh" and get what I expect, an echo of all the records in the input.txt file. Is ssh killing the file handle? On the box "uname -a" gives "SunOS... (2 Replies)
Discussion started by: twk
2 Replies

7. Shell Programming and Scripting

exiting from a loop

I wonder if someone could help me here. I am trying to find a way of exiting from a loop but not exiting me from the script for example #!/bin/ksh # ************* FUNCTIONS ****************** function1() { #ping test ping $1 2 > /dev/null if ; then ... (13 Replies)
Discussion started by: hcclnoodles
13 Replies

8. Shell Programming and Scripting

Else Loop Exiting Early

All, I'm having a problem w/this function. Specifically, I want to call another function (get_stats) when the process in the else completes (the initial if and the elsif seem to work fine). But what's happening is the get_stats function call is running after the else runs only once, NOT when it... (8 Replies)
Discussion started by: GregWold
8 Replies

9. Shell Programming and Scripting

Bash: Exiting while true loop when terminal is not the focus window

I am running an Ubuntu Gutsy laptop with Advanced Compiz fusion options enabled. I am using xdotool to simulate keyboard input in order to rotate through multiple desktops. I am looking for a way to kill a while true loop when the Enter key (or Control+C if it is easier) is pushed when the... (2 Replies)
Discussion started by: acclaypool
2 Replies

10. Programming

exiting in c

how can i exit in a c program i have tried system ("exit"); but this doesnt seem to work just wondered if you could help. (3 Replies)
Discussion started by: ruffenator
3 Replies
Login or Register to Ask a Question