pm2 start api should not restart any instances of the api app if it's already running, per the docs: By default, pm2 will only start a script if that script isnβt already running.
The api app is stopped and started.
pm2 start <app name> againYou will see the uptime reset to 0s and there will be stop/start of the app recorded in the pm2 logs.
OS : OSX
node.js : 6.7.0
PM2 : 2.2.2
2016-12-15 09:21:15: Stopping app:api id:4
2016-12-15 09:21:15: Stopping app:api id:6
2016-12-15 09:21:15: App [api] with id [4] and pid [1534], exited with code [0] via signal [SIGINT]
2016-12-15 09:21:15: App [api] with id [6] and pid [1533], exited with code [0] via signal [SIGINT]
2016-12-15 09:21:15: pid=1534 msg=process killed
2016-12-15 09:21:15: Starting execution sequence in -fork mode- for app name:api id:4
2016-12-15 09:21:15: pid=1533 msg=process killed
2016-12-15 09:21:15: Starting execution sequence in -fork mode- for app name:api id:6
2016-12-15 09:21:15: App name:api id:4 online
2016-12-15 09:21:15: App name:api id:6 online
2016-12-15 09:21:15: Stopping app:api id:8
2016-12-15 09:21:15: App [api] with id [8] and pid [1548], exited with code [0] via signal [SIGINT]
2016-12-15 09:21:15: pid=1548 msg=process killed
2016-12-15 09:21:15: Starting execution sequence in -fork mode- for app name:api id:8
2016-12-15 09:21:15: App name:api id:8 online
Miss-understanding or our doc isn't clear but the aim is that PM2 don't start an app that have the same name :
pm2 start api.js: will have api as name
pm2 start api.js: will not be launched as it got the same name
pm2 start api: since you use the name, we understand that you want the already running application to restart
EDIT : I added this to the documentation : (a script is a path to an application, not the name of an application already running)
@vmarchaud thanks for the clarification
I would say that yes, the docs are unclear. There's no mention (that I saw) that script name functions differently than app name.
As part of a deployment, if I wanted to make sure an app was already running without restarting it, I'd have to either hard-code the script path, parse the ecosystem config to get the path, or use the pm2 api to do the same thing.
This doesn't seem like desired behavior, does it?
The file name of the script will be his name : if you start a script using pm2 start path/to/my/script.js, the name will be script.
If you try to re-run the command, pm2 will forbid it since the script is already started.
But if you use pm2 start script, pm2 will assume that you want to restart the application (since here script is the app name and not a path to a script).
If you want to do that, you can use pm2 info <APP_NAME> && pm2 start myapp.js, pm2 info will fail if the application doesn't exist so if the application exist, it doesn't execute the second command.
I think pm2 info <app> && pm2 start app.js would actually only run pm2 start if the app already exists. pm2 info would succeed for an already-existing app.
You could do this hacky one-liner to start the app if it doesn't exist or is stopped:
[[ $((pm2 info app || echo 'status|stopped') | grep status) =~ 'stopped' ]] && pm2 start app
I can not force API function pm2.start to don't restart.
If I use the same options as for previous start, it stops previous processes.
If I use just script name. It does not stop already running processes, but adds new processes to pm2 list.
If I use script and name - it stops previous processes.
So what is the way to don't stop already running processes by pm2.start() function ?
UPDATE: i don't use 'force' option in the config.
but it seems the doc is incorrect:
force (Default: false) By default, pm2 will only start a script if that script isnβt already running (a script is a path to an application, not the name of an application already running).
Actually the default behaviour is far from "only start a script if that script isn't already running".
@Dzenly the force option is only used when you start a specific file path (not just the script name like index.js, the full path of it)
The doc says that it is default behaviour to don't restart already running processes.
But the situation is vice-versa, it is hard as hell to don't stop already running processes.
BTW, how can I avoid such restarting using pm2.start ?
My bad i misunderstood, could you make an issue a write clearly what you tried and what's the wanted behavior ?
I just want to hear some way how to force pm2.start do not restart already running process.
You so clearly described it for command line case.
pm2 start api.js: will have api as name
pm2 start api.js: will not be launched as it got the same name
pm2 start api: since you use the name, we understand that you want the already running application to restart
Now I wish the same description for pm2.start function :)
+1, that's behavior isn't correct and should be fixed probably?
How to keep working with an already started processes with the same script path, but with different names, environments and arguments, when I just spawn pm2 start ${name} command more then one time?
I just ran into this same issue.
Created a bash script test.sh that echoes process id and sleeps for 10s.
Created a test.json config:
instances : 1,
exec_mode : "fork",
interpreter : "bash",
autorestart : false,
merge_logs : true
Called with:
pm2 start test.json
Expected that if 'pm2 start test.json' called while script is running, that original process is not reloaded and 2nd start has no effect. Instead, first process is stopped and second process starts.
Tried:
pm2 start test.sh
All that did was change pm2's version of config with 'autorestart' set to true. Yes, if 'pm2 start test.sh' is called while already running it states it will not reload but didn't expect it to change from autorestart:false to autorestart:true as I really just want the original supplied config test.json to not be changed.
Expect a way to call pm2 that basically says if the process is already running as configured and a request to start again is issued, do not start.
Use case, running pm2 from a crontab and require single process running at a time. If previous process still running cannot run again.
Basically a cheap pid lock without having to write one. :)
today I run some important scripts that cannot overlap in cron with flock. I thought I would be able to use pm2 for that, by creating an ecosystem file, and running pm2 start ecosyste.config.js --only job1. The problem is that that line stops the execution of the previous script to start the new one. Removing the job name and replacing it with the script name followed by a very long sequence of arguments is very nasty. Pm2 already has the pm2 restart jobname, why make start have almost the same behavior? We need a way to lock the execution of scripts by their job name
I'm trying to start only the stopped services but it also restarts already running services which I don't want. How to get around this?
pm2 start all
I was having the same issues with using pm2 start/restart, running over pm2 start twice was showing weird behaviour with the app configured for 4 instances in cluster mode, pm2 left behind processes it didnt kill aswell as trying to restart the lingering process in fork mode eventhough the app is configured in cluster mode. (it tends to leave behind processes as pid 0)
ββββββββββββββββ¬βββββ¬ββββββββββ¬ββββββββββ¬ββββββββ¬ββββββββββββββββββ¬ββββββββββ¬βββββββββ¬ββββββ¬ββββββββββββ¬ββββββββ¬βββββββββββ
β App name β id β version β mode β pid β status β restart β uptime β cpu β mem β user β watching β
ββββββββββββββββΌβββββΌββββββββββΌββββββββββΌββββββββΌββββββββββββββββββΌββββββββββΌβββββββββΌββββββΌββββββββββββΌββββββββΌβββββββββββ€
β realTimeNode β 0 β 1.1.0 β cluster β 0 β online β 5 β 4s β 0% β 0 B β kevin β enabled β
β realTimeNode β 1 β 1.1.0 β cluster β 0 β online β 5 β 4s β 0% β 0 B β kevin β enabled β
β realTimeNode β 2 β 1.1.0 β cluster β 14071 β waiting restart β 7 β 0 β 0% β 0 B β kevin β enabled β
β realTimeNode β 3 β 1.1.0 β cluster β 14019 β online β 5 β 3s β 0% β 84.1 MB β kevin β enabled β
PM2 | App [realTimeNode:2] exited with code [1] via signal [SIGINT]
PM2 | App [realTimeNode:2] will restart in 150ms
PM2 | App [realTimeNode:2] starting in -fork mode-
PM2 | App [realTimeNode:0] online
PM2 | process already started
PM2 | Trace: Error: process already started
PM2 | at Object.God.logAndGenerateError (/usr/lib/node_modules/pm2/lib/God/Methods.js:39:12)
PM2 | at Object.God.startProcessId (/usr/lib/node_modules/pm2/lib/God/ActionMethods.js:293:21)
PM2 | at /usr/lib/node_modules/pm2/lib/God/ActionMethods.js:471:20
PM2 | at /usr/lib/node_modules/pm2/node_modules/async/internal/withoutIndex.js:9:16
PM2 | at replenish (/usr/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:66:17)
PM2 | at iterateeCallback (/usr/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:50:17)
PM2 | at /usr/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
PM2 | at /usr/lib/node_modules/pm2/lib/God/ActionMethods.js:298:14
PM2 | at ready (/usr/lib/node_modules/pm2/lib/God.js:151:15)
PM2 | at Timeout._onTimeout (/usr/lib/node_modules/pm2/lib/God.js:258:16)
PM2 | at Object.God.logAndGenerateError (/usr/lib/node_modules/pm2/lib/God/Methods.js:34:15)
PM2 | at /usr/lib/node_modules/pm2/lib/God/ActionMethods.js:475:30
PM2 | at /usr/lib/node_modules/pm2/node_modules/async/internal/once.js:12:16
PM2 | at iterateeCallback (/usr/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:45:17)
PM2 | at /usr/lib/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
PM2 | at Object.God.startProcessId (/usr/lib/node_modules/pm2/lib/God/ActionMethods.js:293:14)
PM2 | at /usr/lib/node_modules/pm2/lib/God/ActionMethods.js:471:20
PM2 | at /usr/lib/node_modules/pm2/node_modules/async/internal/withoutIndex.js:9:16
PM2 | at replenish (/usr/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:66:17)
PM2 | at iterateeCallback (/usr/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:50:17)
my fix was to basically delete the ecosystem file and then start it again, from package.json:
pm2 del ./ecosystem/development/ecosystem.config.js && npm install && npm run build && pm2 start ./ecosystem/development/ecosystem.config.js --env development --update-env
Most helpful comment
I think
pm2 info <app> && pm2 start app.jswould actually only runpm2 startif the app already exists.pm2 infowould succeed for an already-existing app.You could do this hacky one-liner to start the app if it doesn't exist or is stopped:
[[ $((pm2 info app || echo 'status|stopped') | grep status) =~ 'stopped' ]] && pm2 start app