0

I used php to run python script on Linux via web after running and I get process ID

<!DOCTYPE html>
<html>
   <body>
      <?php
      echo 'Starting the mapping process';
      echo exec('nohup python cloudemir.py '.$_GET[message].' > /dev/null 2>&1 & echo $! &');
     ?>
  </body>
</html> 

which is then used to kill the process when I don't need it which works fine

<!DOCTYPE html>
<html>
    <body>
        <?php
        $pid=$_GET['pid'];
        echo exec("kill -9 $pid");
        ?>
    </body>
</html>

The python script I used is just publishing one message via MQTT every second. The problem is that I can't change the message content.

If the message is Hi, and I run this process, it says "Hi", "Hi"... every second. If I change python script message to Hi2, and run it again I get new process id and new message is Hi2 every second and there is no Hi message any more.

I need it to run separately and to get both messages, and when needed to kill one of them. I need it to be able to run unlimited number of instances of that python sctipt with different configuiration, I don't need to build new scripts.

domain/run.php?message=Hi

domain/run.php?message=Hi2

This should print Hi, Hi2, Hi, Hi2

domain/run.php?message=Hi

domain/run.php?message=Hi2

domain/run.php?message=Hi3

This should print Hi, Hi2, Hi3, Hi, Hi2, Hi3

In both cases it prints only last message

I even tried the same without php, but calling the script from another python script with

proc = subprocess.Popen("nohup python cloudemir.py "+content+" > /dev/null 2>&1 &", shell=True)

But it remains the same, last call overwrites the previous

4
  • Are you saying that you want your python script to remember all the messages it has received until it is killed? Commented Feb 20, 2020 at 15:01
  • I guess what I am asking is how do you know, how many messages a python instance will handle? domain/run.php?message=Hi domain/run.php?message=Hi2 maybe you need a domain/run.php?clear_state=True or something like that? Commented Feb 20, 2020 at 15:07
  • I suppose that because I am running the script in the backgound, each time I run it it is new instance and runs separately. Is that possible? Commented Feb 20, 2020 at 15:17
  • yes exec will return a new instance, you could store the contents in a file somewhere and read that file... but that would give all the messages unless you add in some options to the python script to clear that file. Commented Feb 20, 2020 at 16:10

1 Answer 1

1

You should use sys.argv. As an example of the file cloudemir.py:

import sys

print(sys.argv[0])

And then you can call it with:

python cloudemir.py Hi # First time
python cloudemir.py Hi2 # Second time

This way the name after cloudemir.py will be inputed as a param to the python program.

Sign up to request clarification or add additional context in comments.

1 Comment

It is good solution for improvement, but still only one message is printed. And the argv[0] printed the name of the program, and agr[1] printed the message. I need both messages runing, not one to replace another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.