Process existence or/and permission


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Process existence or/and permission
# 1  
Old 06-26-2017
Process existence or/and permission

Hi,

In a bash script, I'm using kill -0 to test if I have permission to kill a process. There are 3 cases:
- the process exists and I have permission: OK
- the process doesn't exist and it's OK (because I decided to ignore processes that are already dead).
- I don't have permission and I want to raise an error.

If I use ps $pid, I cannot tell the difference between a process that does not exist and a process I cannot "see":
Code:
santiago@alphard:~$ ps 1 > /dev/null && echo OK || echo ERR
ERR
santiago@alphard:~$ sudo ps 1 > /dev/null && echo OK || echo ERR
OK

If I use kill -0 $pid, I could tell the difference reading stderr but it would be language specific:
Code:
santiago@alphard:~$ for pid in 1 123; do for lc in C fr_FR.utf-8; do LC_ALL=$lc; kill -0 $pid; echo Status is $?; done; done
-su: kill: (1) - Operation not permitted
Status is 1
-su: kill: (1) - Opération non permise
Status is 1
-su: kill: (123) - No such process
Status is 1
-su: kill: (123) - Aucun processus de ce type
Status is 1

Question: How can I programmatically tell if a process exists and if I have permission on it?

Regards
Santiago
# 2  
Old 06-26-2017
If you only worry about the language, you could use something like

Code:
LC_ALL=C kill -0 $pid

(or: LC_ALL=EN_en.utf-8 kill .....)

i.e. change the language to a well-defined value just for the scope of this command, and get the output always in English.
# 3  
Old 06-26-2017
You can test with ps
Code:
if { ps -p $pid && ! kill -0 $pid; } >/dev/null 2>&1 ; then echo $pid is not killable; else echo happy; fi

Or
Code:
if { ! ps -p $pid || kill -0 $pid; } >/dev/null 2>&1 ; then echo happy; else echo $pid is not killable; fi

# 4  
Old 06-26-2017
Thanks rovf,
That would mostly work I think but beside the language barrier, I don't like relying on error messages that could vary from one implementation to another. But that's the best answer so far.

Thanks MadeInGermany,
My problem is that on some systems, there are some processes that I just don't see as a normal user (not only no permission, really don't see) so I cannot rely on ps. Take the following example:
Code:
santiago@alphard:~$ ps 1; echo $?
  PID TTY      STAT   TIME COMMAND
1

It looks like process 1 does not exists but kill and sudo prove otherwise:
Code:
santiago@alphard:~$ kill -0 1
-bash: kill: (1) - Operation not permitted
santiago@alphard:~$ sudo ps 1; echo $?
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     2:55 init [2]
0

# 5  
Old 06-27-2017
What OS has such a broken ps?
# 6  
Old 06-28-2017
Code:
santiago@alphard:~$ uname -srv
Linux 3.14.32-xxxx-grs-ipv6-64 #9 SMP Thu Oct 20 14:53:52 CEST 2016
santiago@alphard:~$ cat /etc/debian_version
7.11
santiago@alphard:~$ bash --version | head -1
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)
santiago@alphard:~$ ps --version
procps-ng version 3.3.3

I have many Debian and they all work fine except 2 of them. It is kernel related I think.

Here is a list of some of the kernels working fine (they are all Debian distribution kernels):
  • 2.06.26-2-686
  • 2.06.32-5-686
  • 2.06.32-5-amd64
  • 3.02.00-4-686-pae
  • 3.02.00-4-amd64
  • 3.16.00-4-amd64
Here are the 2 kernels that do NOT work as I wish (they are OVH Dedicated Server Debian custom kernel):
  • 2.06.21.05-grsec-xxxx-grs-ipv4-32
  • 3.14.32-xxxx-grs-ipv6-64
Unfortunately, I failed to upgrade the kernel because (according to the hosting company), the hardware is incompatible.

I can live with a non standard kernel but if anyone knows where I can tweak the setting that makes other users processes invisible, I'd take some help with great gratitude.

Regards
Santiago
# 7  
Old 06-28-2017
If /proc/<pid>/ is unreadable then it's a kernel thing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Permission to kill a process

I'm on AIX. I have triggered an infinite loop process (to keep looking for input file availability for further process). At present only I can kill the process. In case my colleague wants to kill the process for any reason, how do I provide permission to others to kill the process? Currently... (3 Replies)
Discussion started by: krishmaths
3 Replies

2. Shell Programming and Scripting

File existence

Hope someone can help me on this In a directory ,files are dynamically generated.I need a script to do the following if files are not received for more than 2 hours or if the received file is empty then do something How can I put that in a script.Thank you eg. in cd /dir_name the... (13 Replies)
Discussion started by: haadiya
13 Replies

3. Shell Programming and Scripting

File existence

Hi I'm using the below command in shell script to check for file exists in the path if ..... fi path and test are variables path and the file exists but the commands inside if condition is executed (! operator used) Is the above way of checking for file existence is correct? ... (4 Replies)
Discussion started by: vinoth_kumar
4 Replies

4. Shell Programming and Scripting

bash about url existence

Hi, I want to make a bash script which is running like : 1.sh http://www. google.com and check if the url does exist printing a message. I want to save the source code of this page in a file. Could you help me ? (4 Replies)
Discussion started by: peter20
4 Replies

5. Shell Programming and Scripting

Help to Monitor the existence of a file

Hi Folks, Please tell me the unix shell script command to check for the existence of some .done file in a location on the UNIX server. (1 Reply)
Discussion started by: dinesh1985
1 Replies

6. Shell Programming and Scripting

check existence of the path

Hi How can I check if the path exist or not? echo "Enter path:"; read my_path; ##I should check whether my_path exists or not.... (5 Replies)
Discussion started by: tjay83
5 Replies

7. Shell Programming and Scripting

File existence using ls

Hi I want to check a particular file is available or not. But i know only the pattern of that file sat AB1234*.txt.I need the latest file name and it ll be used in the script. How can i do this using ls -ltr command. Thanks, LathishSundar V (2 Replies)
Discussion started by: lathish
2 Replies

8. UNIX for Dummies Questions & Answers

Permission on files restricted to a process

Hello, I have this process app.fcgi and a directory containing images. I'd like to ensure that only app.cgi can access those images and more generally that folder.Thanks! (1 Reply)
Discussion started by: JCR
1 Replies

9. Shell Programming and Scripting

File existence

Hey all, I have total new with shell scripting so I don't know if what I need to do even possible, here it is...for a duration of time (say...1 hour) I need to check for the existence of a particular file, if it exists then I will invoke a java program or I will continue to check until a)... (2 Replies)
Discussion started by: mpang_
2 Replies

10. UNIX for Dummies Questions & Answers

Loop for file existence

I wasn't sure if I should post it here of in the Shell Script category, but I figured it was definitely a newbie question. I'm trying to write a script that will check for the existence of a specific file (or for any files within the directory) and then take specific actions. I've removed all... (2 Replies)
Discussion started by: bd_joy
2 Replies
Login or Register to Ask a Question