Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, September 15, 2016

Easiest way to simulate disk full error and out of memory error in a program in Linux

What is /dev/full?

 /dev/full (aka Always Full device) is a special file in Linux, which always returns "No space left on device"  error when a program or a person tries to write to this file. It also provides infinite number of NULL characters when a program tries to read from this file.

The main purpose of this file is for testing programs against "Disk Full" error.

Disk Full Error

The easiest way to demonstrate the "Disk Full" error is by using "echo" command to write something to "/dev/full" file. For testing the "Disk Full" condition in your program, you can try to write the data to "/dev/full" file like you do for writing to a normal file.



Out of Memory Error:

 When you read the /dev/full file, it provides infinite number of NULL characters. Therefore if you use a function which read until the new line (\n) character, it will read the characters to memory until it gets an Out of Memory error. For example, I have created following Java program which uses java.io.BufferedReader.readLine() method. Because this readLine() method read the null characters from the /dev/full without finding any new line characters, the heap space becomes full and it throws java.lang.OutOfMemoryError.


Output:





Monday, September 2, 2013

How to find what is the current working directory of a remotely logged ssh session to a Linux server and disconnect remote sessions

First you need to get the list of users currently connected to your server. You can use "last" command to achieve that. "last" command shows the list of users currently logged in or recently logged in to the system through various terminals. If the user still is connected to the system through the relevant terminal, you can see "still logged in" part in the output row related to that user.
If the user is connected remotely (for example, by ssh), you can see the his terminals related to remote sessions has the prefix "pts" (Pseudo Terminals) and in the third column of output, you can see the remote IP of the machine which the user connected from.

[root@10 ~]# last | grep "still logged"
root     pts/2        192.168.18.1     Mon Sep  2 21:42   still logged in   
root     pts/1        :0.0             Mon Sep  2 21:12   still logged in   
root     :0                            Mon Sep  2 21:11   still logged in 

Then you can find the processes originated by  the user by a connected pseudo terminal using "ps" command and grep command.

[root@10 ~]# ps -ef | grep -v grep | grep "pts/2"
root      6224  3971  0 21:42 ?        00:00:00 sshd: root@pts/2 
root      6229  6224  0 21:42 pts/2    00:00:00 -bash

In the above output, you can see the processes initiated by the remote connection. The first process "sshd" is the process used to initiate the remote connection to this machine. The next process is the shell the remote user has logged into. Therefore the process we needs to check is the second process which "-bash" shell process.

By killing "sshd: root@pts/2" or "-bash" process (process ID 6224 or 6229), you can disconnect this user from the server.

We can use one of following command to get the current working directory of the remotely connected user in "pts/2".

[root@10 ~]# pwdx 6229
6229: /product/softwares

Or

[root@10 ~]# readlink /proc/6229/cwd
/product/softwares

Or

[root@10 ~]# lsof -p 6229 
COMMAND  PID USER   FD   TYPE DEVICE     SIZE    NODE NAME
bash    6229 root  cwd    DIR  253,0     4096  397609 /product/softwares
bash    6229 root  rtd    DIR  253,0     4096       2 /

How to get the current working directory of a process in Linux

Sometimes we encounter  situations where you need to  find the current working directory of a running process. There are several ways to get this information in a Linux machine.

Method 1:

We can get the current working directory (and more details) of a process with pid $PID, by checking the contents of /proc/$PID folder.

Example:

For a process with process ID 3551

[root@10 ~]# readlink /proc/3551/cwd
/etc/sysconfig/network-scripts

Or with more details,

[root@10 ~]# ll /proc/3551
total 0
dr-xr-xr-x 2 root root 0 Sep  2 21:15 attr
-r-------- 1 root root 0 Sep  2 21:15 auxv
-r--r--r-- 1 root root 0 Sep  2 21:05 cmdline
-rw-r--r-- 1 root root 0 Sep  2 21:15 coredump_filter
-r--r--r-- 1 root root 0 Sep  2 21:15 cpuset
lrwxrwxrwx 1 root root 0 Sep  2 21:15 cwd -> /etc/sysconfig/network-scripts
-r-------- 1 root root 0 Sep  2 21:15 environ
lrwxrwxrwx 1 root root 0 Sep  2 21:05 exe -> /sbin/dhclient
dr-x------ 2 root root 0 Sep  2 21:15 fd
dr-x------ 2 root root 0 Sep  2 21:15 fdinfo
-r--r--r-- 1 root root 0 Sep  2 21:15 io
-r--r--r-- 1 root root 0 Sep  2 21:15 limits
-rw-r--r-- 1 root root 0 Sep  2 21:15 loginuid
-r--r--r-- 1 root root 0 Sep  2 21:15 maps
-rw------- 1 root root 0 Sep  2 21:15 mem
-r--r--r-- 1 root root 0 Sep  2 21:15 mounts
-r-------- 1 root root 0 Sep  2 21:15 mountstats
-rw-r--r-- 1 root root 0 Sep  2 21:15 oom_adj
-r--r--r-- 1 root root 0 Sep  2 21:15 oom_score
lrwxrwxrwx 1 root root 0 Sep  2 21:15 root -> /
-r--r--r-- 1 root root 0 Sep  2 21:15 schedstat
-r--r--r-- 1 root root 0 Sep  2 21:15 smaps
-r--r--r-- 1 root root 0 Sep  2 21:05 stat
-r--r--r-- 1 root root 0 Sep  2 21:15 statm
-r--r--r-- 1 root root 0 Sep  2 21:12 status
dr-xr-xr-x 3 root root 0 Sep  2 21:15 task
-r--r--r-- 1 root root 0 Sep  2 21:15 wchan

Method 2:

Use pwdx command.

Example:

For a process with process ID 3551


[root@10 ~]# pwdx 3551
3551: /etc/sysconfig/network-scripts

Method 3:

Use lsof command.

Example:

For a process with process ID 3551

[root@10 ~]# lsof -p 3551
dhclient  3551      root  cwd       DIR      253,0     4096    1769476 /etc/sysconfig/network-scripts
dhclient  3551      root  rtd       DIR      253,0     4096          2 /
....

Monday, July 22, 2013

Different behaviour of Source or (dot) or (period) operator when passing arguments in Linux and Solaris

The source operator is used for including another file in current script and execute the lines in that file in the current shell context. Therefore the final result is same as that the lines in the sourced file were physically presented in the parent file. I recently found out that the behavior of source operator is different in Linux and Solaris.
If you pass command line arguments to a file you are sourcing inside another shell script, and when you retrieve the passed command line arguments in child (sourced) file,

In Linux,
You will get the parameters you passed as command line parameters when you sourcing the file.

In Solaris,
You will get the command line parameters passed to the parent file as the command line parameters for the child file also.

Therefore if you have a file named test.sh with following lines, (testEcho.sh will echo all the command line arguments passed to it).

. "./testEcho.sh" ted
sh ./testEcho.sh ted


and if you execute it like below,
sh ./test.sh param1 param2 param3


Output in linux,
ted
ted

Output in Solaris,
param1 param2 param3
      ted

Friday, June 21, 2013

How to highlight a searching text in "tail -f" output

When we are investigating an software issue of a software run on a unix machine, we normally use "tail -f" command to see the live log messages logged by the software (for e.g: checking managed server logs in a weblogic server). If you are looking the log for finding a specific log message, it will be easier if you can make the search word highlighted in some color in the output.

Following method is a one of way you can achieve that task. Please note that to write ^[ character, you should press Ctrl+V and then Ctrl+[

tail -f testEcho.txt | sed "s/\\(search\\)/^[[33;1m\\1^[[0m/g"