friends:
I am running a script in Linux:
I can use the ps
command get the process.
JavaScript
x
2
1
ps -ef | grep "python test09.py&"
2
but, how can I know the pid of the running script by given key word python test09.py&
using python code?
EDIT-01
I mean, I want to use the python script to find the running script python test09.py&
‘s pid.
EDIT-02
When I run anali’s method I will get this error:
JavaScript
1
27
27
1
Traceback (most recent call last):
2
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_psosx.py", line 363, in catch_zombie
3
yield
4
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_psosx.py", line 429, in cmdline
5
return cext.proc_cmdline(self.pid)
6
ProcessLookupError: [Errno 3] No such process (originated from sysctl)
7
8
During handling of the above exception, another exception occurred:
9
10
Traceback (most recent call last):
11
File "test11.py", line 29, in <module>
12
print(get_pids_by_script_name('test09.py'))
13
File "test11.py", line 15, in get_pids_by_script_name
14
cmdline = proc.cmdline()
15
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/__init__.py", line 694, in cmdline
16
return self._proc.cmdline()
17
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_psosx.py", line 342, in wrapper
18
return fun(self, *args, **kwargs)
19
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_psosx.py", line 429, in cmdline
20
return cext.proc_cmdline(self.pid)
21
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/contextlib.py", line 77, in __exit__
22
self.gen.throw(type, value, traceback)
23
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/psutil/_psosx.py", line 376, in catch_zombie
24
raise AccessDenied(proc.pid, proc._name)
25
psutil.AccessDenied: psutil.AccessDenied (pid=1)
26
27
Advertisement
Answer
If you just want the pid of the current script, then use os.getpid
:
JavaScript
1
3
1
import os
2
pid = os.getpid()
3
However, below is an example of using psutil to find the pids of python processes running a named python script. This could include the current process, but the main use case is for examining other processes, because for the current process it is easier just to use os.getpid
as shown above.
sleep.py
JavaScript
1
4
1
#!/usr/bin/env python
2
import time
3
time.sleep(100)
4
get_pids.py
JavaScript
1
26
26
1
import os
2
import psutil
3
4
5
def get_pids_by_script_name(script_name):
6
7
pids = []
8
for proc in psutil.process_iter():
9
10
try:
11
cmdline = proc.cmdline()
12
pid = proc.pid
13
except psutil.NoSuchProcess:
14
continue
15
16
if (len(cmdline) >= 2
17
and 'python' in cmdline[0]
18
and os.path.basename(cmdline[1]) == script_name):
19
20
pids.append(pid)
21
22
return pids
23
24
25
print(get_pids_by_script_name('sleep.py'))
26
Running it:
JavaScript
1
16
16
1
$ chmod +x sleep.py
2
3
$ cp sleep.py other.py
4
5
$ ./sleep.py &
6
[3] 24936
7
8
$ ./sleep.py &
9
[4] 24937
10
11
$ ./other.py &
12
[5] 24938
13
14
$ python get_pids.py
15
[24936, 24937]
16