3

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.

3
  • Please specify the constraints and aims of which processes you want killed.
    – Otheus
    Commented Mar 2, 2016 at 20:54
  • Which shell do you use?
    – MichalH
    Commented Mar 2, 2016 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/… Commented Mar 3, 2016 at 11:49

2 Answers 2

3

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 remember looking at it 5 years ago and was overwhelemed with the comm command. but now seeing this multiple times, this is a great solution!
    – Aviv
    Commented Nov 10, 2021 at 9:36
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
1
  • 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
    Commented Mar 2, 2016 at 21:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .