2

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? Commented Mar 6, 2019 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? Commented Mar 6, 2019 at 2:40

2 Answers 2

6

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.