0

I run a webserver on a LAMP stack, which includes a secondary Node.js server running on localhost that some requests get passed off to. I normally start/restart this process manually, using these 3 commands:

cd /var/www/html/connect
node secondaryServer.js &> output.log &
disown

I'm trying to migrate this set of commands to a bash script (eventually to be called remotely, via PHP, so I can re-start the Node.js server via an Admin console on the website), so that I just have to call one file instead of typing each line in each time. But I can't seem to get it to work where the Node process survives the end of the script. Most resources I've seen just mention needing to start it in the background, which I'm already doing with the & (to the best of my knowledge). I'm struggling to debug this, as I don't really get any meaningful output aside from knowing that the new process isn't still alive when the script concludes. Any advice on how to get this Node process to survive properly?

I tried merging the 'disown' command with the 'node' command like this:

disown node secondaryServer.js &> output.log &

But that also didn't work.

Edit: I changed the script to:

#!/bin/bash
cd /var/www/html/connect
strace -f -o nodejs.log node secondaryServer.js &> output.log &
PID=$!
echo $PID
disown

I re-ran it using:

sudo ./startSecondaryServer.sh

The file nodejs.log is empty though; does that mean the issue is something different than I expected?

I get a PID out as output, but when I do

ps aux

that PID is not present in the table. And the server isn't running.

5
  • How do you run the script? Have you checked what causes the node.js process to terminate (perhaps using a tool like strace)? Commented Apr 25, 2024 at 15:23
  • 1
    it's quite simple to create a service in systemd which your linux distro is probably using. Let systemd handle keeping your system service running. you're not making it easier for yourself by running it by hand, you're making it harder. Commented Apr 25, 2024 at 15:31
  • I've never used strace or systemd before. I'm not a super experienced Linux dev. Running it by hand is easier in the sense that I actually know how to do it. I'm running the script by typing "sudo ./startNodeServer.sh". Commented Apr 25, 2024 at 15:34
  • Give it a try. Prepend strace -f -o nodejs.log to your node.js command and run the script again. Then open nodejs.log, scroll to the bottom and tell us why it terminates. Commented Apr 25, 2024 at 17:37
  • Edit - formatting in comments is hard. I moved my results / new code to the OP Commented Apr 25, 2024 at 17:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.