1

I have a node app running on my Linux hosting server.

Sometimes the app crashes and I need to restart it, so I log in and type these commands:

pkill node
cd public_html/n49900_dpndev
node server.js &

And the app runs again.

Now I would like to put this into a script so I simply need to start the script (or can execute it from a PHP script, for instance).

I created a file called "startnode":

#!/bin/bash
pkill node
cd public_html/n49900_dpndev
node server.js &

I changed the permissions with chmod 755 startnode.

But when I execute it with ./startnode it responds with:

Terminated

What do I need to do so that the commands I type in manually also work in the bash script?

2
  • 1
    Why don't you just make a systemd unit? – Michael Hampton Mar 6 '19 at 1:26
  • add a -x to your first line: #!/bin/bash -x That will show your verbose/debug node and you can see where the terminated appears. Does node or server.js require an interactive shell? – Scottie H Mar 6 '19 at 2:40
5

Your script name has "node" in the name and pkill node is probably killing it.

Using pkill -x node to kill only processes whose name is exactly "node" might be more to your liking. Or even pkill -f -x "node server.js" to kill the process with the exact full command line.

2

pkill is killing every process launched with a program with a name containing the word node even you script

Rename you script to whatever name not containing the word node

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.