Shall I go for uid or ppid?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shall I go for uid or ppid?
# 1  
Old 04-26-2010
Shall I go for uid or ppid?

Hi Guys,
I'd like to ask your advice on the following, I've written this script to terminate a given process by name:
Code:
#!/bin/bash
echo 'Please enter the process you wish to terminate'
read process
pid=$(pidof $process)
kill -9 $pid
echo $2

to make it safer I want it to reject the killing of root owned processes.

I'm thinking of doing it using an if condition in two ways, either by the process parend ID or the process UID but I'm having some trouble getting there:
Code:
#!/bin/bash
echo 'Please enter the process you wish to terminate'
read process
pid=$(pidof $process)
ps -ef | grep '$process' | awk '{print $2}'
if [ '$2'="root"]
then
echo "The process cannot be terminated for safety reasons"
else
kill -9 $pid
echo $3

From what I understand there is no real command to get a ppid of a process so I'm trying to make it through the UID. What would you suggest?

Lora

Last edited by Franklin52; 04-26-2010 at 06:22 AM.. Reason: Please indent your code and use code tags!
# 2  
Old 04-26-2010
Unless your script runs with with full privilege, processes owned by root will not be stoppable.

If your system has the /proc filesystem you have a command called either ptree or pstree (Solaris; Linux). You can use one of them to find a ppid.

Using a uid may be a problem - on many systems there are generic user names that applications installations have created & are used to run dozens of batch and background processes. Example: oracle. Kill one of them and you have serious problems.

I guess I do not understand your approach at all.
# 3  
Old 04-26-2010
  • Another user id but root has usually no permission to kill processes of root.
  • Your script assmues that people would have to know which processes exist and how to get the pid of the one to kill. If they are capable to issue a ps they are usually also able to issue a kill - so I don't know if a script for such a task is useful (no offence).
  • Anyway you could do a "grep -v ^root" to spare out root processes.
  • ppid is always the pid listed in the second row following a ps -ef output. The header should show. Also /proc has much info about your processes.


---------- Post updated at 01:13 PM ---------- Previous update was at 01:13 PM ----------

Sorry, was interrupted while writing an answer - see Jim's answer Smilie
# 4  
Old 04-26-2010
Thank you Jim and Zaxxon,

Maybe I'm just overcomplicating things, the main idea was for the script not to take any root processes as argument

I think sparing root processes with grep -v ^root seems the quickest way to target this, but I am not sure of how to implement it?

Lora
# 5  
Old 04-26-2010
It's very common to use grep to grovel through ps output, but keep in mind that most approaches that do this do so in a very sloppy manner, which is prone to false positives.

Typically, it's better to use AWK, so that at the very least the process name sought can be easily constrained to the the beginning of the command field (this can be done with grep, but not as easily). Otherwise, a matching username, option on the command line, or environment variable or value (which may be present depending on ps options) could match unwanted processes.

Most grep approaches also use two greps, the second to exclude the first grep from the list, which means such an approach isn't suitable if the process name contains "grep" (such as grep, egrep, fgrep, etc).

If your system has pgrep/pkill, they are almost always the best option.

Unless your script is running with root privileges, regardless of which command you use, you cannot kill any root-owned processes. Not only that, you cannot kill any processes by other unprivileged users. You can only kill processes that you own.

If the script runs SUID root, make sure you understand the security implications of such a decision.

Quote:
Originally Posted by Lora Graham
From what I understand there is no real command to get a ppid of a process so I'm trying to make it through the UID. What would you suggest?
You can use ps to get a processes' parent id. For example, the following posix-compliant ps invocation returns the current shell execution environment's parent process id:
Code:
ps -o ppid= -p $$

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

PID and PPID - please explain :(-

Hi, I need some help understanding PID and PPID that is shown by the ps -ef output. OS is Solaris 5.8. :wall: There are several Oracle databases and processes running on this server and they all have the same PPID. Does that mean they are all spawned off the same startup script? I then... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. UNIX for Dummies Questions & Answers

Process Name from PPID

Hi, I am currently trying to pin down a process NAME (could be a cron job or a tty) in Solaris. First I obtained a pid and then a ppid. I want to take the PPID and from there find the program/process that is making the VSH go high (cpu usage). I am currently trying to go up the chain using PS... (2 Replies)
Discussion started by: troystevens
2 Replies

3. UNIX for Advanced & Expert Users

PPID differs in script and prompt

I tried several times to get answer to the below problem. Someone can please help me? $ cat p1.sh #!/bin/sh `./c1.sh &` while # indefinite loop do x=5; done $ cat c1.sh # sleep for 10 sec and exit #!/bin/sh sleep 10; Execute P1 as ./p1 & $ ps -eaf | grep c1... (1 Reply)
Discussion started by: alexalex1
1 Replies

4. UNIX for Dummies Questions & Answers

Starting a process whose ppid should be 1

How can I start a process with ppid as 1 instead of my current shell pid? (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

5. Solaris

why process PPID changes to 1

Hi All, I have just started using SUN Solaris o.s version is :SunOS spdwa013(my server name) 5.8 Generic_117350-61 sun4u sparc SUNW,Sun-Fire-480R My problem is that i have some processes running on that server they have a PID and a PPID. But sometimes the PPID for a particular process... (2 Replies)
Discussion started by: usha rao
2 Replies

6. UNIX for Advanced & Expert Users

' ps v PPID' command and real memory

hi Guys, Just wondering whether if I execute the ps v 1818740 and I get the below ouput what column should I use If I need to calculate how much real memory (private) is use by those process. Thanks. PID TTY STAT TIME PGIN SIZE RSS LIM TSIZ TRS %CPU %MEM COMMAND ... (1 Reply)
Discussion started by: hariza
1 Replies

7. UNIX for Dummies Questions & Answers

Comparing CRON PID w/Current PPID

All, I've got a script that needs to check if it was started by cron. The code seems to be right, but it's not running correctly if cron starts it. Am I getting the pid's correctly? I'm not having any luck figuring it out. :confused: Any help is appreciated! CRON_ID=$(ps -aef | grep... (1 Reply)
Discussion started by: GregWold
1 Replies

8. UNIX for Dummies Questions & Answers

what is the meaning of ppid 1?

Is there any meaning on parent process id when it is equal = 1? It is found that most of the ppid is lower than pid of the same process by 1 when perform ps -ef but notice that some process's ppid is 1 (2 Replies)
Discussion started by: darkrainbow
2 Replies

9. Shell Programming and Scripting

Need a script to kill processes with PPID of 1

Hi, I have been trying to come up with a script to run as a cron job to kill any processes that have PPID of 1. I have created a file that contains the PID and the PPID. How can I read this file and then execute a kill on any PID where PPID is 1. The file looks like this: 4904 1 4455 1... (5 Replies)
Discussion started by: lbaysdon
5 Replies

10. UNIX for Advanced & Expert Users

strange pid and ppid problem

Hi all, Please look into the following code : int main() { char command; int pid, ppid; ppid = getpid(); /* Get the parent pid */ pid = fork(); /* Fork */ if ( pid ==0 ) { sprintf( command, " gdb a.out %d ", ppid ); printf( "Command line is %s\n", command ); system( command... (3 Replies)
Discussion started by: asvija
3 Replies
Login or Register to Ask a Question