LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-14-2011, 04:47 AM   #1
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Rep: Reputation: Disabled
Unhappy script to kill a process


Hi ,

I have a script to kill a process.
The pidfile.pid contains the process id of the process to be killed.
I didn't understand all of the conditions
Code:
i=1
while [ -a pidfile.pid -a $i -le 5 ]
do
  kill -s TERM `cat pidfile.pid`
  i=`expr $i + 1`
  sleep 1
done
.
Could any one please explain it for me?
What is the -eq checks in the following script
Code:
if [ $i -eq 6 ]
then
  kill -9 `cat pidfile.pid`
  rm pidfile.pid

Last edited by jhonnappier2007; 11-14-2011 at 05:04 AM.
 
Old 11-14-2011, 06:14 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Which shell is running this script?

Assuming it is bash ...

The while [ -a pidfile.pid -a $i -le 5 ] is wrong because the second -a looks to be intended as a logical AND but it has no such meaning according to Bash documentation. OTOH it does seem to work as intended:
Code:
c@CW8:/tmp$ [ -a pidfile.pid -a 1 -le 5 ] && echo true || echo false
false
c@CW8:/tmp$ touch pidfile.pid
c@CW8:/tmp$ [ -a pidfile.pid -a 1 -le 5 ] && echo true || echo false
true
c@CW8:/tmp$ [ -a pidfile.pid -a 6 -le 5 ] && echo true || echo false
false
[ $i -eq 6 ] is true when variable i contains 6:
Code:
c@CW8:/tmp$ i=5; [ $i -eq 6 ] && echo true || echo false
false
c@CW8:/tmp$ i=6; [ $i -eq 6 ] && echo true || echo false
true
 
1 members found this post helpful.
Old 11-14-2011, 06:26 AM   #3
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thank u for ur reply catkin...
 
Old 11-14-2011, 06:44 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Welcome

If you are re-writing this script you could change [ ... ] to [[ ... ]] for reasons explained here including the documented availability of && as a logical AND operator.

Threads can be marked SOLVED via the Thread Tools menu.
 
1 members found this post helpful.
Old 11-14-2011, 06:49 AM   #5
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thank you once again!!!!
 
Old 11-14-2011, 08:50 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
I think that it's a plain shell script, not a bash script. In the "test" manual page, it says that "-a" is the and operator.

Anyway, this would be a better, but bash-specific way of writing it:

Code:
i=1
while [[ -e pidfile.pid && $i -le 5 ]]
do
  kill -s TERM $(cat pidfile.pid)
  ((i++))
  sleep 1
done
 
Old 11-14-2011, 09:46 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by MTK358 View Post
I think that it's a plain shell script, not a bash script. In the "test" manual page, it says that "-a" is the and operator.
The test man page has no -a for "true if file exists".

The GNU bash reference, section 6.4 Bash Conditional Expressions lists both -a and -e for "true if file exists" but does not list the binary operator -a. The binary operator -a is documented in the GNU bash reference, section 4.1 Bourne Shell Builtins under test.

So it could be either a Bourne (sh) script or a bash script, using the builtin [ synonym for the builtin test.

Noteworthy that the -a test operator has a different meaning depending on whether it appears as a unary operator (file existence test) or as a binary operator (logical AND). The same goes for -o; when it is unary it tests whether a shell option is enabled; when it is binary it is logical OR.
 
1 members found this post helpful.
Old 11-14-2011, 11:31 PM   #8
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Hi all,
I should have mentioned earlier,
It is a bash shell script, but anyway it was not running properly.
Thank you all for your replies.
More information is always welcome from anyone
Thank you
 
  


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
Script to kill process id farooqkadri Red Hat 13 10-01-2009 12:54 PM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 10:42 AM
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 04:47 AM
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 12:29 PM
kill process script adnanm Linux - Newbie 3 03-02-2007 01:30 PM


All times are GMT -5. The time now is 12:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration