I am using
systemd to run a shell script at boot,
start.sh, that eventually starts
main.py, my program’s main Python script.
I would like to record the PID of
main.py so it can be programmatically killed later.
I first tried the following, but found that the Python script didn’t stay alive, even though the PID was recorded (note that I’m redirecting error output to a log file).
sudo python main.py 2> >(sudo tee /var/log/app.log) & echo $1 > /tmp/app.pid
However, when removing the
&, the script stays alive, but the PID script never reaches the line where it records the PID.
sudo python main.py 2> >(sudo tee /var/log/app.log) echo $1 > /tmp/app.pid
How can I run
main.py in a way that keeps it alive, but still gives me the PID?