Solved! Go to Solution.
Thank you for the responses. Would there be an easy way to limit the processes being killed to those being started by someone/something in a particular group (ie. only processes started by someone in group "users")? This would be a better (and safer) solution for me than limiting UID's to under 1000, etc. Thanks!!!!
@TheJuiceman wrote:Would there be an easy way to limit the processes being killed to those being started by someone/something in a particular group (ie. only processes started by someone in group "users")?
Well, again, using the UNIX95 behavior allows you to collect various views of *group* information. For that matter, the 'ps' can specify group numbers or names given in a 'gidlist' argument ('-G gidlist).
You should really look at the manpages for 'ps(1)'.
Regards!
...JRF...
The man pages are your friend. Looking at "man (1) ps", you will see the "-G gidlist" option, which will "Select processes whose real groupd ID numbers or group names are given in gidlist"
Thanks. It looks like the "UNIX95= ps -G <group>" route may be the way to go for me.
How could I best write a "wrapper" so that when someone calls my "kill" script via sudo such as....
sudo kill 123 456 789 12345 ... ...
that the script will determine that processes "123", "456", etc are GID of the group I am allowing kill access and allow the kill?
Thank you again for your help.
@TheJuiceman wrote:Thanks. It looks like the "UNIX95= ps -G <group>" route may be the way to go for me.
How could I best write a "wrapper" so that when someone calls my "kill" script via sudo such as....
sudo kill 123 456 789 12345 ... ...
that the script will determine that processes "123", "456", etc are GID of the group I am allowing kill access and allow the kill?
Hi:
Why do you need another script to call your script? I assume that you simply need an argument that denotes the GID of processes you want to kill. All you need do is collect the PIDs that match your criteria and issue a 'kill' for that list.
For that matter, if you are running 11.31, have a look at 'pkill(1)'.
Regards!
...JRF...
Unfortunately, this is a 11.23 box.
You are correct. I just need a way to limit the kill command to executing against PID's that fit my GID criteria.
Example:
sudo scriptkill 123 456 7890
- Do these EXACT processes 123, 456, and 7890 exist?
- If so, do they meet the GID requirement?
- If so, perform a kill on these EXACT PID's, no wildcards or partial returns (ie. allow a kill on process "123" but not "1234" or "2123" ).
- If the criterias are not met, do not allow a kill.
Thanks
Hi (again):
@TheJuiceman wrote:You are correct. I just need a way to limit the kill command to executing against PID's that fit my GID criteria.
Example:sudo scriptkill 123 456 7890
- Do these EXACT processes 123, 456, and 7890 exist?
- If so, do they meet the GID requirement?
- If so, perform a kill on these EXACT PID's, no wildcards or partial returns (ie. allow a kill on process "123" but not "1234" or "2123" ).- If the criterias are not met, do not allow a kill
Leveraging the '-p PID' argument to first find candidates:
# ps -p <PID> -o pid= -o gid=
This gives you a list (in two columns) of PIDs and GIDs. If you like, you can fetch a list of processes like:
# ps -p 123,456,7890 -o pid= -o gid=
The "=" sign after each specification suppresses the header line making further parsing of the list easier.
Add any additional columns as necessary; walk the list; match what you want; extract the first field (column) for a PID to kill()'
Regards!
...JRF...
>- If so, do they meet the GID requirement?
Just take my script above and use "-ogid=" and then check for equality:
gid=$(UNIX95=EXTENDED_PS ps -p $pid -ogid=)
if [ "$gid" != some-magic-gid ]; then
echo "skip wrong group process" 2>&1
continue
fi
You guys are the best!!!!
This is really close now. Just one last piece.....is there a way to prevent wildcards? I would like to prevent someone from executing something like "sudo scriptkill * " or "sudo scriptkill abcd". The latter returns a nasty message. The wildcard, however, is returning a lot of "interesting" possiblities....yikes!!! Is there a way to restrict the input to accept only numerical entries and no wildcards?
Thanks again!!!
Apparently I should have asked this way back at the beginning of this, but do you really want general users to be able to kill of the process of other general users? I think you may end up with a revolt on your hands.
If not, then why re-write what the kill command itself already restricts them to? As is, they can kill their own processes.
@TheJuiceman wrote:Just one last piece.....is there a way to prevent wildcards? ... Is there a way to restrict the input to accept only numerical entries and no wildcards?
Hi:
You might use this as an intialization step to valid and otherwise reduce the input arguments to only numeric values:
#!/bin/sh
typeset LIST=''
for PID in $@
do
if [ $(expr "${PID}" : '[0-9]*') -ne $(expr "${PID}" : '.*') ]; then
echo "PID value = '${PID}' is invalid"
else
LIST=$(echo ${LIST} ${PID})
fi
done
echo "Using: ${LIST}"
exit 0This would look like:
./pidlist 123 456 a 7fff 7890 PID value = 'a' is invalid PID value = '7fff' is invalid Using: 123 456 7890
Regards!
...JRF...
Thank you for the help everyone!!!
I have a script that looks like it would work. The reason for the need for such a script is for some developers. They occassionally will need to kill a user process when testing or troubleshooting. This will allow them to do it without putting the system in harms way.
The group ID that is applicable to what is needed is "20". Below is what has been assembled by your contributions:
#!/usr/bin/sh
for pid in $*; do
if [ $(expr "${pid}" : '[0-9]*') -ne $(expr "${pid}" : '.*') ]; then
echo "Variable constraint is invalid. "
echo "You must use a numeric process ID with this command" && exit
fi
ps -p $pid >/dev/null 2>&1
[[ $? != 0 ]] && echo "Process doesn't exist: $pid" && continue
gid=$(UNIX95=EXTENDED_PS ps -p $pid -ogid= )
if [ $gid != "20" ]; then
echo "Cannot kill system process: $pid" 2>&1
continue
fi
kill $pid
done
Testing by echoing the $pid and trying various possible variables looks promising. I have it terminating if it picks up a non-numeric entry, which seems to work better than letting it continue. What do you all think? See any problems or gotchas?
Thanks again for your help!!!
@TheJuiceman wrote:Thank you for the help everyone!!!
I have a script that looks like it would work.
Below is what has been assembled by your contributions:
What do you all think? See any problems or gotchas?
Hi (again):
This looks reasonable. Instead of :
[[ $? != 0 ]] && echo "Process doesn't exist: $pid" && continue
...my preference (in part for clarity) would be:
[[ $? != 0 ]] && { echo "Process doesn't exist: ${pid}"; continue; } Regards!
...JRF...
And one last refinement:
[[ $? != 0 ]] ...
vs
[[ $? -ne 0 ]] ...
The comparison != is for strings, while -ne is for numbers. For simple cases like this, the two methods produce the same results. But consider magnitude comparisons where 12 is less than 9 when comparing strings. By using the numeric comparisons (-lt -le -eq -gt -ge -ne) errors with variables that have non-numeric characters will be caught rather than producing an unpredictable result.