Linux服务与进程管理
一、服务管理
- dnf -y install nginx 安装nginx服务器(端口号为80,使用tcp协议)
- CentOS6版本通过service命令进行服务管理
- CentOS7版本及以上通过systemctl命令进行服务管理
- systemctl status xxx —> 查看xxx服务的状态
- systemctl start xxx —> 启动xxx服务
- systemctl stop xxx —> 停止xxx服务
- systemctl restart xxx —> 重启xxx服务
- systemctl reload xxx —> 重新加载xxx服务(重载过程中并不关闭服务)
- systemctl enable xxx —> 设置xxx服务开机自启
- systemctl disable xxx —> 禁止xxx服务开机自启
- netstat 查询系统上的网络套接字连接情况(即查询端口情况)
- netstat -anlpt 查看所有tcp协议的端口
- netstat -anlpu 查看所有udp协议的端口
- -a 查询所有端口
- -n 禁止使用域名解析功能,提高查询速度
- -l 仅显示监听的端口
- -p 显示与端口有关的程序名和进程的pid
- -t 显示与tcp协议有关的端口
- -u 显示与udp协议有关的端口
# 查看当前是否安装nginx[root@myserver ~]# rpm -qa | grep nginx# 安装nginx[root@myserver ~]# dnf -y install nginxLast metadata expiration check: 0:15:42 ago on Tue 28 Jul 2020 08:20:47 AM CST.Dependencies resolved.======================================================================================= Package Arch Version Repo Size...... nginx-mod-mail-1:1.14.1-9.module_el8.0.0+184+e34fea82.x86_64 nginx-mod-stream-1:1.14.1-9.module_el8.0.0+184+e34fea82.x86_64 Complete!# 查看nginx是否安装成功[root@myserver ~]# rpm -qa | grep nginxnginx-mod-http-image-filter-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64nginx-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64nginx-mod-http-xslt-filter-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64nginx-mod-stream-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64nginx-filesystem-1.14.1-9.module_el8.0.0+184+e34fea82.noarchnginx-mod-mail-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64nginx-all-modules-1.14.1-9.module_el8.0.0+184+e34fea82.noarchnginx-mod-http-perl-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64# 查看nginx服务状态,按q键退出查看 --> Active: inactive (dead) --> 未启动[root@myserver ~]# systemctl status nginx● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: dis> Active: inactive (dead)Jul 28 08:15:38 myserver systemd[1]: nginx.service: Unit cannot be reloaded because it>Jul 28 08:36:32 myserver systemd[1]: nginx.service: Unit cannot be reloaded because it># 开启nginx服务[root@myserver ~]# systemctl start nginx.service # 查看是否成功启动 --> Active: active (running) --> 已启动[root@myserver ~]# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: dis> Active: active (running) since Tue 2020-07-28 08:56:39 CST; 12s ago Process: 37270 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 37268 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)......# 停止nginx服务[root@myserver ~]# systemctl stop nginx.service # 查看是否成功停止 --> Active: inactive (dead) --> 已停止[root@myserver ~]# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: dis> Active: inactive (dead)......# 重启nginx[root@myserver ~]# systemctl restart nginx.service # 查看是否重启成功[root@myserver ~]# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: dis> Active: active (running) since Tue 2020-07-28 08:59:31 CST; 11s ago Process: 37393 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)......# 重载nginx[root@myserver ~]# systemctl reload nginx.service [root@myserver ~]# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: dis> Active: active (running) since Tue 2020-07-28 08:59:31 CST; 1min 13s ago Process: 37444 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCESS)......# 将nginx设为开机自启 --> 在/etc/systemd/system/multi-user.target.wants/文件夹下创建一个文件nginx.service(映射的是/etc/systemd/system/multi-user.target.wants/下的nginx.service文件,其是nginx服务的启动文件)[root@myserver ~]# systemctl enable nginx.service Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.# 关闭nginx开机自启 --> 将/etc/systemd/system/multi-user.target.wants/目录下的nginx.service文件删除[root@myserver ~]# systemctl disable nginx.service Removed /etc/systemd/system/multi-user.target.wants/nginx.service.# 检测nginx服务是否在运行[root@myserver ~]# netstat -anlpt | grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 37394/nginx: master tcp6 0 0 :::80 :::* LISTEN 37394/nginx: master
二、进程管理
- top 查看系统所有进程
- ps 查看进程的状态
- a 显示现行终端机下的所有程序,包括其他用户的程序
- u 以用户为主的格式来显示程序状况
- x 显示所有程序,不以终端机来区分
- kill 停止进程
- kill -9 pid —> 强制杀死进程号为pid的进程
- kill -15 pid —> 进程号为pid的进程执行完当前任务就停止
- killall xxx —> 结束xxx服务的所有进程
# 查看系统所有进程[root@myserver ~]# toptop - 14:45:31 up 3:37, 1 user, load average: 0.07, 0.06, 0.02Tasks: 334 total, 1 running, 332 sleeping, 0 stopped, 1 zombie%Cpu(s): 0.2 us, 0.6 sy, 0.0 ni, 98.9 id, 0.0 wa, 0.2 hi, 0.0 si, 0.0 stMiB Mem : 3752.0 total, 1032.9 free, 1588.6 used, 1130.5 buff/cacheMiB Swap: 4044.0 total, 4044.0 free, 0.0 used. 1872.8 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6874 root 20 0 4782568 417876 109132 S 18.8 10.9 4:11.82 gnome-shell 7852 root 20 0 532420 42732 31144 S 2.0 1.1 0:17.06 gnome-terminal- ......# 列出所有含有nginx字样的进程[root@myserver ~]# ps aux | grep nginx# 运行用户 pid 所占用各种资源----------------------------------------------------------root 40463 7.6 0.0 117748 2168 ? Ss 14:51 0:00 nginx: master process /usr/sbin/nginxnginx 40464 33.2 0.2 149292 7936 ? S 14:51 0:02 nginx: worker processnginx 40465 6.0 0.2 149292 7936 ? S 14:51 0:00 nginx: worker processnginx 40466 46.8 0.2 149292 7928 ? S 14:51 0:03 nginx: worker processnginx 40467 38.2 0.2 149292 7936 ? S 14:51 0:03 nginx: worker processnginx 40468 15.3 0.2 149292 7916 ? S 14:51 0:01 nginx: worker processnginx 40469 44.1 0.2 149292 7940 ? S 14:51 0:03 nginx: worker processnginx 40470 32.5 0.2 149292 7940 ? S 14:51 0:02 nginx: worker processnginx 40471 33.0 0.2 149292 7940 ? S 14:51 0:02 nginx: worker processroot 40479 0.0 0.0 12108 980 pts/0 R+ 14:51 0:00 grep --color=auto nginx# 杀死40464号进程[root@myserver ~]# kill -9 40464# 检测是否杀死40464号进程[root@myserver ~]# ps aux | grep nginxroot 40463 0.0 0.0 117748 3500 ? Ss 14:51 0:00 nginx: master process /usr/sbin/nginxnginx 40465 0.0 0.2 149292 7936 ? S 14:51 0:00 nginx: worker processnginx 40466 0.4 0.2 149292 7928 ? S 14:51 0:03 nginx: worker processnginx 40467 0.3 0.2 149292 7936 ? S 14:51 0:03 nginx: worker processnginx 40468 0.1 0.2 149292 7916 ? S 14:51 0:01 nginx: worker processnginx 40469 0.4 0.2 149292 7940 ? S 14:51 0:03 nginx: worker processnginx 40470 0.2 0.2 149292 7940 ? S 14:51 0:02 nginx: worker processnginx 40471 0.3 0.2 149292 7940 ? S 14:51 0:02 nginx: worker processnginx 40686 0.0 0.2 149292 7940 ? S 15:05 0:00 nginx: worker processroot 40689 0.0 0.0 12108 988 pts/0 S+ 15:05 0:00 grep --color=auto nginx# 杀死40463号进程(nginx服务主进程)[root@myserver ~]# kill -15 40463# 检测是否杀死40463号进程(主进程被干掉,和nginx服务有关的运行进程全部被干掉)[root@myserver ~]# ps aux | grep nginxroot 40737 0.0 0.0 12108 1100 pts/0 S+ 15:08 0:00 grep --color=auto nginx# 查看当前nginx服务的状态,是关闭的[root@myserver ~]# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: inactive (dead)......# 打开nginx服务[root@myserver ~]# systemctl start nginx.service # 检测nginx服务是否被打开[root@myserver ~]# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2020-07-28 15:10:49 CST; 9s ago......# 查看nginx服务有关进程[root@myserver ~]# ps aux | grep nginxroot 41077 0.0 0.0 117748 2160 ? Ss 15:32 0:00 nginx: master process /usr/sbin/nginxnginx 41078 0.0 0.2 149292 7932 ? S 15:32 0:00 nginx: worker processnginx 41079 0.0 0.2 149292 7948 ? S 15:32 0:00 nginx: worker processnginx 41080 0.0 0.2 149292 7956 ? S 15:32 0:00 nginx: worker processnginx 41081 0.0 0.2 149292 7924 ? S 15:32 0:00 nginx: worker processnginx 41082 0.0 0.2 149292 7960 ? S 15:32 0:00 nginx: worker processnginx 41083 0.0 0.2 149292 7960 ? S 15:32 0:00 nginx: worker processnginx 41084 0.0 0.2 149292 7960 ? S 15:32 0:00 nginx: worker processnginx 41085 0.0 0.2 149292 7960 ? S 15:32 0:00 nginx: worker processroot 41115 0.0 0.0 12108 1060 pts/0 S+ 15:33 0:00 grep --color=auto nginx# 杀死所有和nginx服务有关的进程[root@myserver ~]# killall nginx# 查看是否杀死成功nginx服务[root@myserver ~]# ps aux | grep nginxroot 41149 0.0 0.0 12108 1032 pts/0 S+ 15:33 0:00 grep --color=auto nginx# 查看杀死nginx服务有关进程后nginx服务状态[root@myserver ~]# systemctl status nginx.service ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: inactive (dead)......