xrazx Posted August 4, 2017 Posted August 4, 2017 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! -xRaZxP.S. Sorry if this is a double post as this is the second time writing this post.
Krydos Posted August 4, 2017 Posted August 4, 2017 (edited) 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 July 3, 2018 by Krydos clarification
xrazx Posted August 4, 2017 Author Posted August 4, 2017 Wow, I should have posted earlier! Thanks for the help; this seems to be exactly what I want to do! I'll let you know how it turns out! -xRaZx
xrazx Posted August 4, 2017 Author Posted August 4, 2017 (edited) 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 August 4, 2017 by xrazx
Krydos Posted August 4, 2017 Posted August 4, 2017 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.
xrazx Posted August 5, 2017 Author Posted August 5, 2017 Ah, sorry for the confusion, I totally forgot about permissions! (Changed test file to 755 but not main file...) Thank you so much for all your help! -xRaZx
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now