LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Kill pid (https://www.linuxquestions.org/questions/linux-newbie-8/kill-pid-4175554575/)

dfn 09-26-2015 11:16 AM

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


goumba 09-26-2015 11:27 AM

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 (Post 5425970)
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



chrism01 09-27-2015 10:02 PM

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.

goumba 09-28-2015 03:19 AM

Quote:

Originally Posted by chrism01 (Post 5426600)
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".

chrism01 09-28-2015 03:46 AM

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)

goumba 09-28-2015 03:54 AM

Quote:

Originally Posted by chrism01 (Post 5426683)
You do realise I'm not the OP ??
;)

Yeah I didn't read the username on that post. My mistake, it's 4am. ;)

sudowtf 09-29-2015 09:58 AM

http://www.shellcheck.net/


All times are GMT -5. The time now is 06:53 PM.