Friday, October 28, 2022

[SOLVED] `nohup node build/index.js &` does not execute in the background

Issue

Inside a GitLab CI pipeline, I run the nohup command to start a NodeJS server. I have experimented with several versions of the command. None of the versions ran completely in the background. For some reasons they always stay running in the terminal.

This is the command I used most recently:

ORIGIN=http://$PUBLIC_HOST:$PUBLIC_PORT nohup node build/index.js \
     --port $PUBLIC_PORT | ts >> build/log.txt 2>&1 &

I have tried with and without the pipe at the end.

Which version of this command logs the output of the node process in the background together with timestamps without hanging the terminal?

PS: I am not able to use pm2 or other daemons with online metrics for security considerations.


Solution

After looking at the answer to How can I use nohup to run process as a background process in linux?, I have found a way to let it run by wrapping the core command into bash -c "command".

The command is now

ORIGIN=http://$PUBLIC_HOST:$PUBLIC_PORT \ 
    nohup bash -c "node build/index.js --port $PUBLIC_PORT | ts"  \
    >> build/out.log 2>&1 &

With a variation to split errors and logs based on https://askubuntu.com/questions/349262/run-a-nohup-command-over-ssh-then-disconnect with

ORIGIN=http://$PUBLIC_HOST:$PUBLIC_PORT \
    nohup bash -c "node build/index.js --port $PUBLIC_PORT | ts" \
    1>>build/out.log \
    2>>build/err.log &


Answered By - sryscad
Answer Checked By - Mildred Charles (WPSolving Admin)