How to find the process id listening on a port?

On Windows – Example trying to find which process is listening on port 1433

Show if any process is listening on port 1433 for example
c:\temp> netstat -ano | find /I “1433” | find /I “LISTEN”
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:1433 0.0.0.0:0 LISTENING 6244

Using tlist or task manager one can find the PID (last column) from the above command to find the process
c:\temp> tlist | find /I “6244”
6244 sqlservr.exe

On Linux
Using netsat find if any process is listening to port 22
$ netstat -an | grep 22 | grep LISTEN
tcp 0 0 :::22 :::* LISTEN

Using fuser one can find the process id listening on the port, the process id is 3788, one has to be root to see the result
[root@localhost ~]# fuser -v 22/tcp
here: 22

USER PID ACCESS COMMAND
22/tcp root 3788 f…. sshd

Using lsof using the port#, below shows SSH is listening to port and process id is 3788. This command needs to be run as root.
[root@localhost ~]# /usr/sbin/lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
sshd 3788 root 3u IPv6 8374 TCP *:ssh (LISTEN)

On AIX
https://www-304.ibm.com/support/docview.wss?uid=swg21264632

Advertisements

process control

# lists jobs in current session
$ jobs

# start job 1 (sleeps for 200 seconds)
$ sleep 200

# suspended job 1 using control-z
[1] + Stopped sleep 200

# start job 2 (sleeps for 400 seconds)
$ sleep 400

# suspended job 2 using control-z
[2] + Stopped sleep 400

# lists jobs in current session shows the jobs are suspended i.e. not running
$ jobs
[2] + Stopped sleep 400
[1] – Stopped sleep 200

# place sleep 400 in background
$ bg %2
[2] sleep 400

# shows job status sleep 400 is running and 1 is stopped/suspended
$ jobs
[1] + Stopped sleep 200
[2] – Running sleep 400

# place sleep 200 in background
$ bg %1
[1] sleep 200

# shows job status both are running
$ jobs
[1] + Running sleep 200
[2] – Running sleep 400

# place sleep 200 job in foreground
$ fg %1
sleep 200

# pressed control-z to suspend sleep 200
[1] + Stopped sleep 200

# show job status sleep 200 is suspended and sleep 400 is running
$ jobs
[1] + Stopped sleep 200
[2] – Running sleep 400

# wait for suspended job wait will return immediately as it is not running
$ wait %1

# show job status sleep 200 is suspended and sleep 400 is running
$ jobs
[1] + Stopped sleep 200
[2] – Running sleep 400

# place job 1 (sleep 200) in background so it will run
$ bg %1
[1] sleep 200

# wait tells to wait for sleep 200 to finish
$ wait %1

[2] + Done sleep 400

Background process

There are multiple ways to place a program in background mode. Background mode is when the program runs and returns you the prompt back and the program is till running but it can be tied to the session or untied to the shell.

The following scenario is when a process is tied to a shell:
Example: $ sleep 60 &
In the above example you are starting “sleep” in background, it will return the prompt back to you while the program runs.

In the case if the program is started but not in background mode, by pressing Ctrl-Z will suspended the program and return the prompt back to you, at that point the program is no longer running i.e. is suspended, so typing “bg” will place the program in background mode and return the prompt back to you.

Example: $ sleep 60
<Press Ctrl-Z>
Stopped
$ bg

By using nohup infront of the command the command is not tied to shell, example:
$ nohup sleep 60 &

ksh – Process

— Get process id of current process
$ echo $$
6113
— shows process id
$ ps
PID TTY TIME CMD
2050 pts/2 00:00:00 ps
6113 pts/2 00:00:00 ksh

Useful when creating a unique temp file example TEMPFILE=/tmp/tmpfile.$$

— Get process id of last background id
$ sleep 10 &
[1] 2078
$ echo $!
2078

— Print parent process id
$ print $PPID
6048

How to find/set max processes allowed per user on OS-level?

On AIX
— List number of max processes allowed per user
$ /home/oracle:$ /usr/sbin/lsattr -E -l sys0 -a maxuproc
maxuproc 2048 Maximum number of PROCESSES allowed per user True

The value returned by ulimit may not always match so use the value returned by lsattr.

To change the value of user process by running chdev
$ chdev -l sys0 -a maxumproc=new_limit

How to change unix prompt?

C-shell (csh)
Example: set prompt=”[%n@%m %c] \! %”
oracle@localhost ~] 39 %
In this above example sets prompt to display username, hostname, current working directory and history number.
%n – username, %m – hostname, %c – pwd, \! – history number

K-shell (ksh)
Example: $ export PS1=’$USER [$PWD] $ ‘
oracle [/home] $
In the above example, sets prompt to display username and current working directory.

whereiz – find all version of a command

Using the following script it will find all version of a command in $PATH (from Unix Power Tools)

$ cat /tmp/whereiz
#!/bin/sh
testx=”test -x”

fixpath=”`echo $PATH | sed \
-e 's/^:/.:/' \
-e 's/::/:.:/g' \
-e 's/:$/:./'`”

# echo $fixpath
IFS=”: ”
for command
do
where=””
for direc in $fixpath
do $testx $direc/$command && where=”$where $direc/$command”
done
case “$where” in
?*) echo $where ;;
esac
done

Example
$ /tmp/whereiz ls grep
/bin/ls /tmp/ls
/bin/grep