LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-05-2012, 11:32 AM   #1
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Rep: Reputation: 0
Root PID's


Hi,

I'm trying to write a little script to kill certain PID's. Which I seem to have done, but I also want to make sure that no root processes are killed too.

How can I tell the diffrence between a user PID and a Root PID?

I know by using ps -ef I can see the user associated with the PID but I have no idea how to add this in to my script.

Here is what I have so far (don't laugh! )

Code:
#!/bin/bash
# print the process list
ps -ef

# if the string is empty
if [ -z $(pidof gcalctool) ];

	then
		echo "This process is currently not running"
# if process is running kill and echo

else 
	kill -9 $(pidof gcalctool) ;

echo "The calculator was terminated" 


fi
 
Old 04-05-2012, 11:36 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,353

Rep: Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989Reputation: 1989
One option is to just run the script as a user. Users can't kill root's processes.
 
Old 04-05-2012, 11:52 AM   #3
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
you should never use "kill -9 something", as the process got no chance to end properly.
A very bad habbit.
First try HUP and the other signals. The very last resort is "kill -9 ...."

ps -u someuser
gives you all processes run under the according UID
 
1 members found this post helpful.
Old 04-05-2012, 12:02 PM   #4
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by uhelp View Post
you should never use "kill -9 something", as the process got no chance to end properly.
A very bad habbit.
First try HUP and the other signals. The very last resort is "kill -9 ...."

ps -u someuser
gives you all processes run under the according UID
Thanks everyone

So how exactly would I incorporate the ps -u in to a script?

What I basically want to do is, if the UID is root, don't kill the process.
 
Old 04-05-2012, 12:08 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
There should be system users to consider as well, so better make it:
Code:
PSUIDMIN=$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)
/bin/ps --no-header ax -eo pid,uid,cmd | while read PSPID PSUID PSARGS; do
 [ $PSUID -ge ${PSUIDMIN:=500} ] && echo "$PSPID $PSARGS"
done
 
Old 04-05-2012, 12:20 PM   #6
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
There should be system users to consider as well, so better make it:
Code:
PSUIDMIN=$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)
/bin/ps --no-header ax -eo pid,uid,cmd | while read PSPID PSUID PSARGS; do
 [ $PSUID -ge ${PSUIDMIN:=500} ] && echo "$PSPID $PSARGS"
done
Thanks unspawn, I really can't make top from tail with that though. How could i put that in to my script?
 
Old 04-05-2012, 12:36 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Code:
#!/bin/bash --
# Disable after debugging:
set -vxe
# Get the minimal value for unprivileged user Ids:
PSUIDMIN=$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)
# Print the process list and read in three variables:
/bin/ps --no-header ax -eo pid,uid,cmd | while read PSPID PSUID PSARGS; do
        # If user Id is ( not root and not system user ) echo and kill:
        if [ $PSUID -ge ${PSUIDMIN:=500} ]; then
                echo -en "Killing \"${PSARGS:0:24}\": "
                kill -15 $PSPID && sleep 10s
                kill -15 $PSPID && sleep 10s
                kill -9 $PSPID && echo "done." || echo "failed."
        fi
done
exit 0

Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq 
http://wooledge.org/mywiki/BashPitfalls"; }
 
Old 04-05-2012, 12:54 PM   #8
Dale255
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Code:
#!/bin/bash --
# Disable after debugging:
set -vxe
# Get the minimal value for unprivileged user Ids:
PSUIDMIN=$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)
# Print the process list and read in three variables:
/bin/ps --no-header ax -eo pid,uid,cmd | while read PSPID PSUID PSARGS; do
        # If user Id is ( not root and not system user ) echo and kill:
        if [ $PSUID -ge ${PSUIDMIN:=500} ]; then
                echo -en "Killing \"${PSARGS:0:24}\": "
                kill -15 $PSPID && sleep 10s
                kill -15 $PSPID && sleep 10s
                kill -9 $PSPID && echo "done." || echo "failed."
        fi
done
exit 0

Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq 
http://wooledge.org/mywiki/BashPitfalls"; }
Way over my head. I just want to be able to stop the process being killed if the UID is root.
 
Old 04-06-2012, 09:38 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
Quote:
Originally Posted by Dale255 View Post
I just want to be able to stop the process being killed if the UID is root.
I thought you wanted to kill non-root processes?..


Quote:
Originally Posted by Dale255 View Post
Way over my head.
Try this:
Code:
#!/bin/bash --
# Remove uncomment the line below when you understand what the script does:
set -vxe
PSUIDMIN=$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)
/bin/ps --no-header ax -eo pid,uid,cmd | while read PSPID PSUID PSARGS; do
 if  [ $PSUID -ge ${PSUIDMIN:=500} ]; then
  # Remove the echo and quotes if you really, really want to kill:
  echo "kill -15 $PSPID"
 fi
done
exit 0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I assign a Command to a specific PID so I can kill a predetermined PID johnmccarthy Linux - Newbie 1 11-03-2011 09:41 AM
What is the more easier way to check the pid and kill the pid cmx08 Linux - General 5 09-09-2008 11:57 PM
kill pid.... won't work with 'pid' variable given.. sachitha Programming 6 03-06-2006 08:48 PM
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 11:32 PM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 09:05 PM


All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration