{"action":"created","issue":{"url":"https://api.github.com/repos/Unitech/pm2/issues/2531","repository_url":"https://api.github.com/repos/Unitech/pm2","labels_url":"https://api.github.com/repos/Unitech/pm2/issues/2531/labels{/name}","comments_url":"https://api.github.com/repos/Unitech/pm2/issues/2531/comments","events_url":"https://api.github.com/repos/Unitech/pm2/issues/2531/events","html_url":"https://github.com/Unitech/pm2/issues/2531","id":190673476,"number":2531,"title":"pm2 autorestart:false not working when i use it from process with started pm2 also","user":{"login":"devmetal","id":3758485,"avatar_url":"https://avatars.githubusercontent.com/u/3758485?v=3","gravatar_id":"","url":"https://api.github.com/users/devmetal","html_url":"https://github.com/devmetal","followers_url":"https://api.github.com/users/devmetal/followers","following_url":"https://api.github.com/users/devmetal/following{/other_user}","gists_url":"https://api.github.com/users/devmetal/gists{/gist_id}","starred_url":"https://api.github.com/users/devmetal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/devmetal/subscriptions","organizations_url":"https://api.github.com/users/devmetal/orgs","repos_url":"https://api.github.com/users/devmetal/repos","events_url":"https://api.github.com/users/devmetal/events{/privacy}","received_events_url":"https://api.github.com/users/devmetal/received_events","type":"User","site_admin":false},"labels":[{"id":125446670,"url":"https://api.github.com/repos/Unitech/pm2/labels/inspecting","name":"inspecting","color":"fbca04","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-11-21T10:20:27Z","updated_at":"2016-11-21T11:39:55Z","closed_at":"2016-11-21T10:24:36Z","body":"Hi, we try to use pm2 in production to start processes dinamically. We encountered an interesting issue. We try to search what cause this behaviour but we did not find anything. I wish this is just some missunderstanding because i love using pm2.\r\n\r\nHere is my poc code:\r\n\r\n```JavaScript\r\nconst pm2 = require('pm2');\r\nconst { join } = require('path');\r\n\r\nconst pm2Connect = () => new Promise((resolve, reject) => {\r\n  pm2.connect((err) => {\r\n    if (err) return reject(err);\r\n    resolve();\r\n  });\r\n});\r\n\r\nconst pm2Disconnect = () => pm2.disconnect();\r\n\r\nconst pm2List = () => new Promise((resolve, reject) => {\r\n  pm2.list((err, lst) => {\r\n    if (err) return reject(err);\r\n    resolve(lst);\r\n  })\r\n});\r\n\r\nconst pm2Start = (options) => new Promise((resolve, reject) => {\r\n  const errback = (err, proc) => {\r\n    if (err) return reject(err);\r\n    resolve(proc[0]);\r\n  }\r\n\r\n  const args = [options, errback];\r\n  pm2.start(...args);\r\n});\r\n\r\nconst pm2Delete = p => new Promise((resolve, reject) => {\r\n  const pid = p.pm_id;\r\n  pm2.delete(pid, (err) => {\r\n    if (err) return reject(err);\r\n    resolve(p);\r\n  })\r\n});\r\n\r\nconst script = join(__dirname, 'child.js');\r\n\r\nconst startAChild = (name) => () => pm2Start({\r\n  name,\r\n  script,\r\n  autorestart: false\r\n});\r\n\r\nconst delay = (tm) => () => new Promise((resolve) => {\r\n  setTimeout(() => resolve(), tm);\r\n});\r\n\r\nconst delay3000 = delay(3000);\r\n\r\nconst watch = () => {\r\n  pm2List().then((items) => {\r\n    console.log('------------------');\r\n    items.forEach((item) => {\r\n      console.log(item.name, item.pid, item.pm_id, item.pm2_env.status);\r\n    });\r\n\r\n    const stopped = items.filter(item => item.pm2_env.status === 'stopped');\r\n    if (stopped.length) {\r\n      console.log('delete processes');\r\n      return Promise.all(stopped.map(pm2Delete))\r\n        .then((deleted) => {\r\n          deleted.forEach((item) => {\r\n            console.log(item.name, item.pid);\r\n          })\r\n        })\r\n    }\r\n  })\r\n  .then(delay3000)\r\n  .then(() => {\r\n    watch();\r\n  })\r\n}\r\n\r\npm2Connect()\r\n  .then(startAChild('proc1'))\r\n  .then(startAChild('proc2'))\r\n  .then(startAChild('proc3'))\r\n  .then(startAChild('proc4'))\r\n  .then(watch);\r\n``` \r\n\r\nThe child process is just a simple do -> process.exit(0) style script.\r\n\r\nThe most important part is the startAChild function. \r\n\r\n```JavaScript\r\nconst startAChild = (name) => () => pm2Start({\r\n  name,\r\n  script,\r\n  autorestart: false\r\n});\r\n```\r\n\r\nI try set autorestart to false because the production application need to start the processes just one. I know we can use nodejs fork or spawn methods, but we love the log system and monitoring in pm2. I we start this process, with nodejs like this:\r\n\r\n```bash\r\nnode spawner.js\r\n```\r\n\r\nThe whole thing is working well. The processes start one, and no autorestart.\r\n\r\nBut! If we start the spawner with pm2:\r\n\r\n```bash\r\npm2 start spawner.js\r\n```\r\n\r\nThe processes will be restart and restart and restart in infinite loop. This is a normal behaviour? Or the pm2 is not designed for this? Pls help me, why the autorestart not working?\r\n#### Software versions used\r\n\r\n```\r\nOS         :  Ubuntu 16 LTS\r\nnode.js    :  6.9.1 LTS\r\nPM2        : 2.1.5\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/Unitech/pm2/issues/comments/261914796","html_url":"https://github.com/Unitech/pm2/issues/2531#issuecomment-261914796","issue_url":"https://api.github.com/repos/Unitech/pm2/issues/2531","id":261914796,"user":{"login":"devmetal","id":3758485,"avatar_url":"https://avatars.githubusercontent.com/u/3758485?v=3","gravatar_id":"","url":"https://api.github.com/users/devmetal","html_url":"https://github.com/devmetal","followers_url":"https://api.github.com/users/devmetal/followers","following_url":"https://api.github.com/users/devmetal/following{/other_user}","gists_url":"https://api.github.com/users/devmetal/gists{/gist_id}","starred_url":"https://api.github.com/users/devmetal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/devmetal/subscriptions","organizations_url":"https://api.github.com/users/devmetal/orgs","repos_url":"https://api.github.com/users/devmetal/repos","events_url":"https://api.github.com/users/devmetal/events{/privacy}","received_events_url":"https://api.github.com/users/devmetal/received_events","type":"User","site_admin":false},"created_at":"2016-11-21T11:39:55Z","updated_at":"2016-11-21T11:39:55Z","body":"Not working for me:\r\n\r\nmaster.js:\r\n```JavaScript\r\nvar pm2 = require('pm2');\r\n\r\npm2.connect(false, function () {\r\n  pm2.start('./test.js', {autorestart: false}, function (test) {\r\n    console.log(\"started\")\r\n  })\r\n})\r\n```\r\n\r\ntest.js\r\n```JavaScript\r\nconsole.log('oyoyo');\r\n```\r\n\r\n```bash\r\npm2 start master.js\r\n```\r\n\r\npm2 logs:\r\n```bash\r\nPM2        | Starting execution sequence in -fork mode- for app name:master id:0\r\nPM2        | App name:master id:0 online\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n0|master   | started\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10247], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10257], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10267], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10277], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10287], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10297], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10307], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10317], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10327], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10337], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10347], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10357], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10367], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10377], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10387], exited with code [0] via signal [SIGINT]\r\nPM2        | Starting execution sequence in -fork mode- for app name:test id:1\r\nPM2        | App name:test id:1 online\r\n1|test     | oyoyo\r\nPM2        | App [test] with id [1] and pid [10397], exited with code [0] via signal [SIGINT]\r\nPM2        | Script /home/adam/pm2poc/test.js had too many unstable restarts (16). Stopped. \"errored\"\r\n```"}}
{"actor":{"display_login":"devmetal"}}