Home > Enterprise >  Start python scripts from a python script
Start python scripts from a python script

Time:11-08

Im running a linux server that runs 4 python scripts on startup, but sometimes some of them crash and I need to restart it manually, so I thought about running a new script that checks if one of them stopped through ps -fA and restart. Here is the code:

# Search output
generaldata = "site_general_data"
amsniffer = "site_AM_sniffer"
b3sniffer = "site_B3_sniffer"
cryptosniffer = "site_CRYPTO_snifferB"

# Output active
outputa = subprocess.check_output('ps -fA | grep python',shell=True)

# Verify Scripts
if generaldata not in str(outputa):
    subprocess.run('sudo python3.6 /bin/site_general_data.py', shell=True)
    ativogen = "General Data inactive"
    print("General Data inactive")
else:
    ativogen = "General Data active"
if amsniffer not in str(outputa):
    subprocess.run('sudo python3.6 /bin/site_AM_sniffer.py', shell=True)
    ativoam = "AM Sniffer inactive"
    print("AM Sniffer inactive")
else:
    ativoam = "AM Sniffer active"
if b3sniffer not in str(outputa):
    subprocess.run('sudo python3.6 /bin/site_B3_sniffer.py', shell=True)
    ativob3 = "B3 Sniffer inactive"
    print("B3 Sniffer inactive")
else:
    ativob3 = "B3 Sniffer active"
if cryptosniffer not in str(outputa):
    subprocess.run('sudo python3.6 /bin/site_CRYPTO_snifferB.py', shell=True)
    ativocry = "Crypto Sniffer inactive"
    print("Crypto Sniffer inactive")
else:
    ativocry = "Crypto Sniffer active"

The scripts are site_general_data, site_AM_sniffer, site_B3_sniffer and site_CRYPTO_snifferB.

The problem is that I want this script to run with no interruptions, I wanted it to start the aborted python script in a new shell and keep checking, but right now it starts running the script in the same shell. I've also explored the Popen documentation under subprocess but still no success. Is there a way to do this?

CodePudding user response:

As proposed by @Jeppe in the comments appending & was enough to solve my problem, the script now runs with no interruption, restarting any crashed applications.

  • Related