Developing process check and execution program in ubuntulinux 18.04 environment. (python3)

Asked 1 years ago, Updated 1 years ago, 103 views

About processes running in ubuntulinux 18.04 environment We are developing a program that checks the execution and termination status of the process and allows the process to be re-executed if it is terminated. (python3)

So I'm just trying to find the process with the shell command and check it as 0 or 1 to run the process.

import subprocess

mResult = subprocess.check_output("pgrep -lf processname | wc -l", shell=True) print('Result : ', int(mResult))

If you run it as an example, as shown above, If the returned mResult value is enabled, it will be 3 or 2. (The above values change at all times depending on the situation...)

However, if you enter 'pgrep -lf processname | wc -l' directly from the terminal, If it's running, it's returned to 1 or 0.

I would like to know the reason why it is returned to a completely different value when executed on Python.

Or if there is a better way to develop the above program, please help me.

python3 shell-script subprocess

2022-09-20 19:20

1 Answers

We recommend that you program the system area with C rather than Python.

A pid directory is created under /proc, and you can use the information in this directory.

And if you want to use a command like grep, it's more efficient to write a shell script instead of Python.


2022-09-20 19:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.