Linux操作系统的进程管理和信号

                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.lsof命令详解

1>.lsof概述

list open files查看当前系统文件的工具。在linux环境下,一切皆文件,用户通过文件不仅可以访问常规数据,还可以访问网络连接和硬件如传输控制协议 (TCP) 和用户数据报协议 (UDP)套接字等,系统在后台都为该应用程序分配了一个文件描述符

2>.命令参数

-a:
  列出打开文件存在的进程
-c<进程名>:
  列出指定进程所打开的文件
-g:
  列出GID号进程详情
-d<文件号>:
  列出占用该文件号的进程
+d<目录>:
  列出目录下被打开的文件
+D<目录>:
  递归列出目录下被打开的文件
-n<目录>:
  列出使用NFS的文件
-i<条件>:
  列出符合条件的进程(、、协议、:端口、 @ip )
-p<进程号>:
  列出指定进程号所打开的文件
-u:
  列出UID号进程详情
-h:
  显示帮助信息
-v:
  显示版本信息。
-n:
  不反向解析网络名字

3>.进程管理

[root@node101.yinzhengjie.org.cn ~]# lsof /dev/pts/           #查看由登陆用户启动而非系统启动的进程
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash root 0u CHR , 0t0 /dev/pts/
bash root 1u CHR , 0t0 /dev/pts/
bash root 2u CHR , 0t0 /dev/pts/
bash root 255u CHR , 0t0 /dev/pts/
lsof root 0u CHR , 0t0 /dev/pts/
lsof root 1u CHR , 0t0 /dev/pts/
lsof root 2u CHR , 0t0 /dev/pts/
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof /dev/pts/1    #查看由登陆用户启动而非系统启动的进程

