LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-27-2005, 03:12 AM   #1
jaimese
LQ Newbie
 
Registered: Oct 2005
Posts: 11

Rep: Reputation: 0
why my script die itself when I try to kill a process??


Hi guys!

I need some help with an extrange problem. I´m trying to write an script that manage telnet connections to some networking devices, working in background and monitorizing some networks parameters. I can connect and perform some tasks in these machines but I have a problem I can´t solve.

One of the 'security' modules is designed to avoid a second telnet session can be opened in a device wich has a session opened, because this means my script is hanged trying to connect. To do it, I check in the process list to look for this kind of connection. Since all the connections should be done from the linux server, if I can kill the process in this machine , there shouldn't be any problem.

I have a problem I hope u can help me with.

When I find a session opened before the connection the script have to do, and try to kill this process, the script dies itself also. Here is the code,

idcon=$(ps -ef | grep $eq | grep -v 'grep' | grep -v 'ping' | cut -f2 )

if [ -n $idcon ] 2>/dev/null
then
echo "No se han detectado consolas abiertas"
else
echo "Cerrando consola abierta en" $eq
kill -9 $idcon 2>/dev/null
fi
exit 0

I have tried all the signals but nothing works. The worst part s that I have seen codes likes this in some pages that seems to work perfectly, so if anybody has an idea....
 
Old 10-27-2005, 03:43 AM   #2
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
Re: why my script die itself when I try to kill a process??

Quote:
Originally posted by jaimese
When I find a session opened before the connection the script have to do, and try to kill this process, the script dies itself also. Here is the code,

idcon=$(ps -ef | grep $eq | grep -v 'grep' | grep -v 'ping' | cut -f2 )

if [ -n $idcon ] 2>/dev/null
then
echo "No se han detectado consolas abiertas"
else
echo "Cerrando consola abierta en" $eq
kill -9 $idcon 2>/dev/null
fi
exit 0
Do you have something like this:
Code:
set -e
in your code somewhere? That might be the problem. Also, can you give examples for $eq and a line output from ps -ef that it should match? This information is necessary to further inspect the problem. Also, what is the filename of the script?

Greetings,
Benjamin
 
Old 10-27-2005, 03:50 AM   #3
jaimese
LQ Newbie
 
Registered: Oct 2005
Posts: 11

Original Poster
Rep: Reputation: 0
First of all, thanks for this quick help.

I have no idea about the meaning of this line 'set -e' and it´s not included in the code. I´m going to try it.

Anyway, the $eq variable means the destination ip formatted as "10.1.X.X".

The output line from the ps is somthing like this 'root 25306 23477 0 10:37 pts/2 00:00:00 telnet 10.1.6.5 40000'. I filter it looking for this ip, avoiding the lines that has the words grep or ping (the script has to try connectivity before making the connections)

The script name is vguard.sh

I hope u can give some info to solve the problem.

Thank you very much!

Jaime.
 
Old 10-27-2005, 04:18 AM   #4
blindcoder
ROCK Linux
 
Registered: Dec 2003
Location: Berlin, Germany
Distribution: Crystal ROCK
Posts: 108

Rep: Reputation: 15
Quote:
Originally posted by jaimese
I have no idea about the meaning of this line 'set -e' and it�s not included in the code. I�m going to try it.

Anyway, the $eq variable means the destination ip formatted as "10.1.X.X".

The output line from the ps is somthing like this 'root 25306 23477 0 10:37 pts/2 00:00:00 telnet 10.1.6.5 40000'. I filter it looking for this ip, avoiding the lines that has the words grep or ping (the script has to try connectivity before making the connections)
Okay, I think I got your problem. Try modifying the line like this:
Code:
id=$(ps -ef | grep $eq | grep -v 'grep' | grep -v 'ping' | sed -e 's,\t, ,g' -e 's,  *, ,g' | cut -f2 -d' ')
What happened? The 'cut' command expects tab characters for column separation by default. In your example that somehow returned the whole line. The kill command then looked like this:
Code:
kill -9 root     25306 23477  0 10:37 pts/2    00:00:00 telnet 10.1.6.5 40000
In this case, kill tried to kill the following processes: 25306, 23477, 0, 40000. The 23477 is the PARENT process of the telnet process: your script.
Or in other words: all parameters were checked, and if they were integer numbers, the process will be killed. Had you not used 2>/dev/null you would have seen error messages about the remaining parameters.
With the new code I sent you above the output from ps is changed to turn tabs into spaces, consecutives spaces into one space and then cut is instructed to use a single space as column separator. This then returns only the second column, the process id you want.

set -e means: stop this script if any command returns non-zero status, ie: fails.
Another command you might be interested in: set -x
This means that each command is printed to stderr before it is executed. This can help a lot with debugging.

HTH,
Benjamin
 
Old 10-27-2005, 05:50 AM   #5
jaimese
LQ Newbie
 
Registered: Oct 2005
Posts: 11

Original Poster
Rep: Reputation: 0
Ben, thank you very much. I have finally solve the problem, It´s due to what you have said.

I can continue with my script on my own.

Thanks,

Jaime.
 
  


Reply



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
the process that would not die -- can't kill fdisk Furlinastis Slackware 12 10-30-2005 02:28 PM
how do i kill a process from inside a bash script? mikaelo Programming 4 05-28-2004 08:51 AM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 02:09 PM
BASH script inform user and kill process mounters Programming 3 02-11-2002 04:57 PM
script at xwin start to kill old process(es) butthead Programming 3 02-01-2002 08:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:54 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