Friday, November 4, 2016

How do I display shell command history with date and time under UNIX or Linux operating systems?

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character. Defining the environment variable as follows
root@linuxforfreshers.com:~$ vim .bash_profile

$ HISTTIMEFORMAT="%d/%m/%y %T "
:wq
Or
root@linuxforfreshers.com:~$  echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
root@linuxforfreshers.com:~$ source .bash_profile
Where,
%d – Day
%m – Month
%y – Year
%T – Time
root@linuxforfreshers.com:~$  history
Sample outputs:

986  10/03/1104:31:36 memcached-tool  10.10.28.22:11211 stats
  987  10/03/1104:31:36 w
  988  10/03/1104:31:37 iostat
  989  10/03/1104:31:37 top
  990  10/03/1104:31:37 at
  991  10/03/1104:31:38 atop
  992  10/03/1104:31:40 collectl
  993  10/03/1104:31:41 grep CPU /proc/cpuinfo
  994  10/03/1104:31:45 vmstat 3 100
  995  10/03/1104:31:55 sar -W -f /var/log/sa/sa12

References:
For more info type the following commands:
man bash
help history
man 3 strftime



Thursday, November 3, 2016

How to check which port number mysql server is running on linux ?

Method 1:

To find a listener on a port, do this:

root@linuxforfreshers.com:~$netstat -tln | grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN     

Or

root@linuxforfreshers.com:~$netstat -tlpn | grep mysql
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      870/mysqld


Method 2:

root@linuxforfreshers.com:~$grep port /etc/mysql/my.cnf
# One can use all long options that the program supports.
# It has been reported that passwords should be enclosed with ticks/quotes
port                 = 3306
port                 = 3306

Method 3:

Using Mysql Query
root@linuxforfreshers.com:~$  mysql –u username –p password

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |


+---------------+-------+

how to check mysql server uptime in linux ?


Method 1:
Using command line

root@linuxforfreshers.com:~$mysqladmin  version | grep Uptime

Uptime:                                   30 sec

Method 2:

root@linuxforfreshers.com:~$mysqladmin  status
Uptime: 121  Threads: 1  Questions: 116  Slow queries: 0  Opens: 46  Flush tables: 1  Open tables: 41  Queries per second avg: 0.958

Method 3:

Show MySQL server uptime using a query
root@linuxforfreshers.com:~$ mysql –u username –p password
mysql> SHOW GLOBAL STATUS LIKE 'Uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 223   |
+---------------+-------+


(The uptime is given in seconds)

how to check if mysql server is running or not in linux ?


Method 1:

root@linuxforfreshers.com:~$ service mysql status
Or
root@linuxforfreshers.com:~$ /etc/init.d/mysql status
mysql start/running, process 13749

Method 2:
root@linuxforfreshers.com:~$ps aux | grep mysql
mysql    13749  0.2  1.3 492660 52336 ?        Ssl  13:05   0:38 /usr/sbin/mysqld

Method 3:
root@linuxforfreshers.com:~$netstat -vulntp |grep -i mysql
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      13749/mysqld    
Method 4:
root@linuxforfreshers.com:~$lsof -i :3306
COMMAND   PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
mysqld  13749 mysql   10u  IPv4 5037327      0t0  TCP localhost:mysql (LISTEN)

Method 5:
root@linuxforfreshers.com:~$mysqladmin ping

mysqld is alive

Tuesday, October 4, 2016

who command examples in linux ?

You need to use who command to display users who are currently logged in your server

Purpose
Display who is on the system
Syntax
The basic syntax is as follows:
who
who am i
who [options] [File]
who --help
who --version
who | grep 'userNameHere'

who command examples
To show a list of all the users currently logged in to the system, type:
$ who
root@linuxforfreshers.com:~$ who
root     :0           2016-10-03 07:46 (:0)
root     pts/61       2016-10-04 08:08 (:0)
root     pts/46       2016-10-04 07:58 (:0)

To show only hostname and user associated with stdin (usually keyboard), enter:
$ who -m
root@linuxforfreshers.com:~$ who -m
root     pts/61       2016-10-04 08:08 (:0)

