# Launch node server in background

**URL:** <https://forum.ansible.com/t/launch-node-server-in-background/35855>\
**Category:** Ansible Project\
**Created:** [October 12, 2021, 3:28pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855 "2021-10-12T15:28:56Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 3:28pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/1 "2021-10-12T15:28:56Z")

</div>

Hmm. I must be making again something fundamentally wrong 🙂

Trying to start a simple node server app into background

- name: Start server (run as non-privileged user)  
tags: server  
shell: |  
nohup node server.js 2\>&1 &  
args:  
chdir: my-server-dir  
become: yes  
become\_user: “{{ non\_root\_user }}”

This runs w/o problems. However, the service is not started…

Running the same command from console works…

It doesn’t even run as “sudo” (if f I remove the “become” lines at the end, my entire script runs as sudo)

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 4:07pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/2 "2021-10-12T16:07:54Z")

</div>

Worked around with `forever`, but would like to understand…

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 12, 2021, 4:42pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/3 "2021-10-12T16:42:21Z")

</div>

> Hmm. I must be making again something fundamentally wrong 🙂

Yes, you are trying to use Ansible as glorified shell \*and\* ignore all the options to run a service properly (e.g. with systemd).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 4:43pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/4 "2021-10-12T16:43:11Z")

</div>

Thanks for the helpful comment.

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 4:45pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/5 "2021-10-12T16:45:07Z")

</div>

…even though it might not be the high level of “best practice” (in fact you can read a lot of this Racke comments, especially with Ansible) - and I’m surely aware of that - it is a legit shell command.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 12, 2021, 4:54pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/6 "2021-10-12T16:54:17Z")

</div>

> ...even though it might not be the high level of "best practice" (in fact you can read a lot of this Racke comments, especially with Ansible) - and I'm surely aware of that - it is a legit shell command.

Yes, but running a server through nohup is certainly bad practice. Nohup is rather provided to prevent long running processes to be disrupted.

There is no really good way to control the server when it is running. How do you stop it for example? Restart it when it is crashed? Etc.

And the shell called from Ansible doesn't have the same environment as when you login as regular user.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 5:04pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/7 "2021-10-12T17:04:59Z")

</div>

Posts disappear here…

However, disregard. IMHO there is nothing wrong with “node server.js &” and I was just trying to figure out why this doesn’t work with Ansible. I wasn’t looking for a religious discussion.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 12, 2021, 5:08pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/8 "2021-10-12T17:08:34Z")

</div>

> Posts disappear here...
> 
> However, disregard. IMHO there is nothing wrong with "node server.js &" and I was just trying to figure out why this doesn't work with Ansible. I wasn't looking for a religious discussion.

Neither me .. just pointing out common sense using Ansible and Unix services.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 6:00pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/9 "2021-10-12T18:00:34Z")

</div>

Look, that sucks meanwhile. The question was: “why is this not working”? Not: Who here is able to teach me best practice?

Because - this is something I can get around the corner.

I give you the last word. Will not respond.

Thanks

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 12, 2021, 6:09pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/10 "2021-10-12T18:09:42Z")

</div>

> Look, that sucks meanwhile. The question was: "why is this not working"? Not: Who here is able to teach me best practice?
> 
> Because - this is something I can get around the corner.
> 
> I give you the last word. Will not respond.
> 
> Thanks

Fair enough, but the trouble is other users might reuse your questionable examples.

&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 6:20pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/11 "2021-10-12T18:20:52Z")

</div>

Yes, now everyone understands that you’ve got it.

Anyone else with an answer to the actual question? Why is that not possible?

I don’t mean: Why can’t you do that?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 12, 2021, 6:24pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/12 "2021-10-12T18:24:40Z")

</div>

> Yes, now everyone understands that you've got it.
> 
> Anyone else with an answer to the actual question? Why is that not possible?

Did you try to remove the redirection from your shell command and look at stdout/stderr (with register + debug task)?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 6:32pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/13 "2021-10-12T18:32:57Z")

