Jump to content

Recommended Posts

Posted

I tried to use subprocess as a way to continuously run a python script even after closing the browser, using the start and stop pages on https://flazepe.gitbook.io/heliohost/tutorials/discord-bot

But no matter how long I waited, the script only ran when the "start" page was open, and when I tried to stop the script, it told me that there was no script to stop.

 

 

my start script is

 

 

 

 

#!/usr/bin/python3.7

print("Content-Type: text/html\n\n")

import os, subprocess, signal


counter = 0
p = subprocess.Popen(['ps', '-u', 'andyye03'], stdout=subprocess.PIPE)
# must match your username --------^^^^^^^^
out, err = p.communicate()
for line in out.splitlines():
    if 'gaonnuriallimi.py'.encode('utf-8') in line:
#     ^^^^^^^^^^^----- this has to match the filename of your bot script

        counter += 1
        print("Bot already running.")
if counter == 0:
    subprocess.Popen("/home/andyye03/gaonnuriallimi.py")
#                         ^^^^^^^^-- be sure to update it to your username

    print("Bot started!")

 

 

 

and my stop script is

 

 

 

 

#!/usr/bin/python3.7

import os, subprocess, signal

print("Content-Type: text/html\n\n")

counter = 0
p = subprocess.Popen(['ps', '-u', 'andyye03'], stdout=subprocess.PIPE)
# must match your username --------^^^^^^^^

out, err = p.communicate()
for line in out.splitlines():
    if 'gaonnuriallimi.py'.encode('utf-8') in line:
#     ^^^^^^^--- this has to match the filename of your loop

        counter += 1
        pid = int(line.split(None, 1)[0])
        print("Stopping bot.")
        os.kill(pid, signal.SIGTERM)

if counter == 0:
    print("Already stopped.")

 

 

 

Posted

the script only ran when the "start" page was open

That's funny, because you have >>>5<<< of them running at once right now:

# ps aux | grep -v grep | grep andyye03
andyye03  1863  0.2  0.2 272596 45280 ?        S    15:45   0:08 /usr/bin/python3.7 /home/andyye03/public_html/cgi-bin/gaonnuriallimi.py
andyye03  8600  0.2  0.2 270036 42480 ?        S    15:50   0:07 /usr/bin/python3.7 /home/andyye03/public_html/cgi-bin/gaonnuriallimi.py
andyye03 22318  0.3  0.2 268244 41036 ?        S    15:44   0:09 /usr/bin/python3.7 /home/andyye03/public_html/cgi-bin/gaonnuriallimi.py
andyye03 23821  0.2  0.2 272600 45068 ?        S    15:46   0:08 /usr/bin/python3.7 /home/andyye03/public_html/cgi-bin/gaonnuriallimi.py
andyye03 25727  0.2  0.2 266092 38652 ?        S    15:56   0:06 /usr/bin/python3.7 /home/andyye03/gaonnuriallimi.py
I'll kill them for you.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...