[root@node101.yinzhengjie.org.cn ~]# ps
PID TTY TIME CMD
pts/ :: bash
pts/ :: mysqld_safe
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -p #指定进程号,可以查看该进程打开的文件
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld_sa root cwd DIR , /home/softwares/mysql-5.7.-linux-glibc2.-x86_64
mysqld_sa root rtd DIR , /
mysqld_sa root txt REG , /usr/bin/bash
mysqld_sa root mem REG , /usr/lib/locale/locale-archive
mysqld_sa root mem REG , /usr/lib64/libc-2.17.so
mysqld_sa root mem REG , /usr/lib64/libdl-2.17.so
mysqld_sa root mem REG , /usr/lib64/libtinfo.so.5.9
mysqld_sa root mem REG , /usr/lib64/ld-2.17.so
mysqld_sa root mem REG , /usr/lib64/gconv/gconv-modules.cache
mysqld_sa root 0r CHR , 0t0 /dev/null
mysqld_sa root 1w CHR , 0t0 /dev/null
mysqld_sa root 2u CHR , 0t0 /dev/pts/
mysqld_sa root 255r REG , /home/softwares/mysql-5.7.-linux-glibc2.-x86_64/b
in/mysqld_safe[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof -p 4985 #指定进程号,可以查看该进程打开的文件

4>.文件管理

-c:
查看指定用户打开的文件。 -d:
列出目录下被打开的文件 -D:
递归列出目录下被打开的文件。
[root@node101.yinzhengjie.org.cn ~]# ps
PID TTY TIME CMD
pts/ :: bash
pts/ :: mysqld_safe
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -c mysqld_safe       #查看指定程序打开的文件
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld_sa root cwd DIR , /home/softwares/mysql-5.7.-linux-glibc2.-x86_64
mysqld_sa root rtd DIR , /
mysqld_sa root txt REG , /usr/bin/bash
mysqld_sa root mem REG , /usr/lib/locale/locale-archive
mysqld_sa root mem REG , /usr/lib64/libc-2.17.so
mysqld_sa root mem REG , /usr/lib64/libdl-2.17.so
mysqld_sa root mem REG , /usr/lib64/libtinfo.so.5.9
mysqld_sa root mem REG , /usr/lib64/ld-2.17.so
mysqld_sa root mem REG , /usr/lib64/gconv/gconv-modules.cache
mysqld_sa root 0r CHR , 0t0 /dev/null
mysqld_sa root 1w CHR , 0t0 /dev/null
mysqld_sa root 2u CHR , 0t0 /dev/pts/
mysqld_sa root 255r REG , /home/softwares/mysql-5.7.-linux-glibc2.-x86_64/b
in/mysqld_safe[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof -c mysqld_safe       #查看指定程序打开的文件

[root@node101.yinzhengjie.org.cn ~]# lsof -u root | more               #查看指定用户打开的文件
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd root cwd DIR , /
systemd root rtd DIR , /
systemd root txt REG , /usr/lib/systemd/systemd
systemd root mem REG , /usr/lib64/libuuid.so.1.3.
systemd root mem REG , /usr/lib64/libblkid.so.1.1.
systemd root mem REG , /usr/lib64/libz.so.1.2.
systemd root mem REG , /usr/lib64/liblzma.so.5.2.
systemd root mem REG , /usr/lib64/libcap-ng.so.0.0.
systemd root mem REG , /usr/lib64/libattr.so.1.1.
systemd root mem REG , /usr/lib64/libdl-2.17.so
systemd root mem REG , /usr/lib64/libpcre.so.1.2.
systemd root mem REG , /usr/lib64/libc-2.17.so
systemd root mem REG , /usr/lib64/libpthread-2.17.so
systemd root mem REG , /usr/lib64/libgcc_s-4.8.-.so
.
systemd root mem REG , /usr/lib64/librt-2.17.so
systemd root mem REG , /usr/lib64/libmount.so.1.1.
systemd root mem REG , /usr/lib64/libkmod.so.2.2.
systemd root mem REG , /usr/lib64/libaudit.so.1.0.
systemd root mem REG , /usr/lib64/libpam.so.0.83.
systemd root mem REG , /usr/lib64/libcap.so.2.22
systemd root mem REG , /usr/lib64/libselinux.so.
systemd root mem REG , /usr/lib64/ld-2.17.so
systemd root 0u CHR , 0t0 /dev/null
......

[root@node101.yinzhengjie.org.cn ~]# lsof -u root | more       #查看指定用户打开的文件

[root@node101.yinzhengjie.org.cn ~]# lsof +D /var/log/         #查看指定目录下被打开的文件,递归列出目录下被打开的文件
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
auditd root 5w REG , /var/log/audit/audit.log
tuned root 3w REG , /var/log/tuned/tuned.log
rsyslogd root 6w REG , /var/log/messages
rsyslogd root 7w REG , /var/log/cron
rsyslogd root 8w REG , /var/log/secure
rsyslogd root 9w REG , /var/log/maillog
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof +D /var/log/         #查看指定目录下被打开的文件,递归列出目录下被打开的文件

[root@node101.yinzhengjie.org.cn ~]# lsof +d /var/log/           #查看指定目录下被打开的文件,列出目录下被打开的文件但不递归。
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsyslogd root 6w REG , /var/log/messages
rsyslogd root 7w REG , /var/log/cron
rsyslogd root 8w REG , /var/log/secure
rsyslogd root 9w REG , /var/log/maillog
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof +d /var/log/         #查看指定目录下被打开的文件,列出目录下被打开的文件但不递归。

5>.网络管理

-i:
通过参数-i查看网络连接的情况,包括连接的ip、端口,协议等以及一些服务的连接情况,例如:sshd等。也可以通过指定ip查看该ip的网络连接情况 -a:
查看存在的进程。 -p:
指定进程 -n:
no host names, -P:
no port names, -s:
指定协议状态通过多个参数可以清晰的查看网络连接情况、协议连接情况等
[root@node101.yinzhengjie.org.cn ~]# lsof -i -n             #查看所有网络连接,通过参数-i查看网络连接的情况,包括连接的ip、端口等以及一些服务的连接情况,例如:sshd等。
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dhclient root 6u IPv4 0t0 UDP *:bootpc
sshd root 3u IPv4 0t0 TCP *:ssh (LISTEN)
sshd root 4u IPv6 0t0 TCP *:ssh (LISTEN)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
mysqld mysql 32u IPv6 0t0 TCP *:mysql (LISTEN)
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof -i -n             #查看所有网络连接,通过参数-i查看网络连接的情况,包括连接的ip、端口等以及一些服务的连接情况,例如:sshd等。

[root@node101.yinzhengjie.org.cn ~]# lsof -i -n
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dhclient root 6u IPv4 0t0 UDP *:bootpc
sshd root 3u IPv4 0t0 TCP *:ssh (LISTEN)
sshd root 4u IPv6 0t0 TCP *:ssh (LISTEN)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
mysqld mysql 32u IPv6 0t0 TCP *:mysql (LISTEN)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -i@127.0.0.1
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -i@172.30.1.101 #通过参数-i查看网络连接的情况,也可以通过指定ip查看该ip的网络连接情况
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd root 3u IPv4 0t0 TCP node101.yinzhengjie.org.cn:ssh->gateway: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP node101.yinzhengjie.org.cn:ssh->gateway: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP node101.yinzhengjie.org.cn:ssh->gateway: (ESTABLISHED)
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof -i@172.30.1.101       #通过参数-i查看网络连接的情况,也可以通过指定ip查看该ip的网络连接情况

[root@node101.yinzhengjie.org.cn ~]# lsof -i: -n                #查看端口连接情况,通过参数-i:端口可以查看端口的占用情况,-i参数还有查看协议,ip的连接情况等
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd root 3u IPv4 0t0 TCP *:ssh (LISTEN)
sshd root 4u IPv6 0t0 TCP *:ssh (LISTEN)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof -i:22 -n           #查看端口连接情况,通过参数-i:端口可以查看端口的占用情况,-i参数还有查看协议,ip的连接情况等

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep sshd
root : ? :: /usr/sbin/sshd -D
root : ? :: sshd: root@pts/
root : ? :: sshd: root@pts/
root : ? :: sshd: root@pts/
root : pts/ :: grep --color=auto sshd
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -i -n -a -p #查看指定进程打开的网络连接,参数-i、-a、-p等,-i查看网络连接情况,-a查看存在的进程,-p指定进程
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd root 3u IPv4 0t0 TCP 172.30.1.101:ssh->172.30.1.254: (ESTABLISHED)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof -i -n -a -p 3901       #查看指定进程打开的网络连接,参数-i、-a、-p等,-i查看网络连接情况,-a查看存在的进程,-p指定进程

[root@node101.yinzhengjie.org.cn ~]# lsof -n -P -i TCP -s TCP:ESTABLISHED      #查看指定状态的网络连接
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd root 3u IPv4 0t0 TCP 172.30.1.101:->172.30.1.254: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:->172.30.1.254: (ESTABLISHED)
sshd root 3u IPv4 0t0 TCP 172.30.1.101:->172.30.1.254: (ESTABLISHED)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# lsof -n -P -i TCP -s TCP:ESTABLISHED   #查看指定状态的网络连接

6>.恢复删除文件

[root@node101.yinzhengjie.org.cn ~]# lsof |grep /var/log/messages
rsyslogd root 6w REG , /var/log/messages
in:imjour root 6w REG , /var/log/messages
rs:main root 6w REG , /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# rm -f /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof |grep /var/log/messages
rsyslogd root 6w REG , /var/log/messages (deleted)
in:imjour root 6w REG , /var/log/messages (deleted)
rs:main root 6w REG , /var/log/messages (deleted)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /proc//fd/
l-wx------ root root Nov : /proc//fd/ -> /var/log/messages (deleted)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /proc//fd/ > /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /var/log/messages
-rw-r--r-- root root Nov : /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#

二.进程管理工具-kill命令

1>.kill命令概述

  向进程发送控制信号,以实现对进程管理,每个信号对应一个数字,信号名称以SIG开头(可省略),不区分大小写。

2>.kill常用信号

[root@node101.yinzhengjie.org.cn ~]# kill -l                       #显示当前系统可用信号
) SIGHUP ) SIGINT ) SIGQUIT ) SIGILL ) SIGTRAP
) SIGABRT ) SIGBUS ) SIGFPE ) SIGKILL ) SIGUSR1
) SIGSEGV ) SIGUSR2 ) SIGPIPE ) SIGALRM ) SIGTERM
) SIGSTKFLT ) SIGCHLD ) SIGCONT ) SIGSTOP ) SIGTSTP
) SIGTTIN ) SIGTTOU ) SIGURG ) SIGXCPU ) SIGXFSZ
) SIGVTALRM ) SIGPROF ) SIGWINCH ) SIGIO ) SIGPWR
) SIGSYS ) SIGRTMIN ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# trap -l
) SIGHUP ) SIGINT ) SIGQUIT ) SIGILL ) SIGTRAP
) SIGABRT ) SIGBUS ) SIGFPE ) SIGKILL ) SIGUSR1
) SIGSEGV ) SIGUSR2 ) SIGPIPE ) SIGALRM ) SIGTERM
) SIGSTKFLT ) SIGCHLD ) SIGCONT ) SIGSTOP ) SIGTSTP
) SIGTTIN ) SIGTTOU ) SIGURG ) SIGXCPU ) SIGXFSZ
) SIGVTALRM ) SIGPROF ) SIGWINCH ) SIGIO ) SIGPWR
) SIGSYS ) SIGRTMIN ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# kill -l       #显示当前系统可用信号

) SIGHUP
无须关闭进程而让其重读配置文件
) SIGINT
中止正在运行的进程;相当于Ctrl+c
) SIGQUIT
相当于ctrl+\
) SIGKILL
强制杀死正在运行的进程
) SIGTERM
终止正在运行的进程
) SIGCONT
继续运行
) SIGSTOP
后台休眠
指定信号的方法 :
() 信号的数字标识,如:, ,
() 信号完整名称,如:SIGHUP
() 信号的简写名称,如:HUP

