How detect and restart a Node.js script running via SSH? - node.js

I'm running a .js script with Node via SSH on my web host (Bluehost). I have a shared hosting, so I just downloaded/unzipped node and I run a script in SSH terminal like so:
> ./node/bin/node ./script.js
the script is continuously printing some output in an endless loop but after some time (about an hour) it gets killed by the server.
How do I detect it and restart the script?
I tried to create a cron job that runs restart.sh every minute in hopes to run my shell command if the process is not detected:
#!/bin/bash
if pgrep node >/dev/null
then
echo "Process is running." > /home2/xxxx/txt.txt
else
ps aux > /home2/xxxx/txt.txt
fi
but don't see any processes in the txt.txt:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
xxx 2 0.0 0.0 113292 2696 ? SN 05:27 0:00 /bin/bash /home2/xxx/restart.sh
xxx 4 0.0 0.0 155460 3988 ? RN 05:27 0:00 ps aux

You can use pm2 for monitoring your node process
npm install -g pm2
Start a node process :
pm2 start -n "My process" node myscript.js
PM2 site : https://pm2.keymetrics.io/
PM2 will restart your script if needed.
To see node process :
pm2 list
But if your script end after 1 hour, perharps is there a problem with your code.
You can find out and error logs in : ~user/.pm2/logs

Related

pg_dump not been killed inside script with kill command

Question 1:
Jenkins job has a script running, inside the script there is pg_dump command.
I'd like to kill this job and all children processes but
kill -9 PID
kill -TERM PID
is only killing Jenkins job and not killing pg_dump
Question 2:
How can I identify the Jenkins job through command line? Right now:
ps -ef | grep jenkins will return
jenkins 28706 2608 0 17:39 ? 00:00:00 /bin/sh -xe /var/lib/jenkins/tmp/jenkins5167617865881040149.sh
I just know that this is the job because it's the only one running, but obviously the name in Jenkins itself is different.

Job -l after nohup

How can I monitor a job that is still running (I guess detached?) after I started it with nohup, exited the server and logged back in? Normally, I use jobs -l to see what's running, but this is showing blank.
You need to understand the difference between a process and a job. Jobs are managed by the shell, so when you end your terminal session and start a new one, you are now in a new instance of Bash with its own jobs table. You can't access jobs from the original shell but as the other answers have noted, you can still find and manipulate the processes that were started. For example:
$ nohup sleep 60 &
[1] 27767
# Our job is in the jobs table
$ jobs
[1]+ Running nohup sleep 60 &
# And this is the process we started
$ ps -p 27767
PID TTY TIME CMD
27767 pts/1 00:00:00 sleep
$ exit # and start a new session
# Now jobs returns nothing because the jobs table is empty
$ jobs
# But our process is still alive and kicking...
$ ps -p 27767
PID TTY TIME CMD
27767 pts/1 00:00:00 sleep
# Until we decide to kill it
$ kill 27767
# Now the process is gone
$ ps -p 27767
PID TTY TIME CMD
You can monitor if the proceses if still running using
ps -p <pid>, where is the ID of the process you get after using the nohup command.
If you see valid entries you process is probably alive.
You could have a list of the processes running under current user with ps -u "$USER" or ps -u "$(whoami)".
Try this :
ps -ef | grep <pid>

How to get a list of programs running with nohup

