shell-查看MySQL状态

ps -ef

[root@nginx ~]# cat mariadb.sh 
#!/bin/bash
systemctl status mariadb &> /dev/null
if [ $? -eq 0 ]; then
	echo "mysql is running"
else
	echo "警告: mysql服务不可用"
	echo "重启mysql服务: $(systemctl restart mariadb &> /dev/null)"
	echo "mysql服务是否运行: $(ps -ef ) | grep mariadbd "
fi 
[root@nginx ~]# systemctl stop mariadb       #关闭mysql
[root@nginx ~]# ./mariadb.sh 
警告: mysql服务不可用
重启mysql服务: 
mysql服务是否运行: UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 13:55 ?        00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 13:55 ?        00:00:00 [kthreadd]
root          4      2  0 13:55 ?        00:00:00 [kworker/0:0H]
root          6      2  0 13:55 ?        00:00:00 [ksoftirqd/0]
root          7      2  0 13:55 ?        00:00:00 [migration/0]
root          8      2  0 13:55 ?        00:00:00 [rcu_bh]
root          9      2  0 13:55 ?        00:00:00 [rcu_sched]
root         10      2  0 13:55 ?        00:00:00 [lru-add-drain]
root         11      2  0 13:55 ?        00:00:00 [watchdog/0]
root         13      2  0 13:55 ?        00:00:00 [kdevtmpfs]
root         14      2  0 13:55 ?        00:00:00 [netns]
root         15      2  0 13:55 ?        00:00:00 [khungtaskd]
root         16      2  0 13:55 ?        00:00:00 [writeback]
root         17      2  0 13:55 ?        00:00:00 [kintegrityd]
root         18      2  0 13:55 ?        00:00:00 [bioset]
root         19      2  0 13:55 ?        00:00:00 [bioset]
root         20      2  0 13:55 ?        00:00:00 [bioset]
root         21      2  0 13:55 ?        00:00:00 [kblockd]
root         22      2  0 13:55 ?        00:00:00 [md]
root         23      2  0 13:55 ?        00:00:00 [edac-poller]
root         24      2  0 13:55 ?        00:00:00 [watchdogd]
root         30      2  0 13:55 ?        00:00:00 [kswapd0]
root         31      2  0 13:55 ?        00:00:00 [ksmd]
root         32      2  0 13:55 ?        00:00:00 [khugepaged]
root         33      2  0 13:55 ?        00:00:00 [crypto]
root         41      2  0 13:55 ?        00:00:00 [kthrotld]
root         42      2  0 13:55 ?        00:00:00 [kworker/u256:1]
root         43      2  0 13:55 ?        00:00:00 [kmpath_rdacd]
root         44      2  0 13:55 ?        00:00:00 [kaluad]
root         45      2  0 13:55 ?        00:00:00 [kpsmoused]
root         47      2  0 13:55 ?        00:00:00 [ipv6_addrconf]
root         60      2  0 13:55 ?        00:00:00 [deferwq]
root         95      2  0 13:55 ?        00:00:00 [kauditd]
root        273      2  0 13:56 ?        00:00:00 [ata_sff]
root        278      2  0 13:56 ?        00:00:00 [mpt_poll_0]
root        279      2  0 13:56 ?        00:00:00 [mpt/0]
root        294      2  0 13:56 ?        00:00:00 [nfit]
root        295      2  0 13:56 ?        00:00:00 [scsi_eh_0]
root        296      2  0 13:56 ?        00:00:00 [scsi_tmf_0]
root        302      2  0 13:56 ?        00:00:00 [scsi_eh_1]
root        304      2  0 13:56 ?        00:00:00 [scsi_tmf_1]
root        305      2  0 13:56 ?        00:00:00 [scsi_eh_2]
root        307      2  0 13:56 ?        00:00:00 [scsi_tmf_2]
root        310      2  0 13:56 ?        00:00:00 [irq/16-vmwgfx]
root        311      2  0 13:56 ?        00:00:00 [ttm_swap]
root        384      2  0 13:56 ?        00:00:00 [kdmflush]
root        385      2  0 13:56 ?        00:00:00 [bioset]
root        395      2  0 13:56 ?        00:00:00 [kdmflush]
root        396      2  0 13:56 ?        00:00:00 [bioset]
root        408      2  0 13:56 ?        00:00:00 [bioset]
root        409      2  0 13:56 ?        00:00:00 [xfsalloc]
root        410      2  0 13:56 ?        00:00:00 [xfs_mru_cache]
root        411      2  0 13:56 ?        00:00:00 [xfs-buf/dm-0]
root        412      2  0 13:56 ?        00:00:00 [xfs-data/dm-0]
root        413      2  0 13:56 ?        00:00:00 [xfs-conv/dm-0]
root        414      2  0 13:56 ?        00:00:00 [xfs-cil/dm-0]
root        415      2  0 13:56 ?        00:00:00 [xfs-reclaim/dm-]
root        416      2  0 13:56 ?        00:00:00 [xfs-log/dm-0]
root        417      2  0 13:56 ?        00:00:00 [xfs-eofblocks/d]
root        418      2  0 13:56 ?        00:00:00 [xfsaild/dm-0]
root        419      2  0 13:56 ?        00:00:00 [kworker/0:1H]
root        498      1  0 13:56 ?        00:00:00 /usr/lib/systemd/systemd-journald
root        519      1  0 13:56 ?        00:00:00 /usr/sbin/lvmetad -f
root        521      1  0 13:56 ?        00:00:00 /usr/lib/systemd/systemd-udevd
root        682      2  0 13:56 ?        00:00:00 [xfs-buf/sda1]
root        685      2  0 13:56 ?        00:00:00 [xfs-data/sda1]
root        692      2  0 13:56 ?        00:00:00 [xfs-conv/sda1]
root        695      2  0 13:56 ?        00:00:00 [xfs-cil/sda1]
root        699      2  0 13:56 ?        00:00:00 [xfs-reclaim/sda]
root        701      2  0 13:56 ?        00:00:00 [xfs-log/sda1]
root        702      2  0 13:56 ?        00:00:00 [xfs-eofblocks/s]
root        707      2  0 13:56 ?        00:00:00 [xfsaild/sda1]
root        855      1  0 13:56 ?        00:00:00 /sbin/auditd
root        932      1  0 13:56 ?        00:00:00 /usr/bin/VGAuthService -s
root        933      1  0 13:56 ?        00:00:02 /usr/bin/vmtoolsd
root        935      1  0 13:56 ?        00:00:00 /usr/sbin/abrtd -d -s
root        940      1  0 13:56 ?        00:00:00 /usr/bin/abrt-watch-log -F BUG: WARNING: at WARNING: CPU: INFO: possible recursive locking detected ernel BUG at list_del corruption list_add corruption do_IRQ: stack overflow: ear stack overflow (cur: eneral protection fault nable to handle kernel ouble fault: RTNL: assertion failed eek! page_mapcount(page) went negative! adness at NETDEV WATCHDOG ysctl table check failed : nobody cared IRQ handler type mismatch Kernel panic - not syncing: Machine Check Exception: Machine check events logged divide error: bounds: coprocessor segment overrun: invalid TSS: segment not present: invalid opcode: alignment check: stack segment: fpu exception: simd exception: iret exception: /var/log/messages -- /usr/bin/abrt-dump-oops -xtD
polkitd     950      1  0 13:56 ?        00:00:00 /usr/lib/polkit-1/polkitd --no-debug
root        956      1  0 13:56 ?        00:00:00 /usr/lib/systemd/systemd-logind
dbus        960      1  0 13:56 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root       1003      1  0 13:56 ?        00:00:00 /usr/sbin/crond -n
root       1018      1  0 13:56 ?        00:00:00 login -- root
root       1426      1  0 13:56 ?        00:00:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root       1427      1  0 13:56 ?        00:00:00 /usr/sbin/sshd -D
  ot       1428      1  0 13:56 ?        00:00:00 /usr/sbin/rsyslogd -n
root       1557      1  0 13:56 ?        00:00:00 /usr/libexec/postfix/master -w
postfix    1559   1557  0 13:56 ?        00:00:00 qmgr -l -t unix -u
root       1674   1018  0 13:56 tty1     00:00:00 -bash
root       3297   1674  0 13:58 tty1     00:00:00 bash
root       3817      2  0 13:59 ?        00:00:00 [kworker/u257:0]
root       3818      2  0 13:59 ?        00:00:00 [hci0]
root       3819      2  0 13:59 ?        00:00:00 [hci0]
root       3823      2  0 13:59 ?        00:00:00 [kworker/u257:2]
root       3915   1427  0 14:00 ?        00:00:00 sshd: root@pts/0
root       3917   3915  0 14:00 pts/0    00:00:00 -bash
postfix    4011   1557  0 14:00 ?        00:00:00 pickup -l -t unix -u
root       4012   3917  0 14:00 pts/0    00:00:00 bash
root       4074      1  0 14:01 ?        00:00:00 /usr/sbin/anacron -s
root       6644      1  0 14:02 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     6697   6644  0 14:06 ?        00:00:00 nginx: worker process
root       7292      2  0 15:00 ?        00:00:00 [kworker/0:2]
root       7761      2  0 15:06 ?        00:00:00 [kworker/0:3]
root       8015      2  0 15:07 ?        00:00:00 [kworker/u256:0]
root       8265   1427  0 15:10 ?        00:00:00 sshd: root@pts/1
root       8267   8265  0 15:10 pts/1    00:00:00 -bash
root       8298      2  0 15:11 ?        00:00:00 [kworker/0:0]
root       8597      2  0 15:13 ?        00:00:00 [kworker/u256:2]
root       8598   4012  0 15:13 pts/0    00:00:00 /bin/bash ./mariadb.sh
mysql      8641      1  0 15:13 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql      8803   8641  1 15:13 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root       8838   8598  0 15:14 pts/0    00:00:00 ps -ef | grep mariadbd 

用nststat -ntlp更直观

[root@nginx ~]# cat mariadb.sh 
#!/bin/bash
systemctl status mariadb &> /dev/null
if [ $? -eq 0 ]; then
	echo "mysql is running"
else
	echo "警告: mysql服务不可用"
	echo "重启mysql服务: $(systemctl restart mariadb &> /dev/null)"
	echo "mysql服务是否运行: $( netstat -ntlp ) | grep mariadbd "
fi 
[root@nginx ~]# systemctl stop mariadb     #关闭mairadb
[root@nginx ~]# ./mariadb.sh 
警告: mysql服务不可用
重启mysql服务: 
mysql服务是否运行: Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1557/master         
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      9070/mysqld         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6644/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1427/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1557/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1427/sshd            | gre

你可能感兴趣的:(shell)