Jump to content

Continuous run of python using subprocess does not work


andyye03

Recommended Posts

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.")

 

 

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...