2

Not a long time ago we found out about pkill and we had in mind to start using it in a setuid (for root) script for global clean-up of processes. This could save us lots of stupid maintenance where some clients can´t remove general resources using their scripts only due not important permission limitations.

However, after some struggling we only came up with pkill -v -u root <name> (so far we intent to make it simple and prevent from devolving into a long and ugly script with sed,awk,grep and so on). Of course it doesn´t work — it just kills everything but the processes that match the given name.

Is there a any short modified version of that pkill command that get us the results we need?

P.S: I want to avoid any discussions about the morality of giving some sort of root power to the users.

The running OS is solaris 10, if that matters.

  • Please specify the constraints and aims of which processes you want killed. – Otheus Mar 2 '16 at 20:54
  • Which shell do you use? – MichalH Mar 2 '16 at 21:46
  • Are there any group memberships in common between the users and the process(s) that need to be killed? If so, this might help: serverfault.com/questions/325128/… – Andrew Henle Mar 3 '16 at 11:49
2

This will generate a list of processes with the given name, and a list of processes with the given name running as root, then run comm to find processes in the first list that are not in the second list, then kill them.

#!/bin/ksh
if test $# != 1
then
    echo usage: "$0" processname
    exit 1
fi
pname="$1"
kill $(comm -23 <(pgrep "$pname"|sort -n) <(pgrep -u root "$pname"|sort -n))
1

I am using SUSE , but assuming it works similar on Solaris.

Kill process for a User

In order to kill a process by its user id You can do following

#pkill -U <username>

Check processes for a User

If you just want to check what processes are running for a particular user before killing his processes, you can use: pgrep -U <username

Kill process for multiple users

#pkill -U <user1>,<user2>,<user3> and so on.

Kill all users except root

I know you are avoiding sed , awk , grep. But its easier to write a script rather than having to type each user name. Here is a sample. Please check on sanbox before executing on production.

ps -aef |grep -v UID |grep -v root |awk '{print $1}' 
 |sort -u |while read name
do 
echo "Killing process for user $name"
pkill -U $name
done
  • You can preview the script by replacing pkill -U $name with echo "pkill -U $name" to make sure it works and kills right processes – Anil_M Mar 2 '16 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.