Execute shell script with ampersand from python program

I want to post my long Python job using ampersand. I'm going to disconnect this process from an interactive Python program using a subprocess call.

How would I keep track of the submitted work programmatically in case I want to exit the work from a menu option?

Example of interactive program:
Main Menu
1. Submit long running job &
2. End long running job

      

+3


source to share


2 answers


If you are using a python module subprocess

, you don't really need to reuse it again with &

? You can just save your Popen

job tracking object and it will run while another python process is in progress.



If your "external" python process exits, which track do you need to keep? Will pgrep

/ come in handy pkill

? Alternatively, you could have a long log of your PID code execution, often found in / var / run somewhere, and use that to track if the process is alive and / or signal it.

+4


source


You can use Unix signals. Here we will write SIGUSR1

to tell the process to communicate some information to STDOUT

.

#!/usr/bin/env python
import signal
import sys
def signal_handler(signal, frame):
    print('Caught SIGUSR1!')
    print("Current job status is " + get_job_status())

signal.signal(signal.SIGUSR1, signal_handler)

      



and then from the shell

kill <pid> --signal SIGUSR1

      

0


source







All Articles