LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2012, 03:31 AM   #1
satishchahar
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Rep: Reputation: Disabled
How to terminate while loop.


Hi

I started a while loop directly from terminal to read a file line by line and to run a command on each line. Post running the command I realized to modify the command and tried to stop the loop by using CTRL+C but it didn't stop. I closed that terminal directly but loop is still running. I also tried to KILL the process but it is getting started automatically with a new PID. Please suggest to stop this loop.

Thanks.
 
Old 03-30-2012, 03:37 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
kill the parent process, probably a bash shell.
 
Old 03-30-2012, 03:51 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
How did you start the while loop?
 
Old 03-30-2012, 03:54 AM   #4
satishchahar
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
What I did is I searched the PID for the command running on each line and killed the same. Please suggest, how to kill parent process.
 
Old 03-30-2012, 03:55 AM   #5
satishchahar
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
while read line
do
ldapsearch $line
done < filename
 
Old 03-30-2012, 03:57 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
pstree -p will show the parents with their PIDs
 
Old 03-30-2012, 03:58 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Use the pstree command to find the parent process, e.g.
Code:
pstree -p username
specifying your username to reduce the output.

Edit: Beaten by catkin for a bunch of seconds!



Hi Charles!

Last edited by colucix; 03-30-2012 at 03:59 AM.
 
Old 03-30-2012, 03:59 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by satishchahar View Post
while read line
do
ldapsearch $line
done < filename
It's not obvious why that doesn't respond to multiple Ctrl+C signals, unless the ldapsearch command is programmed to ignore SIGINT.

EDIT: and doesn't exit
 
Old 03-30-2012, 04:00 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by satishchahar View Post
while read line
do
ldapsearch $line
done < filename
Have you tried to rename or move filename?
 
Old 03-30-2012, 04:01 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by colucix View Post
Edit: Beaten by catkin for a bunch of seconds!

Hi Charles!
Hi colucix

We keep doing that, one way round or the other
 
Old 03-30-2012, 04:06 AM   #11
satishchahar
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Hi colucix

I killed the parent process but process is still running with a new parent ID.
Also tried to move the filename.
 
Old 03-30-2012, 04:08 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Try killing the parent's parent
 
Old 03-30-2012, 04:30 AM   #13
satishchahar
LQ Newbie
 
Registered: Sep 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Killing the parent process, didn't help.
 
Old 03-30-2012, 05:11 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Assuming you are not logged on as root, try killing the parent's parent and, if that doesn't work, the parent's parent's parent
 
Old 03-30-2012, 07:48 AM   #15
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
.. terminal to read a file line by line and to run a command on each line. P ..
Also
Quote:
I also tried to KILL the process but it is getting started automatically with a new PID
Could we see the culprit script?

If it is not a live system, then shutdown -h now or shutdown -g0
`

OK.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
ps does not terminate Lotharster Linux - Newbie 1 02-18-2006 09:03 AM
terminate the connection ust Linux - General 1 03-19-2005 10:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration