Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

starting additional PM2 start inside a script doesn't create a new pm2 #4761

Closed
BarryThrill opened this issue Jun 28, 2020 · 9 comments
Closed

Comments

@BarryThrill
Copy link

What's going wrong?

Hello guys!

I have figured out a new might be a bug or something that I have done wrong.

What I have found is that if you run a script that is already started with pm2 lets say we call it test.py and then inside the test.py I want to run another script with pm2 etc:

subprocess.Popen(f'pm2 start test.py -n hello_world', shell=True).communicate()

What should happen is that it should create a new pm2 task with a new name called hello_world. where the test.py is already ran by pm2 but the problem is that it never creates the second pm2 yet the test.py does run all the time.

How could we reproduce this issue?

  1. call it test.py and add the code below into the script

import subprocess
import random
import time

print(f"hello {random.randint(1,500)}")

subprocess.Popen(f'pm2 start test.py -name {random.randint(1,500)}', shell=True)
time.sleep(3)

  1. pm2 start test.py

  2. it should create a new script called etc 312 (which is random) but it doesn't. The second pm2 gets never created.

Supporting information

===============================================================================
--- PM2 REPORT (Sun Jun 28 2020 12:32:15 GMT+0200 (GMT+02:00)) ----------------
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 3.2.2
node version         : 10.16.3
node path            : not found
argv                 : C:\Program Files\nodejs\node.exe,C:\Users\Barry-PC\AppData\Roaming\npm\node_modules\pm2\lib\Daemon.js
argv0                : node
user                 : undefined
uid                  : N/A
gid                  : N/A
uptime               : 80min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 3.2.2
node version         : 10.16.3
node path            : not found
argv                 : C:\Program Files\nodejs\node.exe,C:\Users\Barry-PC\AppData\Roaming\npm\node_modules\pm2\bin\pm2,report
argv0                : node
user                 : Barry-PC
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : win32
type                 : Windows_NT
cpus                 : Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
cpus nb              : 8
freemem              : 9496604672
totalmem             : 17119911936
home                 : C:\Users\Barry-PC
===============================================================================
--- PM2 list -----------------------------------------------
┌──────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┬──────────┐
│ App name │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user     │ watching │
├──────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┼──────────┤
│ test     │ 0  │ N/A     │ fork │ 10504 │ online │ 6       │ 2s     │ 0%  │ 10.2 MB   │ Barry-PC │ disabled │
└──────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
C:\Users\Barry-PC\.pm2\pm2.log last 20 lines:
PM2        | 2020-06-28T12:31:57: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2        | 2020-06-28T12:31:57: PM2 log: App [test:0] starting in -fork mode-
PM2        | 2020-06-28T12:31:57: PM2 log: App [test:0] online
PM2        | 2020-06-28T12:32:00: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2        | 2020-06-28T12:32:00: PM2 log: App [test:0] starting in -fork mode-
PM2        | 2020-06-28T12:32:00: PM2 log: App [test:0] online
PM2        | 2020-06-28T12:32:03: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2        | 2020-06-28T12:32:03: PM2 log: App [test:0] starting in -fork mode-
PM2        | 2020-06-28T12:32:03: PM2 log: App [test:0] online
PM2        | 2020-06-28T12:32:03: PM2 error: Error caught while calling pidusage
PM2        | 2020-06-28T12:32:03: PM2 error: Error: No maching pid found
PM2        | 2020-06-28T12:32:06: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2        | 2020-06-28T12:32:06: PM2 log: App [test:0] starting in -fork mode-
PM2        | 2020-06-28T12:32:06: PM2 log: App [test:0] online
PM2        | 2020-06-28T12:32:10: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2        | 2020-06-28T12:32:10: PM2 log: App [test:0] starting in -fork mode-
PM2        | 2020-06-28T12:32:10: PM2 log: App [test:0] online
PM2        | 2020-06-28T12:32:13: PM2 log: App [test:0] exited with code [0] via signal [SIGINT]
PM2        | 2020-06-28T12:32:13: PM2 log: App [test:0] starting in -fork mode-
PM2        | 2020-06-28T12:32:13: PM2 log: App [test:0] online
@BarryThrill
Copy link
Author

