LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-26-2015, 11:16 AM   #1
dfn
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Rep: Reputation: Disabled
Kill pid


The OS is AIX.

I created a program to kill long running pid processes.

I am getting the following error message:

-f command cannot be found.

I also want to count the number of pids that are killed and append the results to a text file. I am new to shell script programming.

1.The first part of code is exporting a text file column containing ppids.

pid.txt contents are as follows:

Ppid
5569000
6789034
4567890
1234567
5678908
3457892

2. The second part of the code changes permission on the PpidFile

3. The third part of the code loops through the column in the text
file and kill each Ppid.

4. finally a line of text is appended to a text file. The text contains the total number of pids killed and the date.


Here is my code:
Code:
export PpidFile="/path/to/pids.txt"
if [[ -f "$PpidFile" ]]
then
	/bin/chmod 755 $PpidFile
	Ret=$?
	if [ 0 -eq $Ret ]
	then
		for RelatedEachPid in `/bin/grep -v "Ppid" $PpidFile | /usr/bin/tr "\n" " "`
		do
			/bin/echo "kill -9 $RelatedEachPid"
			/bin/kill -9 $RelatedEachPid
			Ret=$?
			if [ 0 -ne $Ret ]
			then
				/bin/echo "kill -9 $RelatedEachPid Fail"
			else
				/bin/echo "kill -9 $RelatedEachPid 
PIDkill"
			fi

echo "total of pids killed: wc-l $PIDkill -  $date" >> pidkill.txt
 
Old 09-26-2015, 11:27 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Back to Debian, and only Debian.
Posts: 1,158
Blog Entries: 7

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
What shell are you using?

The test should only have one set of single brackets, not double brackets as you have. See the red below.

Quote:
Originally Posted by dfn View Post
Code:
export PpidFile="/path/to/pids.txt"
if [ -f "$PpidFile" ]
then
	/bin/chmod 755 $PpidFile
	Ret=$?
	if [ 0 -eq $Ret ]
	then
		for RelatedEachPid in `/bin/grep -v "Ppid" $PpidFile | /usr/bin/tr "\n" " "`
		do
			/bin/echo "kill -9 $RelatedEachPid"
			/bin/kill -9 $RelatedEachPid
			Ret=$?
			if [ 0 -ne $Ret ]
			then
				/bin/echo "kill -9 $RelatedEachPid Fail"
			else
				/bin/echo "kill -9 $RelatedEachPid 
PIDkill"
			fi

echo "total of pids killed: wc-l $PIDkill -  $date" >> pidkill.txt
 
Old 09-27-2015, 10:02 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
Quote:
The test should only have one set of single brackets, not double brackets
Why ?

According to this see http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS [[ ]] is better.
 
Old 09-28-2015, 03:19 AM   #4
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Back to Debian, and only Debian.
Posts: 1,158
Blog Entries: 7

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
Quote:
Originally Posted by chrism01 View Post
Why ?

According to this see http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS [[ ]] is better.
For consistency.

To the Op: All of your other tests use single brackets. Check out chrism01's post, and make the adjustments.

Also, you never answered what shell you were using.

I don't know if this was a bad copy and paste job, you're missing some stuff:

You're missing the done that goes with do. You're also missing two fis.

Adding those to the end of your code, assuming nothing else is missing, the script works for me with bash. I get no error about -f. Without adding those three keywords, I get "unexpected end of file".

Last edited by goumba; 09-28-2015 at 03:56 AM.
 
Old 09-28-2015, 03:46 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
You do realise I'm not the OP ??


Anyway, double brackets are better / more robust and I would urge the OP to use them in all cases.
FWIW I was originally taught this on a course at Hewlett-Packard many years ago (technically for ksh, but the same logic applies)
 
Old 09-28-2015, 03:54 AM   #6
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Back to Debian, and only Debian.
Posts: 1,158
Blog Entries: 7

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
Quote:
Originally Posted by chrism01 View Post
You do realise I'm not the OP ??
Yeah I didn't read the username on that post. My mistake, it's 4am.
 
Old 09-29-2015, 09:58 AM   #7
sudowtf
Member
 
Registered: Nov 2013
Posts: 187

Rep: Reputation: 45
http://www.shellcheck.net/
 
  


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 kill PID based from pid file contents apss_evaluator Linux - Newbie 4 07-30-2015 08:07 AM
Can I assign a Command to a specific PID so I can kill a predetermined PID johnmccarthy Linux - Newbie 1 11-03-2011 08:41 AM
kill < file (Feeding kill from file), PID, <, awk, grep jaffd Programming 4 04-09-2010 04:06 PM
What is the more easier way to check the pid and kill the pid cmx08 Linux - General 5 09-09-2008 10:57 PM
kill pid.... won't work with 'pid' variable given.. sachitha Programming 6 03-06-2006 07:48 PM


All times are GMT -5. The time now is 08:25 PM.

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