I have this task and I can’t for the life of me get it to keep the process running. I have added in some debug and it shows me the process is getting shutdown almost immediately after it is started up as can be seen by the end time in the output I have provided below. Please advise how I can fix this so that the process keeps on running as future tasks are performed.
hosts: nodes
remote_user: root
become_user: test
become: true
tasks:
I have this task and I can't for the life of me get it to keep the process running. I have added in some debug and it
shows me the process is getting shutdown almost immediately after it is started up as can be seen by the end time in the
output I have provided below. Please advise how I can fix this so that the process keeps on running as future tasks are
performed.
- hosts: nodes
remote_user: root
become_user: test
become: true
tasks:
\- name: execute startup script
shell: nohup npm start > test\.out 2>&1 &
register: status
args:
chdir: "/apps/test/postcodes\.io"
\- debug:
var: status
“stdout”: “[PM2] Applying action restartProcessId on app [app](ids: [ 0 ])\n[PM2] app ✓\n[PM2] Process successfully started\n┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐\n│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │\n├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤\n│ 0 │ app │ default │ 12.0.0 │ fork │ 59888 │ 0s │ 1 │ online │ 0% │ 8.7mb │ wasadm │ disabled │\n└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘”,
“stdout_lines”: [
“[PM2] Applying action restartProcessId on app [app](ids: [ 0 ])”,
“[PM2] app ✓”,
“[PM2] Process successfully started”,
“┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐”,
“│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │”,
“├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤”,
“│ 0 │ app │ default │ 12.0.0 │ fork │ 59888 │ 0s │ 1 │ online │ 0% │ 8.7mb │ wasadm │ disabled │”,
“└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘”
]
}
}
However, using pm2 has introduced other problems such as me failing to have it read the process.env file that node had no issues with so now I am not listening on the intended port with the custom credentials. No doubt there is a way to do this but before looking at adopting pm2 is there not just a simple fix to my existing ansible task in order to launch “node start” and keep that process up and running?
Use command instead of shell module. See documentation.
and provides a handy output to the terminal
"stdout": "[PM2] Applying action restartProcessId on app [app](ids: [ 0 ])\n[PM2] [app](0) ✓\n[PM2] Process successfully
started\n┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐\n│
id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │
watching
│\n├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤\n│
0 │ app │ default │ 12.0.0 │ fork │ 59888 │ 0s │ 1 │ online │ 0% │ 8.7mb │ wasadm │
disabled
│\n└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘",
"stdout_lines": [
"[PM2] Applying action restartProcessId on app [app](ids: [ 0 ])",
"[PM2] [app](0) ✓",
"[PM2] Process successfully started",
"┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐",
"│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem
│ user │ watching │",
However, using pm2 has introduced other problems such as me failing to have it read the process.env file that node had
no issues with so now I am not listening on the intended port with the custom credentials. No doubt there is a way to do
this but before looking at adopting pm2 is there not just a simple fix to my existing ansible task in order to launch
"node start" and keep that process up and running?
I'm sure that pm2 can regard your process.env file, but I think that is beyond the topic of this group.
And no, I neither have or want "just" a fix for your inferior hack, sorry.
I tried the command module before using the shell module.
I thought the command module was the wrong choice because when I run it, as below, it simply hangs.
If I log on to the host I can see the ansible task has spawned the node.js process but I suppose my playbook is hanging because it can’t background it.
Any ideas how to do this because the documentation has left me none the wiser.
I tried the command module before using the shell module.
I thought the command module was the wrong choice because when I run it, as below, it simply hangs.
If I log on to the host I can see the ansible task has spawned the node.js process but I suppose my playbook is hanging
because it can't background it.
Ok, I understand now. I thought using pm2 involved a lot of tweaking. I was trying and failing to get it to read my npm dotenv. Instead of trying to use pm2 to call my app.js directly I have just realised I can use it to launch it indirectly via npm without having to change any config . Simples!