Reputation: 1656
I have a python script that is kicked off via command line and can be killed at any time.
I would like to create a simple web interface where a user can login and start the process and kill it on command.
I realize this is probably possible with Django and a library such as background tasks, but that seems a little overkill for something as simple as what I'm trying to do.
Is there a simple way to start and stop a python job via a web page?
Upvotes: 1
Views: 81
Reputation: 3105
This isn't Python and I'm not sure how complex you want to get, but it is something I use quite often and it is very simple and bare bones.
Just a little Go program which starts a server and on receiving a request it runs the dowebhook.sh
command. You can swap out whatever command you like and can even add more routes with more handlers to run more commands. You'll have to compile the program for the architecture you're running it on, but the pro is that you now have a binary and there's no need for a framework or dependencies. Keep the program running using either supervisor or systemd. Set up a proxy to :8000 in nginx (or whatever server you like) to define the endpoint. Done.
Now your command executes with just a call to the endpoint, which you can trigger any way you like - if you're talking interface it can be a static page with a button that makes an AJAX call.
package main
import (
"fmt"
"net/http"
"os/exec"
)
func handler(w http.ResponseWriter, r *http.Request) {
out, err := exec.Command("dowebhook.sh").Output()
if err != nil {
fmt.Printf("There was a webhook error: %s", err)
}
fmt.Fprintf(w, "Result of %s is %s", r.URL.Path[1:], out)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8000", nil)
}
Upvotes: 1