3>.按PID 

[root@node101.yinzhengjie.org.cn ~]# pstree -p | grep ssh
|-sshd()-+-sshd()---bash()
| |-sshd()---bash()-+-grep()
| `-sshd()---bash()
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# kill - #无须关闭进程而让其重读配置文件
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pstree -p | grep ssh
|-sshd()-+-sshd()---bash()
| |-sshd()---bash()-+-grep()
| `-sshd()---bash()
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# kill -1 3397       #无须关闭进程而让其重读配置文件

[root@node101.yinzhengjie.org.cn ~]# ps -a
PID TTY TIME CMD
pts/ :: mysqld_safe
pts/ :: mysqld
pts/ :: bc
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pidof bc [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# kill `pidof bc` #中止正在运行的进程;相当于Ctrl+c
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -a
PID TTY TIME CMD
pts/ :: mysqld_safe
pts/ :: mysqld
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# kill 2 `pidof bc`     #中止正在运行的进程;相当于Ctrl+c

[root@node101.yinzhengjie.org.cn ~]# pidof bc

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# kill `pidof bc` #让程序退出"QUIT",相当于"ctrl+\"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pidof bc
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# kill 3 `pidof bc`     #让程序退出"QUIT",相当于"ctrl+\"

[root@node101.yinzhengjie.org.cn ~]# pidof bc

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# kill `pidof bc` #相当于15信号,终止正在运行的进程
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pidof bc
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# kill `pidof bc`       #相当于15信号,终止正在运行的进程

[root@node101.yinzhengjie.org.cn ~]# ps
PID TTY TIME CMD
pts/ :: bash
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# kill - #强制杀死正在运行的进程 Connection closed by foreign host. Disconnected from remote host(node101.yinzhengjie.org.cn) at ::. Type `help' to learn how to use Xshell prompt.
[c:\~]$

[root@node101.yinzhengjie.org.cn ~]# kill -9 3630         #强制杀死正在运行的进程

[root@node101.yinzhengjie.org.cn ~]# kill -          #让进程进行错误检查
[root@node101.yinzhengjie.org.cn ~]# echo $? [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# kill -0 1           #让进程进行错误检查

4>.按名称杀死进程

[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN *: *:*
LISTEN *: *:*
LISTEN ::: :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# killall httpd
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN *: *:*
LISTEN ::: :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# killall httpd

5>.按模式杀死进程

pkill [options] pattern
-SIGNAL
-u uid: effective user,生效者
-U uid: real user,真正发起运行命令者
-t terminal: 与指定终端相关的进程
-l: 显示进程名(pgrep可用)
-a: 显示完整格式的进程名(pgrep可用)
-P pid: 显示指定进程的子进程
[root@node101.yinzhengjie.org.cn ~]# pkill -u yinzhengjie        #杀掉指定用户所启用的进程
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pkill - -u yinzhengjie      #杀掉指定用户的所启用的进程包括bash。
[root@node101.yinzhengjie.org.cn ~]#

三.作业管理

1>.Linux的作业控制

前台作业:
通过终端启动,且启动后一直占据终端 后台作业:
可通过终端启动,但启动后即转入后台运行(释放终端)

2>.让作业运行于后台

运行中的作业: 
  Ctrl+z
尚未启动的作业:
  COMMAND &
[root@node101.yinzhengjie.org.cn ~]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) () bytes of data.
bytes from 127.0.0.1: icmp_seq= ttl= time=0.009 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.017 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.018 ms
^Z
[]+ Stopped ping 127.0.0.1 #使用"ctrl +z"可以让进程停止
[root@node101.yinzhengjie.org.cn ~]#

[1]+ Stopped ping 127.0.0.1         #使用"ctrl +z"可以让进程停止

[root@node101.yinzhengjie.org.cn ~]# ping www.baidu.com &
[]
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# PING www.a.shifen.com (61.135.169.121) () bytes of data.
[root@node101.yinzhengjie.org.cn ~]#
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=7.29 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=10.0 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=10.0 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=5.91 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=7.96 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=38.4 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=19.5 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=6.74 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=7.12 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=16.8 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=24.9 ms
bytes from 61.135.169.121 (61.135.169.121): icmp_seq= ttl= time=79.2 ms

[root@node101.yinzhengjie.org.cn ~]# ping www.baidu.com &

3>.后台作业虽然被送往后台运行,但其依然与终端相关;退出终端,将关闭后台作业。如果希望送往后台后,剥离与终端的关系

nohup COMMAND &>/dev/null &
screen;COMMAND
[root@node101.yinzhengjie.org.cn ~]# yum -y install screen
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package screen.x86_64 :4.1.-0.25.20120314git3c2946.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved =======================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================
Installing:
screen x86_64 4.1.-0.25.20120314git3c2946.el7 base k Transaction Summary
=======================================================================================================================================
Install Package Total download size: k
Installed size: k
Downloading packages:
screen-4.1.-0.25.20120314git3c2946.el7.x86_64.rpm | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : screen-4.1.-0.25.20120314git3c2946.el7.x86_64 /
Verifying : screen-4.1.-0.25.20120314git3c2946.el7.x86_64 / Installed:
screen.x86_64 :4.1.-0.25.20120314git3c2946.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install screen    #使用screen的bash执行命令会自动放在后台作业。

[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nohup ping www.baidu.com &> /dev/null &
[]
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# jobs
[]+ Running nohup ping www.baidu.com &>/dev/null &
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# nohup ping www.baidu.com &> /dev/null &  #或者使用nohup命令配合"&"可以剥离终端的执行。

4>.查看当前终端所有作业

[root@node101.yinzhengjie.org.cn ~]# jobs
[]+ Stopped ping 127.0.0.1
[root@node101.yinzhengjie.org.cn ~]#

5>.作业控制

fg [[%]JOB_NUM]:
把指定的后台作业调回前台 bg [[%]JOB_NUM]:
让送往后台的作业在后台继续运行 kill [%JOB_NUM]:
终止指定的作业
[root@node101.yinzhengjie.org.cn ~]# jobs
[]+ Stopped ping 127.0.0.1
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bg #让送往后台的作业在后台继续运行
[]+ ping 127.0.0.1 &
[root@node101.yinzhengjie.org.cn ~]# bytes from 127.0.0.1: icmp_seq= ttl= time=0.013 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.025 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms

[root@node101.yinzhengjie.org.cn ~]# bg 1           #让送往后台的作业在后台继续运行

[root@node101.yinzhengjie.org.cn ~]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) () bytes of data.
bytes from 127.0.0.1: icmp_seq= ttl= time=0.022 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
^Z
[]+ Stopped ping 127.0.0.1
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# jobs
[]+ Stopped ping 127.0.0.1
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# fg #把指定的后台作业调回前台
ping 127.0.0.1
bytes from 127.0.0.1: icmp_seq= ttl= time=0.014 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.017 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.018 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.019 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.018 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.015 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.017 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.015 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.021 ms
^C
--- 127.0.0.1 ping statistics ---
packets transmitted, received, % packet loss, time 17359ms
rtt min/avg/max/mdev = 0.014/0.017/0.022/0.004 ms
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# fg 1           #把指定的后台作业调回前台

[root@node101.yinzhengjie.org.cn ~]# jobs
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) () bytes of data.
bytes from 127.0.0.1: icmp_seq= ttl= time=0.010 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.023 ms
^Z
[]+ Stopped ping 127.0.0.1
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# jobs
[]+ Stopped ping 127.0.0.1
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# killall - ping #给进程发送运行的信号,如果发送"-19"就让进程停止执行
bytes from 127.0.0.1: icmp_seq= ttl= time=0.017 ms
[root@node101.yinzhengjie.org.cn ~]# bytes from 127.0.0.1: icmp_seq= ttl= time=0.026 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.040 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.017 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.018 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.040 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.032 ms

