I was looking to test a node server in a shared hosting environment.
I'm using an ssh terminal session to test.
The server works fine, but of course you can't leave the terminal session without stopping the server.
Using pm2 (npm package), I'm able to keep the server running, but on exiting the terminal session, the pm2 job quits as well, which stops the server. Curious why that would be.
Tried using a crontab to run a shell script that in turn runs the pm2 which in turn starts the node server. The cron tab runs every minute, but the node server never starts.
The sh script works just fine, pm2 works fine, and the node server works fine.
What doesn't work: keeping the node server running after exiting the terminal session.
Here's the shell script that runs the pm2 to trigger the node server...
ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
echo "Process running."
else
echo "Process not running."
PATH=$PATH:/usr/local/bin
pm2 start '/path/to/NodeServer.js' --restart-delay=100
fi
If someone else like me uses pm2 for godaddy shared hosting, and uses a modified version of Habib's answer with pm2 instead of node, do not forget to include path to node into PATH variable, as it is required by pm2:
PATH=$PATH:/home/user/.nvm/versions/node/v11.15.0/bin
pm2 ps | grep 'my-app'
if [ $? -eq 0 ]; then
echo "Process running."
else
echo "Process not running."
cd /home/user/public_html/app
pm2 start app.js --name my-app
fi
The script above works for me. Specifying only full path to pm2 was not working, as it looks for node, which was not in PATH in the cron environment.
ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
echo "Process running."
else
echo "Process not running."
/home/user/.nvm/versions/node/v11.15.0/bin/node /home/user/public_html/node/app.js
fi
I used this in cron job in GoDaddy Shared hosting and it is working for me. Before this, I used node /home/user/public_html/node/app.js in the same script but it throws an error because of path of the node. Still, I can use node in the terminal but in cron job, I have to use full path.
Related
I have created API using node js and my API is live now (in production) but now I always want to open Cpanel and terminal and run command to start node js API. but now I want like I just run one-time command in terminal and it will run automatically even in my computer is shutdown. or there is another way to do it. and I google it I found some code but it's not work
first, I try this
#!/bin/bash
ps cax | grep node > /dev/null
if [ $? -eq 0 ]; then
echo "Process is running." >/dev/null 2>&1
else
echo "Process is not running."
PATH=$PATH:/usr/local/bin
pm2 start /path/to/your/node/application
fi
then i create bat file. and just simple add node app.js then i create php file and
use this code shell_exec('npm start'); and shell_exec('sh script.sh'); but nothing work.
If you're using pm2 to manage the node process, you can configure pm2 to auto restart the node process after you're computer restarts. refer below link to configure pm2
https://pm2.keymetrics.io/docs/usage/startup/
Hello I Find This answer you just have install pm2 and run pm2 startup then run pm2 save to save your query.
for more information https://pm2.keymetrics.io/docs/usage/startup/
I have a Node.JS server running on PM2 that's crashing every once in a while because of a database limit, which I'm working on.
In the meantime, I thought I'd try just setting up a cron job in cpanel to restart the server every hour if it's down.
So I wrote a bash script like the following:
#!/bin/bash
status_code=$(curl --write-out %{http_code} --silent --output /dev/null https://website.com/)
date >> cronlog.txt
if [[ "$status_code" -ne 200 ]] ; then
pkill node
nohup pm2 start bin/www &
echo "Site status $status_code" >> cronlog.txt
echo "Restarting Server" >> cronlog.txt
exit
else
echo "Site fine" >> cronlog.txt
exit 0
fi
Running this from an SSH terminal works perfectly; if the site is down, it'll restart it.
However, once I set up the cron job in cpanel, like so: 0 * * * * /home/acc123/fix.sh, looking at the output of cronlog.txt, I see that the script is definitely running every hour, trying to restart the server - it's just that the server doesn't restart.
A preliminary Google suggested that maybe pm2 wasn't on the path that the cron job runs from, so I modified the script to look like this:
#!/bin/bash
status_code=$(curl --write-out %{http_code} --silent --output /dev/null https://website.com/)
date >> cronlog.txt
if [[ "$status_code" -ne 200 ]] ; then
pkill node
nohup /home/acc123/bin/pm2 start /home/acc123/bin/www &
echo "Site status $status_code" >> cronlog.txt
echo "Restarting Server" >> cronlog.txt
exit
else
echo "Site fine" >> cronlog.txt
exit 0
fi
But nothing changes. Looking at the text file I write to, the script is definitely running every hour, and it's definitely picking up that the site is down, but while the words "Restarting Server" get written to the text file, the server doesn't actually start.
Checking nohup.out confirms that nothing has been written to it, suggesting that somehow the command nohup /home/acc123/bin/pm2 start /home/acc123/bin/www & isn't running correctly.
I'm stumped. Has anyone seen something similar before?
Found it. Looks like node itself also wasn't on the path variable for the cron job. Explicitly specifying where node was fixed the problem.
I need to check in ubuntu terminal if process node is running so that i can use two different commands then if is running or not.
i try copy paste this code in ubuntu terminal:
if pgrep -x "node" > /dev/null then echo "Running" else echo "Stopped" fi
in terminal i get only > and no output...and i was expacting to output: Running or Stopped
But it stucks on >
How can i check in terminal without using external sh file to get information if node process is running or not? I im executing this in node ssh command on another server so i try copy paste this code on my server local but is not go.
You've got three missing semicolons:
if pgrep -x "node" > /dev/null; then echo "Running"; else echo "Stopped"; fi
I have a node server running within a vagrant. The script that starts node 'start.sh' can pick up a debug flag from a file, which I named debug.mode
On the local side I have a script 'startdebug.sh' which logs into vagrant over ssh, writes to the debug.mode file, restarts the script, waits till that is done, then tunnels 5858.
If I run the start.sh file with debug.mode containing '--debug' node opens 5858 and the port is available (I'm checking within vagrant using telnet).
If I do the same using startdebug.sh node says it's opened the debugging port, however the 5858 port it unavailable when I try telnet'ing within the VM.
Any idea? :)
startdebug.sh
/usr/bin/vagrant ssh-config > /tmp/vagrant-ssh-config
ssh -F /tmp/vagrant-ssh-config nodejs "cd /var/www/sportsbook-api && echo $mode > debug.mode && export TERM=linux && sudo ./scripts/restart.sh"
sleep 2.5s
ssh -N -F /tmp/vagrant-ssh-config -L 5858:127.0.0.1:5858 nodejs &
start.sh
if [ -e "debug.mode" ]; then
debug=$(cat "debug.mode")
echo "\nNode $debug mode activated."
fi
nohup node src/main/apps/api & echo $! > run.pid &
I have written an API using node.js. I have deployed it in the production environment. Now i ran the program(server.js) using forever.js, its working fine
But i want my node to run when the system boots itself.
I tried by creating a file in \etc\init api.conf with the following content.
start on startup
exec forever start /home/testuser/server.js
But when i reboot my system the above isn't running.Please help me solve this.
Thanks in advance.
EDIT:
Finally i tried this:
# Source function library.
. /lib/lsb/init-functions
NODE_ENV="production"
PORT="2100"
APP_DIR="/home/testuser/API/"
NODE_APP="server.js"
CONFIG_DIR="$APP_DIR"
PID_DIR="$APP_DIR/pid"
PID_FILE="$PID_DIR/server.pid"
LOG_DIR="/home/testuser/APIlogs/"
LOG_FILE="$LOG_DIR/project-debug.log"
NODE_EXEC=$(which node)
pidFile="$PID_DIR/server.pid"
logFile="$LOG_DIR/project-debug.log"
sourceDir=/home/testuser/API
coffeeFile=server.js
scriptId=$sourceDir/$coffeeFile
start() {
echo "Starting $scriptId"
# This is found in the library referenced at the top of the script
start_daemon
# Start our CoffeeScript app through forever
# Notice that we change the PATH because on reboot
# the PATH does not include the path to node.
# Launching forever or coffee with a full path
# does not work unless we set the PATH.
cd $sourceDir
PATH=/usr/local/bin:$PATH
forever start /home/testuser/API/server.js
RETVAL=$?
}
restart() {
echo -n "Restarting $scriptId"
/usr/local/bin/forever restart $scriptId
RETVAL=$?
}
stop() {
echo -n "Shutting down $scriptId"
/usr/local/bin/forever stop $scriptId
RETVAL=$?
}
status() {
echo -n "Status $scriptId"
/usr/local/bin/forever list
RETVAL=$?
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
When i tried the following command:
/etc/init.d/API start it works fine. When i reboot the system it says "NO forever process" running.
You should have a look at Upstart. People use it with Node. Forever is good just for testing.
I suggest switching to pm2.
$ npm install pm2 -g
$ pm2 start /home/testuser/server.js
$ pm2 list
PM2 can generate and configure a startup script to keep PM2 and your processes alive at every server restart.
$ pm2 startup ubuntu
To save a process list just do:
$ pm2 save
You check here for more information about setting-up pm2 on a Ubuntu server for production.