A
A
Alexey2020-07-30 08:28:43
linux
Alexey, 2020-07-30 08:28:43

How to use ps to find the main child process?

I'm running a Python script via cron. I need to determine at the beginning of the script if there is an active process, so as not to launch it again if the previous launch has not yet completed. I decided to count the number of running processes, and if there are more than one, do not run the script.
Search by team name. This is what ps outputs:

10683 ?        Ss     0:00 /bin/sh -c . /home/master/.profile ; cd /var/www/site && python3 manage.py route
10685 ?        S      0:00 /var/www/site/venv/bin/python3 manage.py route


Processes 2, which is understandable. You need to find only the process that is actually "useful", i.e. 10685
Of course, you can take into account that there will be at least 2 of them, but I want Feng Shui. How to do it? And what does S and Ss stand for? Maybe there is a solution?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
U
unseriously, 2020-07-30
@alenov

S : process is waiting (i.e. sleeping less than 20 seconds)
s : session leader

V
Vitaly Karasik, 2020-07-30
@vitaly_il1

Have a look at pgrep ( https://linuxize.com/post/pgrep-command-in-linux/ ) - short and nice

E
edo1h, 2020-07-30
@edo1h

plus the principle "Don't do what you shouldn't do"

well, you should use the existing infrastructure, and not arrange a "manual sunset"
option 1:
the first link in Google for "python pid file":
https://pypi.org/project/python-pidfile/
option 2:
And the operating conditions of the script - only cron.

regardless of our attitude in systemd, we have to live with it. and this functionality is there out of the box:
https://unix.stackexchange.com/questions/203254/do...
so if the script is linux-only, I would just start it not from cron, but with systemd timers, and let it be systemd keeps track of whether the previous instance has exited or not.

I
Ivan Koryukov, 2020-07-30
@MadridianFox

Usually, checking that the program is already running is done by creating a so-called. lock file.
You need to check at the beginning of the script whether the file has been created
. another process is already running,
if not, create such a file, continue the program execution, and delete the file before completion.
When it comes to defining child processes, ps has a lot of options for filtering and formatting output. You can get the PID of child processes knowing the PID of the parent like this:
ps --ppid=1234 -o pid

V
Vladimir, 2020-07-30
@MechanID

In addition to answering unseriously,
you can see the process tree with ps auxf

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question