[root@node101.yinzhengjie.org.cn ~]# killall -18 ping     #给进程发送运行的信号,如果发送"-19"就让进程停止执行

四.并行运行

同时运行多个进程,提高效率
方法1:编写脚本文件,在脚本中调用依次调用脚本并在后台执行。
    vi all.sh
    f1.sh&
    f2.sh&
    f3.sh&
方法2:
    (f1.sh&);(f2.sh&);(f3.sh&)

方法3:
    { f1.sh& f2.sh& f3.sh& }
[root@node101.yinzhengjie.org.cn ~]# { { ping -c  127.1;ping -c  127.2; }& { ping -c  127.3;ping -c  127.4; }& };
[]
[]
[root@node101.yinzhengjie.org.cn ~]# PING 127.3 (127.0.0.3) () bytes of data.
bytes from 127.0.0.3: icmp_seq= ttl= time=0.011 ms
PING 127.1 (127.0.0.1) () bytes of data.
bytes from 127.0.0.1: icmp_seq= ttl= time=0.026 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.045 ms
bytes from 127.0.0.3: icmp_seq= ttl= time=0.092 ms
bytes from 127.0.0.1: icmp_seq= ttl= time=0.015 ms
bytes from 127.0.0.3: icmp_seq= ttl= time=0.016 ms --- 127.1 ping statistics ---
--- 127.3 ping statistics ---
packets transmitted, received, % packet loss, time 2000ms
packets transmitted, received, % packet loss, time 2000ms
rtt min/avg/max/mdev = 0.011/0.039/0.092/0.037 ms
rtt min/avg/max/mdev = 0.015/0.028/0.045/0.013 ms
PING 127.4 (127.0.0.4) () bytes of data.
PING 127.2 (127.0.0.2) () bytes of data.
bytes from 127.0.0.2: icmp_seq= ttl= time=0.011 ms
bytes from 127.0.0.4: icmp_seq= ttl= time=0.011 ms
bytes from 127.0.0.2: icmp_seq= ttl= time=0.017 ms
bytes from 127.0.0.4: icmp_seq= ttl= time=0.031 ms
bytes from 127.0.0.4: icmp_seq= ttl= time=0.016 ms
bytes from 127.0.0.2: icmp_seq= ttl= time=0.016 ms --- 127.4 ping statistics ---
--- 127.2 ping statistics ---
packets transmitted, received, % packet loss, time 2000ms
packets transmitted, received, % packet loss, time 2000ms
rtt min/avg/max/mdev = 0.011/0.019/0.031/0.009 ms
rtt min/avg/max/mdev = 0.011/0.014/0.017/0.005 ms [] Done { ping -c 127.1; ping -c 127.2; }
[] Done { ping -c 127.3; ping -c 127.4; }
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# { { ping -c 3 127.1;ping -c 3 127.2; }& { ping -c 3 127.3;ping -c 3 127.4; }& };

