How can I tell on CentOS how many node programs are currently running? - node.js

If I have node on my machine, and I run four different node programs, is there a way for me to tell from the CLI how many programs are running?
It's usefull, I think, when I need to tell if a specific node program is on, or just to know how many programs node is running.
Thanks!

Well, you can always run:
$ ps -aef | grep node

If you're only bothered about how many:
$ ps aux | grep `which node` | wc -l
And if you actually want to see details of them, just lop the wordcount (wc -l( command off the end:
$ ps aux | grep `which node`
NB: Using which node will return the location of whichever node executable is to be run by that shell, at that time - this may differ if you run it in a script somewhere, versus at your own terminal prompt, and it may differ a lot if you use NVM. If you have multiple node binaries, you could use grep node but be aware that this may find other processes which include the word node, and sometimes there are many - on a dev machine it will find running versions of Slack, and other desktop apps that use node

Related

centos | perl processes are not shown

I have a centos server in which I have install perl package to run some perl scripts. today I run some perl scripts, and when I run ps -ef | grep perl it shows nothing although the scripts are working properly.
When I use pkill -f (name_of_script) the perl process stopped however they are not shown at all.
Note that yesterday I deleted a user ( X ) which was affected to folder /home/scripts. What do you think the problem is from?
The reason for not showing the process in ps -ef might be due to the process being run as a background process. In that case, the process won't be associated with the terminal where you started it from and thus won't be shown in the output of ps -ef. To see all processes running on the system, including background processes, you can use ps aux instead.

Bash, display processes in specific folder

I need to display processes, that are running in specific folder.
For example, there are folders "TEST" and "RUN". 3 sql files are running from TEST, and 2 from RUN. So when I use command ps xa, I can see all processes, runned from TEST and RUN together. What I want is to see processes, runned only from TEST folder, so only 3. Any commands, solutions to do this?
You can use lsof for this.
lsof | grep '/path/of/RUN'.
If you want to include both RUN and TEST in same command
lsof | grep -E "/path/of/RUN|/path/of/TEST"
Hope it helps.
You can try fuser to see which processes have particular files open; or, on Linux, examine the /proc/12345/cwd symlink for each of the candidate processes (replace 12345 with the process id of each).
fuser TEST/*.sql
for proc in /proc/[1-9]*; do
readlink "$proc/cwd" | grep -q TEST && echo "$proc"
done
The latter is not portable to other U*xes, though some may offer similar facilities.

Node Restart in Shell Script

I want to rerun the node server, whenever there's changes in file, but i want to use fswatch. i am using fswatch with the shell script like
nohup node server.js &
but i can't run the same script again since it will throw EADDRINUSE.
whats the best way to restart the node via script with the help of fswatch (or alternate, without any new installation) ?
Why node doesn't have something like node restart?
what i could think of is get pid from ps and kill it with script, but i guess there should be a better solution.
I am able to do that with the help of fswatch.
fswatch -o mydir | xargs -n1 -I{} ps | grep '[n]ode server.js$' | awk '{system("kill "$1)}'
or putting the same code in seperate shell file. and using it as
xargs -n1 './location-of-shell-file.sh'
when i run grep, that process is also included in ps, so to exclude that i had to use
grep '[n]ode server.js'
EADDRINUSE comes because of you are already bind on the same port. Node.js has no build-in restart mechanics, so yes, you should use some bash scripts (or frameworks) for good restarting background app

Shell script to avoid killing the process that started the script

I am very new to shell scripting, can anyone help to solve a simple problem: I have written a simple shell script that does:
1. Stops few servers.
2. Kills all the process by user1
3. Starts few servers .
This script runs on the remote host. so I need to ssh to the machine copy my script and then run it. Also Command I have used for killing all the process is:
ps -efww | grep "user1"| grep -v "sshd"| awk '{print $2}' | xargs kill
Problem1: since user1 is used for ssh and running the script.It kills the process that is running the script and never goes to start the server.can anyone help me to modify the above command.
Problem2: how can I automate the process of sshing into the machine and running the script.
I have tried expect script but do I need to have a separate script for sshing and performing these tasksor can I do it in one script itself.
any help is welcomed.
Basically the answer is already in your script.
Just exclude your script from found processes like this
grep -v <your script name>
Regarding running the script automatically after you ssh, have a look here, it can be done by a special ssh configuration
Just create a simple script like:
#!/bin/bash
ssh user1#remotehost '
someservers stop
# kill processes here
someservers start
'
In order to avoid killing itself while stopping all user's processes try to add | grep -v bash after grep -v "sshd"
This is a problem with some nuance, and not straightforward to solve in shell.
The best approach
My suggestion, for easier system administration, would be to redesign. Run the killing logic as root, for example, so you may safely TERMinate any luser process without worrying about sawing off the branch you are sitting on. If your concern is runaway processes, run them under a timeout. Etc.
A good enough approach
Your ssh login shell session will have its own pseudo-tty, and all of its descendants will likely share that. So, figure out that tty name and skip anything with that tty:
TTY=$(tty | sed 's!^/dev/!!') # TTY := pts/3 e.g.
ps -eo tty=,user=,pid=,cmd= | grep luser | grep -v -e ^$TTY -e sshd | awk ...
Almost good enough approaches
The problem with "almost good enough" solutions like simply excluding the current script and sshd via ps -eo user=,pid=,cmd= | grep -v -e sshd -e fancy_script | awk ...) is that they rely heavily on the accident of invocation. ps auxf probably reveals that you have a login shell in between your script and your sshd (probably -bash) — you could put in special logic to skip that, too, but that's hardly robust if your script's invocation changes in the future.
What about question no. 2? (How can I automate sshing...?)
Good question. Off-topic. Try superuser.com.

Linux display currently running processes

What is the command to display currently running processes and the option to display PPID's?
I thought that it might be:
jobs -p
jobs -pl
Neither worked though. Any hints?
ps -ef will display all processes and include PPIDs.
it really depends on what you're after
you usually use
top
to see how processes consume system resources
however, if you just want to see some process pid, and know some word from the command that used to run it, try:
ps -ef | grep java
there are other (many) commands and tools, it really depends on the reason you look for the process
for getting one using your username: top -u username idk how to do it with ps but that would be nice to know.

Resources