To show active processes spawned by init:
$ who -p

To show user’s message status as +, – or ?, enter:
$ who -T

root@linuxforfreshers.com:~$ who -T
root     ? :0           2016-10-03 07:46 (:0)
root     + pts/61       2016-10-04 08:08 (:0)
root     + pts/46       2016-10-04 07:58 (:0)

Show or list users logged in
Type the command:
$ who -u
root@linuxforfreshers.com:~$ who -u
root     :0           2016-10-03 07:46   ?          3431 (:0)
root     pts/61       2016-10-04 08:08   .         21700 (:0)
root     pts/46       2016-10-04 07:58 01:46       21700 (:0)

Show time of last system boot
To display time of last system boot pass the -b option to who command:
$ who -b
root@linuxforfreshers.com:~$ who -b
         system boot  2016-10-03 07:44

Show dead processes on the system
You need pass the -d option to who command:
$ who -d
OR
$ who -d -H
root@linuxforfreshers.com:~$ who -d
         pts/9        2016-10-03 16:56                 0 id=/9    term=0 exit=0
         pts/58       2016-10-03 16:56                 0 id=/58   term=0 exit=0
         pts/0        2016-10-03 16:56                 0 id=/0    term=0 exit=0
         pts/60       2016-10-04 15:55                 0 id=/60   term=0 exit=0
         pts/62       2016-10-04 15:55                 0 id=/62   term=0 exit=0

Show system login processes
To just display system login processes pass the -l option:
$ who -l
OR
$ who -l -H
root@linuxforfreshers.com:~$ who -l
LOGIN    tty4         2016-10-03 07:44              1675 id=4
LOGIN    tty5         2016-10-03 07:44              1679 id=5
LOGIN    tty2         2016-10-03 07:44              1687 id=2
LOGIN    tty3         2016-10-03 07:44              1688 id=3
LOGIN    tty6         2016-10-03 07:44              1691 id=6
LOGIN    tty1         2016-10-03 07:44              3142 id=1

Count all login names and number of users logged on the system
To count all login names and number of users logged on:
$ who -q
root@linuxforfreshers.com:~$  who -q
root root root
# users=3

Display the current runlevel
To display the current system runlevel, type:
$ who -r
root@linuxforfreshers.com:~$ who -r
         run-level 2  2016-10-03 07:44

You can combine -r and -b options as follows:
$ who -r -b
root@linuxforfreshers.com:~$ who -r -b
         system boot  2016-10-03 07:44
         run-level 2  2016-10-03 07:44
root@linuxforfreshers.com:~$ who am i
root     pts/61       2016-10-04 08:08 (:0)

Displays "all" information, and headers above each column of data, for example:

root@linuxforfreshers.com:~$ who -aH
NAME       LINE         TIME             IDLE          PID COMMENT  EXIT
           system boot  2016-10-03 07:44
           run-level 2  2016-10-03 07:44
LOGIN      tty4         2016-10-03 07:44              1675 id=4
LOGIN      tty5         2016-10-03 07:44              1679 id=5
LOGIN      tty2         2016-10-03 07:44              1687 id=2
LOGIN      tty3         2016-10-03 07:44              1688 id=3
LOGIN      tty6         2016-10-03 07:44              1691 id=6
LOGIN      tty1         2016-10-03 07:44              3142 id=1
root     ? :0           2016-10-03 07:46   ?          3431 (:0)
           pts/9        2016-10-03 16:56                 0 id=/9    term=0 exit=0
           pts/58       2016-10-03 16:56                 0 id=/58   term=0 exit=0
root     + pts/61       2016-10-04 08:08   .         21700 (:0)
           pts/0        2016-10-03 16:56                 0 id=/0    term=0 exit=0
root     + pts/46       2016-10-04 07:58 01:39       21700 (:0)
           pts/60       2016-10-04 15:55                 0 id=/60   term=0 exit=0
           pts/62       2016-10-04 15:55                 0 id=/62   term=0 exit=0