Operating System - HP-UX
1752402 Members
5863 Online
108788 Solutions
New Discussion

Re: Limiting "sudo kill" to killing only user processes

 
SOLVED
Go to solution
Pete Randall
Outstanding Contributor

Re: Limiting "sudo kill" to killing only user processes

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.


Pete
James R. Ferguson
Acclaimed Contributor

Re: Limiting "sudo kill" to killing only user 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 0

 This 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...

TheJuiceman
Super Advisor
Solution

Re: Limiting "sudo kill" to killing only user processes

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!!!                                                           

James R. Ferguson
Acclaimed Contributor

Re: Limiting "sudo kill" to killing only user processes


@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...

Bill Hassell
Honored Contributor

Re: Limiting "sudo kill" to killing only user processes

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.



Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: Limiting "sudo kill" to killing only user processes

>Is there a way to restrict the input to accept only numerical entries and no wildcards?

 

The script has no idea if there were wildcards but it can validate the input.

You could use pattern matching to check:

if [[ "$pid" != [0-9][0-9]* ]]; then

   echo "Invalid PID: $pid"

   continue

fi

And you can optimize the pattern match with:

if [[ "$pid" != +([0-9]) ]]; then

 

>The comparison != is for strings, while -ne is for numbers.

 

Right.  If you like != so much, you can always use:

(( $? != 0 ))

TheJuiceman
Super Advisor

Re: Limiting "sudo kill" to killing only user processes

Thank you all very much for your help. The script looks and runs beautifully. I'm sure someone else will find this useful as well. Much appreciation and respect gentlemen!!!