Jump to content

Python Script Behavior?


xrazx

Recommended Posts

Hello,

 

I just want to ask a few questions before I try anything.

 

Can python scripts be run other than through utilizing CGI or CRON?

 

Also, if a python script is run through CGI, will the script stop when I close the tab/loose connection?

 

If so, is there anyway to allow the script to continue running? And is there anyway I can kill it if necessary?

 

Basically, I want to run my python script as a singleton and be able to let it run and stop it when necessary.

 

Hopefully what I'm trying to do is not against any terms.

 

Thanks in advance!

 

-xRaZx

P.S. Sorry if this is a double post as this is the second time writing this post.

Link to comment
Share on other sites

Great question! Since internal cron are limited to 2 per day this is a good thing to discuss.

 

First I created a simple loop that would run forever.

 

loop.py:

#!/usr/bin/python3.6

import time

while True:
  print("waiting...")
  time.sleep(5)
Next, I created a way to start the loop with a cgi script. You just open this script in your browser, and if the loop isn't already running it will start it up and it will continue to run in the background.

 

start.py:

#!/usr/bin/python3.6

import os, subprocess, signal

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

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

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

    counter += 1
    print("Loop already running.")

if counter == 0:
  os.system("at now <<< '/home/krydos/public_html/cgi-bin/loop.py'")
# absolute path to your loop --^^

  print("Loop started!")
Finally we need a way to stop the loop from a cgi-script as well.

 

stop.py:

#!/usr/bin/python3.6

import os, subprocess, signal

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

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

out, err = p.communicate()
for line in out.splitlines():
  if 'loop.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 loop.")
    os.kill(pid, signal.SIGTERM)

if counter == 0:
  print("Already stopped.")
All of this code is tested on Tommy. Edited by Krydos
clarification
Link to comment
Share on other sites

Hmm... I seem to be getting 500 internal errors when trying to run this.

 

Am I doing anything wrong by trying to run the start.py and stop.py by navigating to the url where it is located? Or should I make a cron job? If so, what would the intervals be?

 

I have a test.py that just prints a message which is working fine in the same folder, so I'm pretty sure it isn't a problem with getting python to run. Is there anyway I could get the error message?

 

EDIT: Sorry for the double post

 

-xRaZx

Edited by xrazx
Link to comment
Share on other sites

Ok, first of all none of your scripts are executable because they're 644 not 755 like they have to be. Next, unless you enable .py scripts to be executable outside cgi-bin using .htaccess it's not going to work there. Moving start.py and stop.py to cgi-bin is the easiest option. Also, it doesn't really matter where loop.py is. You could put it somewhere like /home/pyraz/loop.py It doesn't output the content-type header so it'll always give a 500 error when you try to execute it as a cgi through a browser.

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