I am accessing a server running CentOS (linux distribution) with an SSH connection.
Since I can't always stay logged in, I use "nohup [command] &" to run my programs.
I couldn't find how to get a list of all the programs I started using nohup.
"jobs" only works out before I log out. After that, if I log back again, the jobs command shows me nothing, but I can see in my log files that my programs are still running.
Is there a way to get a list of all the programs that I started using "nohup" ?
When I started with $ nohup storm dev-zookeper ,
METHOD1 : using jobs,
prayagupd#prayagupd:/home/vmfest# jobs -l
[1]+ 11129 Running nohup ~/bin/storm/bin/storm dev-zookeeper &
NOTE: jobs shows nohup processes only on the same terminal session where nohup was started. If you close the terminal session or try on new session it won't show the nohup processes. Prefer METHOD2
METHOD2 : using ps command.
$ ps xw
PID TTY STAT TIME COMMAND
1031 tty1 Ss+ 0:00 /sbin/getty -8 38400 tty1
10582 ? S 0:01 [kworker/0:0]
10826 ? Sl 0:18 java -server -Dstorm.options= -Dstorm.home=/root/bin/storm -Djava.library.path=/usr/local/lib:/opt/local/lib:/usr/lib -Dsto
10853 ? Ss 0:00 sshd: vmfest [priv]
TTY column with ? => nohup running programs.
Description
TTY column = the terminal associated with the process
STAT column = state of a process
S = interruptible sleep (waiting for an event to complete)
l = is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
Reference
$ man ps # then search /PROCESS STATE CODES
Instead of nohup, you should use screen. It achieves the same result - your commands are running "detached". However, you can resume screen sessions and get back into their "hidden" terminal and see recent progress inside that terminal.
screen has a lot of options. Most often I use these:
To start first screen session or to take over of most recent detached one:
screen -Rd
To detach from current session: Ctrl+ACtrl+D
You can also start multiple screens - read the docs.
If you have standart output redirect to "nohup.out" just see who use this file
lsof | grep nohup.out
You cannot exactly get a list of commands started with nohup but you can see them along with your other processes by using the command ps x. Commands started with nohup will have a question mark in the TTY column.
You can also just use the top command and your user ID will indicate the jobs running and the their times.
$ top
(this will show all running jobs)
$ top -U [user ID]
(This will show jobs that are specific for the user ID)
sudo lsof | grep nohup.out | awk '{print $2}' | sort -u | while read i; do ps -o args= $i; done
returns all processes that use the nohup.out file

Upstart resulting in two processes - why?

I am using upstart to start a NodeJS process which is using NVM (node version manager).
The upstart command is like this:
description "Service to start node app"
author "Barry Steyn"
setuid devuser
setgid devuser
env DIR=/home/devuser/nodejs/authentication
script
chdir $DIR
exec bash -c 'source /home/devuser/nvm/nvm.sh && node app'
end script
respawn
This starts node fine, but when I do a ps wax | grep node, I get these two processes:
4284 ? Ss 0:00 bash -c source /home/devuser/nvm/nvm.sh && node app
4316 ? Sl 1:09 node app
Why do I get two processes? Is this in anyway less efficient?
The first process is the instance of bash that started node.js. The second process is the actual node.js process.
The bash process is using a few resources (mostly memory), but is just sitting around waiting on node.js to exit.
I believe you can get rid of the extra bash by doing this:
Replace:
exec bash -c 'source /home/devuser/nvm/nvm.sh && node app'
With:
exec bash -c 'source /home/devuser/nvm/nvm.sh && exec node app'
This gets the bash process to exec node.js without using a fork first. Mostly that means it won't wait around for node.js to exit.

"forever list" says "No forever process running" but it is running an app

I have started an app with
forever start app.js
After that I typed,
forever list
and it shows that
The "sys" module is now called "util". It should have a similar interface.
info: No forever processes running
But I checked my processes with
ps aux | grep node
and it shows that
root 1184 0.1 1.5 642916 9672 ? Ss 05:37 0:00 node
/usr/local/bin/forever start app.js
root 1185 0.1 2.1 641408 13200 ? Sl 05:37 0:00 node
/var/www/app.js
ubuntu 1217 0.0 0.1 7928 1060 pts/0 S+ 05:41 0:00 grep --color=auto node
I cannot control over the process, since I cannot list the process in "forever list"
How can I let "Forever" knowing its running processes and let having control over its running processes.
forever list should be invoked with same user as that of processes.
Generally it is root user (in case of ubuntu upstart unless specified) so you can switch to root user using sudo su and then try forever list.
PS. Moved to pm2 recently which has lot more features than forever.
i had the same problem today.
in my case: i'm using NVM and forgot that it doesn't set/modify the global node path, so i had to set it manually
export NODE_PATH="/root/.nvm/v0.6.0/bin/node"
If you exec the forever start app.js within init.d you should later type sudo HOME=/home/pi/devel/web-app -u root forever list to have the correct list.
A fix would be great for this.
Encountered this one as well.
I believe an this issue was logged here.
What I could recommend for now is to find the process that's using your node port e.g. 3000. Like so:
sudo lsof -t -i:3000
That command will show the process id.
Then kill the process by performing:
kill PID
sudo su
forever list
This will output the correct list (processes started by root user).

Resources