I'm writing a program in Python to see which process is using a particular file.
import psutil
for i in psutil.process_iter():
print(i)
print(i.open_files())
In it, I learned how to use psutil to identify the files used by process. This works fine if the same user opens the file.
However, when I become another user, I get the following error.
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 1517, in wrapper
return fun(self, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 2036, in open_files
path = readlink(file)
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 212, in readlink
path = os.readlink(path)
PermissionError: [Errno 13] Permission denied: '/proc/371/fd/0'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "//main.py", line 4, in <module>
print(i.open_files())
File "/usr/local/lib/python3.9/site-packages/psutil/__init__.py", line 1136, in open_files
return self._proc.open_files()
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 1519, in wrapper
raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=371, name='bash')
I'm looking for a solution to this problem by changing permission settings or changing python code. I would be happy if you could tell me.
root@3b17530ce829:/# ls -al /proc/371/fd/0
ls: cannot read symbolic link '/proc/371/fd/0': Permission denied
lrwx------ 1 hashito hashito 64 Dec 12 01:57 /proc/371/fd/0
There seems to be a problem with the permissions of the target file.
add..
sudo use...
sudo -E python main.py
psutil.Process(pid=1, name='bash', status='sleeping', started='01:23:19')
[]
psutil.Process(pid=327, name='bash', status='sleeping', started='01:51:23')
[]
psutil.Process(pid=370, name='su', status='sleeping', started='01:57:01')
[]
psutil.Process(pid=371, name='bash', status='sleeping', started='01:57:01')
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 1517, in wrapper
return fun(self, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 2036, in open_files
path = readlink(file)
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 212, in readlink
path = os.readlink(path)
PermissionError: [Errno 13] Permission denied: '/proc/371/fd/0'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "//main.py", line 4, in <module>
print(i.open_files())
File "/usr/local/lib/python3.9/site-packages/psutil/__init__.py", line 1136, in open_files
return self._proc.open_files()
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 1519, in wrapper
raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=371, name='bash')
root@3b17530ce829:/# python main.py
psutil.Process(pid=1, name='bash', status='sleeping', started='01:23:19')
[]
psutil.Process(pid=327, name='bash', status='sleeping', started='01:51:23')
[]
psutil.Process(pid=370, name='su', status='sleeping', started='01:57:01')
[]
psutil.Process(pid=371, name='bash', status='sleeping', started='01:57:01')
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 1517, in wrapper
return fun(self, *args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 2036, in open_files
path = readlink(file)
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 212, in readlink
path = os.readlink(path)
PermissionError: [Errno 13] Permission denied: '/proc/371/fd/0'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "//main.py", line 4, in <module>
print(i.open_files())
File "/usr/local/lib/python3.9/site-packages/psutil/__init__.py", line 1136, in open_files
return self._proc.open_files()
File "/usr/local/lib/python3.9/site-packages/psutil/_pslinux.py", line 1519, in wrapper
raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=371, name='bash')