</div>

OK, then back to the problem:

With and without nohup the same result: The app seems to start, but is killed then. Didn’t try debug yet, but the console output looks good:

TASK [Start server (run as non-privileged user)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [ubuntuVM] =\> {“changed”: true, “cmd”: “nohup node wss.js &\n”, “delta”: “0:00:01.190398”, “end”: “2021-10-12 18:30:38.791334”, “msg”: “”, “rc”: 0, “start”: “2021-10-12 18:30:37.600936”, “stderr”: “”, “stderr\_lines”: , “stdout”: “WSS mock running on port 8080”, “stdout\_lines”: [“WSS mock running on port 8080”]}

TASK [Start server (run as non-privileged user)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [ubuntuVM] =\> {“changed”: true, “cmd”: “node wss.js &\n”, “delta”: “0:00:01.182786”, “end”: “2021-10-12 18:31:23.449623”, “msg”: “”, “rc”: 0, “start”: “2021-10-12 18:31:22.266837”, “stderr”: “”, “stderr\_lines”: , “stdout”: “WSS mock running on port 8080”, “stdout\_lines”: [“WSS mock running on port 8080”]}

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 12, 2021, 6:43pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/14 "2021-10-12T18:43:02Z")

</div>

> OK, then back to the problem:
> 
> With and without nohup the same result: The app seems to start, but is killed then. Didn't try debug yet, but the console output looks good:
> 
> TASK [Start server (run as non-privileged user)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> changed: [ubuntuVM] =\> {"changed": true, "cmd": "nohup node wss.js &\n", "delta": "0:00:01.190398", "end": "2021-10-12 18:30:38.791334", "msg": "", "rc": 0, "start": "2021-10-12 18:30:37.600936", "stderr": "", "stderr\_lines": , "stdout": "WSS mock running on port 8080", "stdout\_lines": ["WSS mock running on port 8080"]}
> 
> TASK [Start server (run as non-privileged user)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> changed: [ubuntuVM] =\> {"changed": true, "cmd": "node wss.js &\n", "delta": "0:00:01.182786", "end": "2021-10-12 18:31:23.449623", "msg": "", "rc": 0, "start": "2021-10-12 18:31:22.266837", "stderr": "", "stderr\_lines": , "stdout": "WSS mock running on port 8080", "stdout\_lines": ["WSS mock running on port 8080"]}

My advice would be to write a script that does the nohup with all the redirections instead using it verbatim. See [https://stackoverflow.com/questions/41676607/ansible-terminates-my-app-with-nohup](https://stackoverflow.com/questions/41676607/ansible-terminates-my-app-with-nohup) for more information. If you do that, switch to command module.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 6:50pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/15 "2021-10-12T18:50:47Z")

</div>

I know this reference. This was my first attempt before I stripped it down to what is there. Didn’t work either.

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 6:55pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/16 "2021-10-12T18:55:48Z")

</div>

Tried again. Same result. No obvious error, but server not started.

./start-wss.sh

#!/bin/sh

# Launch as service

nohup node wss.js \</dev/null \>/dev/null 2\>&1 &

- name: Start server (run as non-privileged user)  
tags: server  
command: ./start-wss.sh  
args:  
chdir: server

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 6:56pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/17 "2021-10-12T18:56:58Z")

</div>

Script works on the node, btw.

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 6:59pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/18 "2021-10-12T18:59:44Z")

</div>

For node I could live with forever, which works fine. But I also have some python scripts to start like so. Maybe to wrap them as service seems the only option then. I’m not aware of a “forever” for python…

Let’s bury that.

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [October 12, 2021, 7:26pm UTC](https://forum.ansible.com/t/launch-node-server-in-background/35855/19 "2021-10-12T19:26:38Z")

</div>

Looks like forever is supposed to work with python as well. Let’s see

forever start -c python python\_script.py

[https://stackoverflow.com/questions/19571282/using-forever-js-with-python](https://stackoverflow.com/questions/19571282/using-forever-js-with-python)
