I'm currently working on automating some processes via Django. For this purpose I have a shell script, that performs certain tasks for me. My webinterface has a "Start Process"-Button and a "Stop Process"-Button.
I came up with the idea to use the process id of the shell script to kill the process when I want to abort it. My start.sh looks like this:
pid=$$;
source ./stop.sh || true
echo $pid > "./processid.txt";
#do the actual stuff
And my stop.sh looks like this:
pid=$(head -n 1 ./processid.txt)
kill $pid
What this does, it takes the process id and temporarily stores it in a variable, then stops the script, if before the script had be running, and then saves the process id to the processid.txt file. My stop.sh just takes this id out of the file and kills the process.
This does work from the commandline... however it doesn't when I execute it from my python code. I call it like this:
def start_bot():
popencommand = "sh -c \"cd ~/thetargetfolder && ./start.sh\""
Popen(popencommand, shell=True)
def stop_bot():
popencommand = "sh -c \"cd ~/thetargetfolder && ./stop.sh\""
Popen(popencommand, shell=True)
For some reason, this doesn't seem to work. I suspect it has something to do with that the process id that I get in the start.sh is not the correct one when calling the python script...