Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
S : process is waiting (i.e. sleeping less than 20 seconds)
s : session leader
Have a look at pgrep ( https://linuxize.com/post/pgrep-command-in-linux/ ) - short and nice
plus the principle "Don't do what you shouldn't do"
And the operating conditions of the script - only cron.
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question