Linux操作系统的进程管理和作业管理
Linux操作系统的进程管理和信号
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.lsof命令详解
1>.lsof概述
list open files查看当前系统文件的工具。在linux环境下,一切皆文件,用户通过文件不仅可以访问常规数据,还可以访问网络连接和硬件如传输控制协议 (TCP) 和用户数据报协议 (UDP)套接字等,系统在后台都为该应用程序分配了一个文件描述符
2>.命令参数
-a:
列出打开文件存在的进程 -c<进程名>:
列出指定进程所打开的文件 -g:
列出GID号进程详情 -d<文件号>:
列出占用该文件号的进程 +d<目录>:
列出目录下被打开的文件 +D<目录>:
递归列出目录下被打开的文件 -n<目录>:
列出使用NFS的文件 -i<条件>:
列出符合条件的进程(4、6、协议、:端口、 @ip ) -p<进程号>:
列出指定进程号所打开的文件 -u:
列出UID号进程详情 -h:
显示帮助信息 -v:
显示版本信息。 -n:
不反向解析网络名字
3>.进程管理
[root@node101.yinzhengjie.org.cn ~]# lsof /dev/pts/1 #查看由登陆用户启动而非系统启动的进程 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 3903 root 0u CHR 136,1 0t0 4 /dev/pts/1 bash 3903 root 1u CHR 136,1 0t0 4 /dev/pts/1 bash 3903 root 2u CHR 136,1 0t0 4 /dev/pts/1 bash 3903 root 255u CHR 136,1 0t0 4 /dev/pts/1 lsof 4947 root 0u CHR 136,1 0t0 4 /dev/pts/1 lsof 4947 root 1u CHR 136,1 0t0 4 /dev/pts/1 lsof 4947 root 2u CHR 136,1 0t0 4 /dev/pts/1 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps PID TTY TIME CMD 3903 pts/1 00:00:00 bash 4985 pts/1 00:00:00 mysqld_safe 5161 pts/1 00:00:00 ps [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# lsof -p 4985 #指定进程号,可以查看该进程打开的文件 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld_sa 4985 root cwd DIR 253,2 141 494 /home/softwares/mysql-5.7.25-linux-glibc2.12-x86_64 mysqld_sa 4985 root rtd DIR 253,0 224 64 / mysqld_sa 4985 root txt REG 253,0 964608 1770 /usr/bin/bash mysqld_sa 4985 root mem REG 253,0 106075056 67164275 /usr/lib/locale/locale-archive mysqld_sa 4985 root mem REG 253,0 2151672 33561967 /usr/lib64/libc-2.17.so mysqld_sa 4985 root mem REG 253,0 19288 33561974 /usr/lib64/libdl-2.17.so mysqld_sa 4985 root mem REG 253,0 174576 33562342 /usr/lib64/libtinfo.so.5.9 mysqld_sa 4985 root mem REG 253,0 163400 34070969 /usr/lib64/ld-2.17.so mysqld_sa 4985 root mem REG 253,0 26254 33562285 /usr/lib64/gconv/gconv-modules.cache mysqld_sa 4985 root 0r CHR 1,3 0t0 1028 /dev/null mysqld_sa 4985 root 1w CHR 1,3 0t0 1028 /dev/null mysqld_sa 4985 root 2u CHR 136,1 0t0 4 /dev/pts/1 mysqld_sa 4985 root 255r REG 253,2 28494 539340964 /home/softwares/mysql-5.7.25-linux-glibc2.12-x86_64/b in/mysqld_safe[root@node101.yinzhengjie.org.cn ~]#
4>.文件管理
-c: 查看指定用户打开的文件。 -d: 列出目录下被打开的文件 -D: 递归列出目录下被打开的文件。
[root@node101.yinzhengjie.org.cn ~]# ps PID TTY TIME CMD 3903 pts/1 00:00:00 bash 4985 pts/1 00:00:00 mysqld_safe 5165 pts/1 00:00:00 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 4985 root cwd DIR 253,2 141 494 /home/softwares/mysql-5.7.25-linux-glibc2.12-x86_64 mysqld_sa 4985 root rtd DIR 253,0 224 64 / mysqld_sa 4985 root txt REG 253,0 964608 1770 /usr/bin/bash mysqld_sa 4985 root mem REG 253,0 106075056 67164275 /usr/lib/locale/locale-archive mysqld_sa 4985 root mem REG 253,0 2151672 33561967 /usr/lib64/libc-2.17.so mysqld_sa 4985 root mem REG 253,0 19288 33561974 /usr/lib64/libdl-2.17.so mysqld_sa 4985 root mem REG 253,0 174576 33562342 /usr/lib64/libtinfo.so.5.9 mysqld_sa 4985 root mem REG 253,0 163400 34070969 /usr/lib64/ld-2.17.so mysqld_sa 4985 root mem REG 253,0 26254 33562285 /usr/lib64/gconv/gconv-modules.cache mysqld_sa 4985 root 0r CHR 1,3 0t0 1028 /dev/null mysqld_sa 4985 root 1w CHR 1,3 0t0 1028 /dev/null mysqld_sa 4985 root 2u CHR 136,1 0t0 4 /dev/pts/1 mysqld_sa 4985 root 255r REG 253,2 28494 539340964 /home/softwares/mysql-5.7.25-linux-glibc2.12-x86_64/b in/mysqld_safe[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -u root | more #查看指定用户打开的文件 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd 1 root cwd DIR 253,0 224 64 / systemd 1 root rtd DIR 253,0 224 64 / systemd 1 root txt REG 253,0 1616360 5112 /usr/lib/systemd/systemd systemd 1 root mem REG 253,0 20112 33562490 /usr/lib64/libuuid.so.1.3.0 systemd 1 root mem REG 253,0 265624 33562494 /usr/lib64/libblkid.so.1.1.0 systemd 1 root mem REG 253,0 90248 33562302 /usr/lib64/libz.so.1.2.7 systemd 1 root mem REG 253,0 157424 33562488 /usr/lib64/liblzma.so.5.2.2 systemd 1 root mem REG 253,0 23968 33562516 /usr/lib64/libcap-ng.so.0.0.0 systemd 1 root mem REG 253,0 19896 33562352 /usr/lib64/libattr.so.1.1.0 systemd 1 root mem REG 253,0 19288 33561974 /usr/lib64/libdl-2.17.so systemd 1 root mem REG 253,0 402384 33562365 /usr/lib64/libpcre.so.1.2.0 systemd 1 root mem REG 253,0 2151672 33561967 /usr/lib64/libc-2.17.so systemd 1 root mem REG 253,0 141968 33564379 /usr/lib64/libpthread-2.17.so systemd 1 root mem REG 253,0 88776 33554508 /usr/lib64/libgcc_s-4.8.5-20150702.so .1 systemd 1 root mem REG 253,0 43776 33564384 /usr/lib64/librt-2.17.so systemd 1 root mem REG 253,0 277824 33562588 /usr/lib64/libmount.so.1.1.0 systemd 1 root mem REG 253,0 91848 33564014 /usr/lib64/libkmod.so.2.2.10 systemd 1 root mem REG 253,0 127184 33562520 /usr/lib64/libaudit.so.1.0.0 systemd 1 root mem REG 253,0 61672 33562687 /usr/lib64/libpam.so.0.83.1 systemd 1 root mem REG 253,0 20032 33562356 /usr/lib64/libcap.so.2.22 systemd 1 root mem REG 253,0 155784 33562381 /usr/lib64/libselinux.so.1 systemd 1 root mem REG 253,0 163400 34070969 /usr/lib64/ld-2.17.so systemd 1 root 0u CHR 1,3 0t0 1028 /dev/null ......
[root@node101.yinzhengjie.org.cn ~]# lsof +D /var/log/ #查看指定目录下被打开的文件,递归列出目录下被打开的文件 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME auditd 3024 root 5w REG 253,0 980094 67734871 /var/log/audit/audit.log tuned 3399 root 3w REG 253,0 15517 100776432 /var/log/tuned/tuned.log rsyslogd 3402 root 6w REG 253,0 859023 100974924 /var/log/messages rsyslogd 3402 root 7w REG 253,0 11330 100663380 /var/log/cron rsyslogd 3402 root 8w REG 253,0 25751 100974925 /var/log/secure rsyslogd 3402 root 9w REG 253,0 2300 100974926 /var/log/maillog [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof +d /var/log/ #查看指定目录下被打开的文件,列出目录下被打开的文件但不递归。 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsyslogd 3402 root 6w REG 253,0 859023 100974924 /var/log/messages rsyslogd 3402 root 7w REG 253,0 11330 100663380 /var/log/cron rsyslogd 3402 root 8w REG 253,0 25751 100974925 /var/log/secure rsyslogd 3402 root 9w REG 253,0 2300 100974926 /var/log/maillog [root@node101.yinzhengjie.org.cn ~]#
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 3120 root 6u IPv4 23735 0t0 UDP *:bootpc sshd 3397 root 3u IPv4 25829 0t0 TCP *:ssh (LISTEN) sshd 3397 root 4u IPv6 25831 0t0 TCP *:ssh (LISTEN) sshd 3819 root 3u IPv4 26010 0t0 TCP 172.30.1.101:ssh->172.30.1.254:56376 (ESTABLISHED) sshd 3901 root 3u IPv4 25322 0t0 TCP 172.30.1.101:ssh->172.30.1.254:56598 (ESTABLISHED) sshd 4110 root 3u IPv4 25450 0t0 TCP 172.30.1.101:ssh->172.30.1.254:57022 (ESTABLISHED) mysqld 5131 mysql 32u IPv6 29416 0t0 TCP *:mysql (LISTEN) [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -i -n COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME dhclient 3120 root 6u IPv4 23735 0t0 UDP *:bootpc sshd 3397 root 3u IPv4 25829 0t0 TCP *:ssh (LISTEN) sshd 3397 root 4u IPv6 25831 0t0 TCP *:ssh (LISTEN) sshd 3819 root 3u IPv4 26010 0t0 TCP 172.30.1.101:ssh->172.30.1.254:56376 (ESTABLISHED) sshd 3901 root 3u IPv4 25322 0t0 TCP 172.30.1.101:ssh->172.30.1.254:56598 (ESTABLISHED) sshd 4110 root 3u IPv4 25450 0t0 TCP 172.30.1.101:ssh->172.30.1.254:57022 (ESTABLISHED) mysqld 5131 mysql 32u IPv6 29416 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 3819 root 3u IPv4 26010 0t0 TCP node101.yinzhengjie.org.cn:ssh->gateway:56376 (ESTABLISHED) sshd 3901 root 3u IPv4 25322 0t0 TCP node101.yinzhengjie.org.cn:ssh->gateway:56598 (ESTABLISHED) sshd 4110 root 3u IPv4 25450 0t0 TCP node101.yinzhengjie.org.cn:ssh->gateway:57022 (ESTABLISHED) [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -i:22 -n #查看端口连接情况,通过参数-i:端口可以查看端口的占用情况,-i参数还有查看协议,ip的连接情况等 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 3397 root 3u IPv4 25829 0t0 TCP *:ssh (LISTEN) sshd 3397 root 4u IPv6 25831 0t0 TCP *:ssh (LISTEN) sshd 3819 root 3u IPv4 26010 0t0 TCP 172.30.1.101:ssh->172.30.1.254:56376 (ESTABLISHED) sshd 3901 root 3u IPv4 25322 0t0 TCP 172.30.1.101:ssh->172.30.1.254:56598 (ESTABLISHED) sshd 4110 root 3u IPv4 25450 0t0 TCP 172.30.1.101:ssh->172.30.1.254:57022 (ESTABLISHED) [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep sshd root 3397 1 0 16:05 ? 00:00:00 /usr/sbin/sshd -D root 3819 3397 0 16:06 ? 00:00:00 sshd: root@pts/0 root 3901 3397 0 16:34 ? 00:00:00 sshd: root@pts/1 root 4110 3397 0 17:19 ? 00:00:00 sshd: root@pts/2 root 5253 3903 0 18:52 pts/1 00:00:00 grep --color=auto sshd [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# lsof -i -n -a -p 3901 #查看指定进程打开的网络连接,参数-i、-a、-p等,-i查看网络连接情况,-a查看存在的进程,-p指定进程 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 3901 root 3u IPv4 25322 0t0 TCP 172.30.1.101:ssh->172.30.1.254:56598 (ESTABLISHED) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# lsof -n -P -i TCP -s TCP:ESTABLISHED #查看指定状态的网络连接 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 3819 root 3u IPv4 26010 0t0 TCP 172.30.1.101:22->172.30.1.254:56376 (ESTABLISHED) sshd 3901 root 3u IPv4 25322 0t0 TCP 172.30.1.101:22->172.30.1.254:56598 (ESTABLISHED) sshd 4110 root 3u IPv4 25450 0t0 TCP 172.30.1.101:22->172.30.1.254:57022 (ESTABLISHED) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
6>.恢复删除文件
[root@node101.yinzhengjie.org.cn ~]# lsof |grep /var/log/messages rsyslogd 3402 root 6w REG 253,0 859089 100974924 /var/log/messages in:imjour 3402 3409 root 6w REG 253,0 859089 100974924 /var/log/messages rs:main 3402 3410 root 6w REG 253,0 859089 100974924 /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 3402 root 6w REG 253,0 859089 100974924 /var/log/messages (deleted) in:imjour 3402 3409 root 6w REG 253,0 859089 100974924 /var/log/messages (deleted) rs:main 3402 3410 root 6w REG 253,0 859089 100974924 /var/log/messages (deleted) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /proc/3402/fd/6 l-wx------ 1 root root 64 Nov 18 16:05 /proc/3402/fd/6 -> /var/log/messages (deleted) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /proc/3402/fd/6 > /var/log/messages [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /var/log/messages -rw-r--r-- 1 root root 859221 Nov 18 19:01 /var/log/messages [root@node101.yinzhengjie.org.cn ~]#
二.进程管理工具-kill命令
1>.kill命令概述
向进程发送控制信号,以实现对进程管理,每个信号对应一个数字,信号名称以SIG开头(可省略),不区分大小写。
2>.kill常用信号
[root@node101.yinzhengjie.org.cn ~]# kill -l #显示当前系统可用信号 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# trap -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX [root@node101.yinzhengjie.org.cn ~]#
1) SIGHUP 无须关闭进程而让其重读配置文件 2) SIGINT 中止正在运行的进程;相当于Ctrl+c 3) SIGQUIT 相当于ctrl+\ 9) SIGKILL 强制杀死正在运行的进程 15) SIGTERM 终止正在运行的进程 18) SIGCONT 继续运行 19) SIGSTOP 后台休眠 指定信号的方法 : (1) 信号的数字标识,如:1, 2, 9 (2) 信号完整名称,如:SIGHUP (3) 信号的简写名称,如:HUP
3>.按PID
[root@node101.yinzhengjie.org.cn ~]# pstree -p | grep ssh |-sshd(3397)-+-sshd(3819)---bash(3821) | |-sshd(3901)---bash(3903)-+-grep(5372) | `-sshd(4110)---bash(4112) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# kill -1 3397 #无须关闭进程而让其重读配置文件 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# pstree -p | grep ssh |-sshd(3397)-+-sshd(3819)---bash(3821) | |-sshd(3901)---bash(3903)-+-grep(5376) | `-sshd(4110)---bash(4112) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -a PID TTY TIME CMD 4985 pts/1 00:00:00 mysqld_safe 5131 pts/1 00:00:01 mysqld 5383 pts/2 00:00:00 bc 5396 pts/1 00:00:00 ps [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# pidof bc 5383 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# kill 2 `pidof bc` #中止正在运行的进程;相当于Ctrl+c [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ps -a PID TTY TIME CMD 4985 pts/1 00:00:00 mysqld_safe 5131 pts/1 00:00:01 mysqld 5399 pts/1 00:00:00 ps [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pidof bc 5406 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# kill 3 `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 ~]# pidof bc 5413 [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 ~]# ps PID TTY TIME CMD 3630 pts/0 00:00:00 bash 3914 pts/0 00:00:00 ps [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# kill -9 3630 #强制杀死正在运行的进程 Connection closed by foreign host. Disconnected from remote host(node101.yinzhengjie.org.cn) at 22:04:48. Type `help' to learn how to use Xshell prompt. [c:\~]$
[root@node101.yinzhengjie.org.cn ~]# kill -0 1 #让进程进行错误检查 [root@node101.yinzhengjie.org.cn ~]# echo $? 0 [root@node101.yinzhengjie.org.cn ~]#
4>.按名称杀死进程
[root@node101.yinzhengjie.org.cn ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 :::22 :::* [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 0 128 *:22 *:* LISTEN 0 128 :::22 :::* [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
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 -9 -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) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.009 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.017 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.018 ms ^Z [1]+ Stopped ping 127.0.0.1 #使用"ctrl +z"可以让进程停止 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ping www.baidu.com & [2] 4205 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data. [root@node101.yinzhengjie.org.cn ~]# 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=7 ttl=56 time=7.29 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=8 ttl=56 time=10.0 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=9 ttl=56 time=10.0 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=10 ttl=56 time=5.91 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=11 ttl=56 time=7.96 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=12 ttl=56 time=38.4 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=13 ttl=56 time=19.5 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=14 ttl=56 time=6.74 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=15 ttl=56 time=7.12 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=16 ttl=56 time=16.8 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=17 ttl=56 time=24.9 ms 64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=18 ttl=56 time=79.2 ms
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 0:4.1.0-0.25.20120314git3c2946.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================= Package Arch Version Repository Size ======================================================================================================================================= Installing: screen x86_64 4.1.0-0.25.20120314git3c2946.el7 base 552 k Transaction Summary ======================================================================================================================================= Install 1 Package Total download size: 552 k Installed size: 914 k Downloading packages: screen-4.1.0-0.25.20120314git3c2946.el7.x86_64.rpm | 552 kB 00:00:06 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : screen-4.1.0-0.25.20120314git3c2946.el7.x86_64 1/1 Verifying : screen-4.1.0-0.25.20120314git3c2946.el7.x86_64 1/1 Installed: screen.x86_64 0:4.1.0-0.25.20120314git3c2946.el7 Complete! [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nohup ping www.baidu.com &> /dev/null & [1] 4263 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# jobs [1]+ Running nohup ping www.baidu.com &>/dev/null & [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 0 [root@node101.yinzhengjie.org.cn ~]#
4>.查看当前终端所有作业
[root@node101.yinzhengjie.org.cn ~]# jobs [1]+ 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 [1]+ Stopped ping 127.0.0.1 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bg 1 #让送往后台的作业在后台继续运行 [1]+ ping 127.0.0.1 & [root@node101.yinzhengjie.org.cn ~]# 64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.013 ms 64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.025 ms 64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.016 ms
[root@node101.yinzhengjie.org.cn ~]# ping 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.022 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.016 ms ^Z [1]+ Stopped ping 127.0.0.1 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# jobs [1]+ Stopped ping 127.0.0.1 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# fg 1 #把指定的后台作业调回前台 ping 127.0.0.1 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.014 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.017 ms 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.018 ms 64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.019 ms 64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.018 ms 64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.015 ms 64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.017 ms 64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.015 ms 64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.021 ms ^C --- 127.0.0.1 ping statistics --- 11 packets transmitted, 11 received, 0% 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 ~]# 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) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.010 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.023 ms ^Z [1]+ Stopped ping 127.0.0.1 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# jobs [1]+ Stopped ping 127.0.0.1 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# killall -18 ping #给进程发送运行的信号,如果发送"-19"就让进程停止执行 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.017 ms [root@node101.yinzhengjie.org.cn ~]# 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.026 ms 64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.040 ms 64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.017 ms 64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.018 ms 64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.040 ms 64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.032 ms
四.并行运行
同时运行多个进程,提高效率 方法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 3 127.1;ping -c 3 127.2; }& { ping -c 3 127.3;ping -c 3 127.4; }& }; [3] 4310 [4] 4311 [root@node101.yinzhengjie.org.cn ~]# PING 127.3 (127.0.0.3) 56(84) bytes of data. 64 bytes from 127.0.0.3: icmp_seq=1 ttl=64 time=0.011 ms PING 127.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.026 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.045 ms 64 bytes from 127.0.0.3: icmp_seq=2 ttl=64 time=0.092 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.015 ms 64 bytes from 127.0.0.3: icmp_seq=3 ttl=64 time=0.016 ms --- 127.1 ping statistics --- --- 127.3 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms 3 packets transmitted, 3 received, 0% 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) 56(84) bytes of data. PING 127.2 (127.0.0.2) 56(84) bytes of data. 64 bytes from 127.0.0.2: icmp_seq=1 ttl=64 time=0.011 ms 64 bytes from 127.0.0.4: icmp_seq=1 ttl=64 time=0.011 ms 64 bytes from 127.0.0.2: icmp_seq=2 ttl=64 time=0.017 ms 64 bytes from 127.0.0.4: icmp_seq=2 ttl=64 time=0.031 ms 64 bytes from 127.0.0.4: icmp_seq=3 ttl=64 time=0.016 ms 64 bytes from 127.0.0.2: icmp_seq=3 ttl=64 time=0.016 ms --- 127.4 ping statistics --- --- 127.2 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms 3 packets transmitted, 3 received, 0% 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 [3] Done { ping -c 3 127.1; ping -c 3 127.2; } [4] Done { ping -c 3 127.3; ping -c 3 127.4; } [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/11885041.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。