0

I am Looking for some help in creating a script to kill processes, but return an error message if they are owned by root. Firstly I would like to say that I am a total beginner at this. At the moment I currently have: kill ps -aef | grep $1 | grep -v grep | awk '{print $2}' Which works ok to kill a process name taken as an argument. but I cant get it work as an if statement, to stop it from killing a process if the the owner is root.

0

2 Answers 2

2
pgrep -u 0 "$1" && echo "Can't kill process: Owned by root" || killall "$1"

The pgrep -u 0 will only search for processes owned by UID 0 (root). If the process isn't found (so its either not existing or not running as root), prgep will return a non-zero return code and the killall command will be executed. If prgrep returns a zero return code, the echo will be executed, but not the killall command.

1

try

ps -aef | grep $1 | egrep -v grep | awk '$1 != "root" {print $2}'

the statement $1 != "root" will filter out process own by root. You might still get error on daemon process (e.g. www-data if you are running apache).

You must log in to answer this question.

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