BarryThrill commented Jun 28, 2020

If I run py test.py

import random
from subprocess import Popen, PIPE, STDOUT
import time

while True:
    variable = random.randint(1, 500)
    print(f"hello {variable}")

    Popen(f"where python", shell=True, universal_newlines=True)

    time.sleep(3)

>>> hello 49
>>> C:\Users\PC\AppData\Local\Programs\Python\Python38\python.exe
>>> C:\Users\PC\AppData\Local\Microsoft\WindowsApps\python.exe

if I run pm2 start test.py same code:

>>> hello 123
>>> hello 32
>>> hello 41

I think the issue has something to do with that. when I launch a python script which launches another pm2, "watching" is enable...

Unitech added a commit that referenced this issue Jun 29, 2020
@Unitech
Copy link
Owner

Unitech commented Jun 29, 2020

fix on going

@BarryThrill
Copy link
Author

fix on going

Please let me know if you need more information or if I can help you by performing some tests. 😄

Also out of curiosity, is there any ETA for the upcoming fix?

@Unitech
Copy link
Owner

Unitech commented Jun 29, 2020

You can try the fix:

npm install Unitech/pm2#master -g

then
test.py:

import subprocess
import random
import time
import os

print(f"hello {random.randint(1,500)}")

my_env = os.environ.copy()
my_env['PM2_PROGRAMMATIC'] = 'true'

subprocess.Popen(f'pm2 start test.py -name {random.randint(1,500)}', env=my_env, shell=True)
time.sleep(3)

@BarryThrill
Copy link
Author

You can try the fix:

npm install Unitech/pm2#master -g

then
test.py:

import subprocess
import random
import time
import os

print(f"hello {random.randint(1,500)}")

my_env = os.environ.copy()
my_env['PM2_PROGRAMMATIC'] = 'true'

subprocess.Popen(f'pm2 start test.py -name {random.randint(1,500)}', env=my_env, shell=True)
time.sleep(3)

I havent tried Ubuntu/Linux yet but I tested through Windows. Right now nothing happens. I start the script by doing pm2 start test.py - It does print out:

hello 123
hello 421
hello 42

but it does not create additional pm2 when it reaches the subprocess.Popen line.

Not sure if its windows 10 64x related?

@Krylanc3lo
Copy link

Krylanc3lo commented Jun 29, 2020

Hello,

if it can help, I have the same issue. Here is my use case.
I have a first script with prints hello and calls a second script.

#!/usr/bin/python3
# test1.py
from subprocess import Popen
import sys

print("hello")
Popen(f'pm2 start test2.py --name test2 --interpreter /usr/bin/python3 --no-autorestart --time', shell=True, close_fds=True)
sys.exit()
#!/usr/bin/python3
# test2.py
import sys

print('Hello World, it is the second script')
sys.exit()

When I do launch test1.py directly (without pm2: python3 test1.py), test2.py is correctly launched by pm2 and attached to the console.
However, when I launch test1.py through pm2, I have an infinite loop for test1 (while it should only execute it once) and test2 never starts (pm2 start test1.py --name test1 --interpreter /usr/bin/python3 --no-autorestart --time)

so I think that on top of the issue with the 2nd pm2 not being launched, there is also an issue leading to an infinite loop.

I will continue to dig and let you know if I find anything.

@BarryThrill
Copy link
Author

I can confirm it works very fine with Linux/Ubuntu but be aware of the fork bomb with my code. I would say to test @Krylanc3lo test.py & test2.py and add a long sleep between because there is two problems I would say:

  1. It does not work for Windows 10 64x
  2. Like @Krylanc3lo described the problem. It seems like the script continues after the sys.exit() even thought there is a --no-autorestart.

Appreciate the fast help @Unitech ! <3

@Krylanc3lo
Copy link

Thank you @Unitech for the quick fix

@BarryThrill
Copy link
Author

I can confirm the fix works for Ubuntu! @Unitech However it doesn't seem to work for me using Windows 10 64x

@Unitech Unitech closed this as completed Aug 20, 2020
mendrix pushed a commit to mendrix/pm2 that referenced this issue Dec 29, 2020
mendrix pushed a commit to mendrix/pm2 that referenced this issue Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants