Making several wild guesses based on statements in earlier posts, try:
#!/bin/bash
PIDkill=0 # # of processes successfully killed.
PlogFile="/path/to/pidkill.txt # File to receive log entries.
PpidFile="/path/to/pid.txt" # File containing list of PIDs to kill.
# Verify that $PpidFile exists and is readable...
if [ ! -r "$PpidFile" ]
then printf 'Cannot read file "%s"\n' "$PpidFile"
exit 1
fi
# Process the PIDs in $PpidFile.
while read RelatedEachPid
do # Check for the header line...
if [ "$RelatedEachPid" = 'Ppid' ]
then # Header found, skip to next line from $PpidFile.
continue
fi
# Kill the process.
echo "kill -9 $RelatedEachPid"
if kill -9 $RelatedEachPid
then # kill succeeded: note status and increment counter.
echo "kill -9 $RelatedEachPid Success"
PIDkill=$((PIDkill + 1))
# Following two lines are commented out because the kill command will
# print a diagnostic message if it fails; why produce two outputs?
# else # kill failed: note status.
# echo "kill -9 $RelatedEachPid Fail"
fi
done < "$PpidFile"
# Log the results from this run.
echo "total of pids killed: $PIDkill - $(date)" >> "$PlogFile"
Note that:
I added a line to explicitly use bash (since you didn't say how you invoked this script, the default shell on AIX is a 1988 version of the Korn shell, and the unknown -f command diagnostic could have come from ksh not recognizing [[ -f file ]] although I would have expected that to yield a complaint about [[ instead of about -f ),
a PIDkill variable has been created and it is incremented every time kill succeeds,
a variable ( PlogFile ) has been added to specify the pathname of the output log file (that file was the only file in your script that didn't use an absolute pathname and there is nothing in your script to control the directory in which it runs),
the filename that is the last component in the PpidFile variable has been changed to match the filename specified in post #1 in this thread, and
your reference to the undefined variable date was replaced with a command substitution of the date utility.
In general this is a very bad idea. First, processes might be runningg for a long time because they need to run for such a long time. If you kill the db-writer process of a DB it will not help the DB any but most probably corrupt the it beyond repair. If you kill a a systems daemon you might halt the system but most probably achieve nothing productive.
As a rule of thumb: never let scripts kill processes they have not spawned themselves. A script may kill a process it has started before in the background, but any other process should only be killed interactively! The reason is that admins (in general) are a lot smarter than scripts and can analyse the situation before they kill something vital.
This is, for an already bad idea, an even worse realisation. With kill -9 you are killing a process from outside without any chance to clean up: temporary files, shared memory segments, semaphores and any other item a process can allocate will remain there instead of being cleaned up by the exiting process.
If you really need to stop a process try with kill -15 first, then wait for some time. Signal 15 will tell a process to kill itself and well-written processes honour this signal, cleaning up whatever they have allocated. (Processes not doing so should be moved to /dev/null instead of being run, their programmers should be beaten with a UNIX manual.) Only then you might use kill -9 , but NEVER from script and never routinely. This is the desperate measure of last resort and should be used that way.
Finally, your choice of shells:
In AIX the default shell is ksh (in fact a ksh88). You can of course use any shell you want, but it is always a good idea to write scripts in a way so that they assume as little as possible. Don't make your scripts dependent on a non-standard shell without any necessity. Nothing in your script couldn't be written in ksh with the same ease as in bash.
I getting one error. I do not know how fix it. In the text file pid.txt column ppid there is a value of 1.
Here is my pid.txt contents
125895
345679
456789
1234567
1
1
1
Here is the error I am getting:
line 31 kill : 9 invalid signal specification
+ read RelatedEachPid
+'[' 1= Ppid ']'
+ echo 'kill 9 1'
How can this be fix, so that I do not get the above error messages?
Here is the code I used:
#!/bin/bash
PIDkill=0 # # of processes successfully killed.
PlogFile="/path/to/pidkill.txt # File to receive log entries.
PpidFile="/path/to/pid.txt" # File containing list of PIDs to kill.
# Verify that $PpidFile exists and is readable...
if [ ! -r "$PpidFile" ]
then printf 'Cannot read file "%s"\n' "$PpidFile"
exit 1
fi
# Process the PIDs in $PpidFile.
while read RelatedEachPid
do # Check for the header line...
if [ "$RelatedEachPid" = 'Ppid' ]
then # Header found, skip to next line from $PpidFile.
continue
fi
# Kill the process.
echo "kill -9 $RelatedEachPid"
if kill -9 $RelatedEachPid
then # kill succeeded: note status and increment counter.
echo "kill -9 $RelatedEachPid Success"
PIDkill=$((PIDkill + 1))
# Following two lines are commented out because the kill command will
# print a diagnostic message if it fails; why produce two outputs?
# else # kill failed: note status.
# echo "kill -9 $RelatedEachPid Fail"
fi
done < "$PpidFile"
# Log the results from this run.
echo "total of pids killed: $PIDkill - $(date)" >> "$PlogFile"
Filter out the PID "1", it's the "init process" you shouldn't be able to "kill" it. But on the off chance that you somehow manage it, I'll point out that you are unlikely to like the results.
PID 1 is the init process, indispensable for the entire system to be up and running. I wouldn't kill that by no means if I were in your shoes...
The error message shown does not quite relate to the script - line 31 is way down below the kill command?
BTW - do you know the difference between PID and PPID? If your file really contains PPIDs (and the 1 in there seems to indicate such), I'd think twice before killing those.