uniprocessor or multiprocessor - unix

On unix, how could we know whether the system is multiprocessor or uniprocessor?

Some times we have to answer owr own question :)
On Solaris run the command
/usr/sbin/psrinfo -v|grep "Status of processor"|wc -l
On AIX run the command
lsdev -C|grep Process|wc -l
On HP-UX run the following commands (requires superuser privileges):
P=`echo processor_count/D | adb -k /stand/vmunix /dev/mem |tail -1|awk '{print $2}'` echo "The number of processors on `hostname` = $P"
On Tru64 run the command
/usr/sbin/psrinfo -v|grep "Status of processor"|wc -l

How about
cat /proc/cpuinfo | grep -i 'processor' | wc -l
Or even
dmesg | grep -i cpu
Look out for "Brought up x processors" in the last one

I don't know if it applies to Unix as well, but for Linux, from command line, see here: http://www.howtogeek.com/howto/ubuntu/display-number-of-processors-on-linux/

Related

How can I ssh into a second server, run a command, and assign the output to a variable?

This is what I am attempting to do:
fromServer=$(ssh -A first.com ssh second.com rpm -qa | grep exampleString)
echo $fromServer
echo does not print anything. If I manually shh into first and then ssh into second then run the command I get output:
ssh first.com
ssh second.com
rpm -qa | grep exampleString
How can I combine these three steps into one line and store the output into a variable?
Use proper quoting or escaping:
fromServer=$(ssh -A first.com 'ssh second.com rpm -qa | grep exampleString')
echo $fromServer
or
fromServer=$(ssh -A first.com ssh second.com rpm -qa \| grep exampleString)
echo $fromServer
% VAR=$(ssh -C user#server ls -la \| grep vim)
% echo $VAR
-rw------- 1 user user 15153 Mar 22 13:45 .vimrc
edit: oooooooh, sneaky, I did not see you were doing two SSH ☺
So then you'll need a bit more quoting, because you don't want to have your pipe being interpreted by first.com. Here's three ways to work around that:
fromServer=$(ssh -A first.com ssh second.com rpm -qa \\\| grep exampleString)
fromServer=$(ssh -A first.com 'ssh second.com rpm -qa \| grep exampleString')
fromServer=$("ssh -A first.com 'ssh second.com rpm -qa | grep exampleString'")
What's happening is that you want to execute:
user#second % rpm -qa | grep exampleString
on the second.com server, so you have to escape the pipe so it's not interpreted by the first.com server:
user#first % ssh second.com rpm -qa \| grep exampleString
or
user#first % ssh second.com 'rpm -qa | grep exampleString'
but then again, you need to have that executed on first.com, from your local workstation, as you still don't want to see the pipe interpreted, you need to add a second layer of escaping/quoting:
user#workstation % ssh first.com "ssh second.com 'rpm -qa | grep exampleString'"
or
user#workstation % ssh first.com 'ssh second.com rpm -qa \| grep exampleString'
and then, once you're sure you get an output you can put that whole command's output in a variable:
VAR=$(ssh first.com "ssh second.com 'rpm -qa | grep exampleString'")
HTH

UNIX for loop with some options

I have for loop:
for mnt `cat $file.txt`
do
grep -h -i -A 3 -B 4 *log | grep -v "10001" >> extrafile.txt
done
What does -A 3 and -B 4 means?
After and Before followed by number of lines
After en Before. After and before what?
No wonder the grep is confusing: You don't mention the "${mnt}" you are searching for. When I improve your script (moving input and output to the end, outside the loop, and using ${mnt}), the script looks like
while read -r mnt; do
grep -h -i -A 3 -B 4 "${mnt}" *log | grep -v "10001"
done < "${file.txt}" >> extrafile.txt
You get the context of every hit from $file.txt and delete all lines with 10001.

Kill nohup process unix

I am running jar using nohup command
nohup java -jar Test.jar
I can't find PID of the jar I am running. I try with:
ps ax | grep java
but it is not listing, then I tried with
ps -ef | grep nohup
and I got this output
root 8503 7529 0 21:52 pts/0 00:00:00 grep nohup
which of the PIDs is PID of nohup process? The first one 8503 is always diffrent, while the second one is the same.
Maybe the program test.jar is finished. See output in nohup.out. To avoid to display grep as a process, use the following pattern:
ps -aux | grep 'j[a]va'
or
ps -aux | grep java | grep -v grep
The following command in your terminal may help you out to run the script using nohup and redirect the output in your desired file.
General Syntax
nohup some_command &> nohup_log_file.out &
Example
nohup python script.py &> nohup_log_script.out &

Determine the process pid listening on a certain port

As the title says, I'm running multiple game servers, and every of them has the same name but different PID and the port number. I would like to match the PID of the server which is listening on certain port, and then I would like to kill this process. I need that in order to complete my bash script.
Is that even possible? Because it didn't find yet any solutions on the web.
Short version which you can pass to kill command:
lsof -i:80 -t
The -p flag of netstat gives you PID of the process:
netstat -l -p
*use sudo if showing - instead of PID
Edit: The command that is needed to get PIDs of socket users in FreeBSD is sockstat.
As we worked out during the discussion with #Cyclone, the line that does the job is:
sockstat -4 -l | grep :80 | awk '{print $3}' | head -1
netstat -p -l | grep $PORT and lsof -i :$PORT solutions are good but I prefer fuser $PORT/tcp extension syntax to POSIX (which work for coreutils) as with pipe:
pid=`fuser $PORT/tcp`
it prints pure pid so you can drop sed magic out.
One thing that makes fuser my lover tools is ability to send signal to that process directly (this syntax is also extension to POSIX):
$ fuser -k $port/tcp # with SIGKILL
$ fuser -k -15 $port/tcp # with SIGTERM
$ fuser -k -TERM $port/tcp # with SIGTERM
Also -k is supported by FreeBSD: http://www.freebsd.org/cgi/man.cgi?query=fuser
netstat -nlp should tell you the PID of what's listening on which port.
Syntax:
kill -9 $(lsof -t -i:portnumber)
Example:
To kill the process running at port 4200, run following command
kill -9 $(lsof -t -i:4200)
Tested in Ubuntu.
Since sockstat wasn't natively installed on my machine I hacked up stanwise's answer to use netstat instead..
netstat -nlp | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:2000" | awk '{print $7}' | sed -e "s/\/.*//g""
I wanted to programmatically -- using only Bash -- kill the process listening on a given port.
Let's say the port is 8089, then here is how I did it:
badPid=$(netstat --listening --program --numeric --tcp | grep "::8089" | awk '{print $7}' | awk -F/ '{print $1}' | head -1)
kill -9 $badPid
I hope this helps someone else! I know it is going to help my team.
on windows, the netstat option to get the pid's is -o and -p selects a protocol filter, ex.:
netstat -a -p tcp -o

Kill respawing server running on port 3000 created by `node . > /dev/null 2> /dev/null < /dev/null &`

I'm using AWS CodeDeploy in which server running on pm2 dose not work due to explanation given here in troubleShoot documentation.
I followed the documentation and in AfterInstall script used node . > /dev/null 2> /dev/null < /dev/null & to run the node server in the background.
I've tried following ways to kill the server
fuser -k 3000/tcp
lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9
kill -9 $(lsof -t -i:3000)
but each time a new process respwans with a different PID.
How can I kill this background process and add it to the ApplicationStop script for CodeDeploy?
One of the problems with finding a pid with grep is that the grep pid will also show up as a result and can kill itself before the target, so try;
ps ax | grep node | grep -v grep
if it looks reasonable, review this;
ps ax | grep node | grep -v grep | awk '{print $1}'
then run the kill;
ps ax | grep node | grep -v grep | awk '{print $1}' | xargs kill -9
pkill is a less flexible option (no regex filtering) but if you use that be sure to use the -I flag so you don't kill anything you did not intend to.
I was able to kill using pkill node command.

Resources