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?
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 evenpkill -f -x "node server.js"
to kill the process with the exact full command line.pkill
is killing every process launched with a program with a name containing the wordnode
even you scriptRename you script to whatever name not containing the word
node