最新文章

  1. oh-my-zsh主题
  2. Hadoop2.x的Eclipse插件编译与安装
  3. PPAS Migration Toolkit document
  4. Cheatsheet: 2014 10.01 ~ 10.30
  5. IOS 文件管理 2
  6. HAProxy+Keepalived+PXC负载均衡和高可用的PXC环境
  7. mac和windows系统下 eclipse svn 设置代理服务器
  8. 献身说法---修复bug时的一些小技巧
  9. ios开发常用RGB色值
  10. 决策树系列(三)——ID3
  11. Python列表之班荆道故
  12. Web前端-Vue.js必备框架(一)
  13. A1111. Online Map
  14. java监控指定路径下文件及文件夹变化
  15. topcoder srm 420 div1
  16. mycat 多个逻辑库加读写分离
  17. Invalid Host header 的解决方案
  18. sqlite元数据
  19. 【基于初学者的SSH】struts2 环境配置
  20. [Usaco2006 Nov]Roadblocks第二短路

热门文章

  1. 深入解读阿里云Redis开发规范
  2. Web自动化遇到shadowDOM节点操作(还没试)
  3. &#39;object &#39;&#39;/usr/local/lib/libdns.so&#39;&#39; from /etc/ld.so.preload cannot be preloaded: ignored.&#39;
  4. jQuery prop方法替代attr方法
  5. 推荐一款好用的免费FTP客户端Filezilla
  6. Vue 搭建项目
  7. Linux 就该这么学 CH02新手必须掌握的Linux命令
  8. 移动端实现裁剪图片生成base64图片(可缩放)
  9. 初识阿里开源的本地Java进程监控调试工具arthas(阿尔萨斯)
  10. Vue框架(四)——路由跳转、路由传参、cookies、axios、跨域问题、element-ui模块