01.进程管理
1.进程和程序的区别
2.ps -axuf 命令 重点 查看进程
3.面试题:查看文件被哪个进程所调用lsof messages
4.kill命令
1.kill命令
语法结构:kill 编号 进程id
编号:
1 #重新加载 不会改变父进程的PID号 用户不会和服务器断开连接
9 #强制杀死进程 直接杀死进程 用户和我们直接断开连接 内存东西丢失(直接拔电源)
15 #平滑杀死进程 内存的内容会写入到磁盘(类似笔记本点击按钮关机) 默认就是15
kill pid #平滑杀死进程 systemctl stop nginx
kill -1 pid #重新加载配置 systemctl reload nginx
kill -9 pid #强制杀死进程
killall nginx #杀死所有nginx的进程
pkill nginx #杀死所有nginx的进程
-----------------
#安装一个web服务nginx 可以联网
[root@oldboy ~]# yum -y install nginx
#启动服务
[root@oldboy ~]# systemctl start nginx
#查看端口号
[root@oldboy ~]# netstat -tnulp|grep -v 'sshd'
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12595/nginx: master
#将游戏代码拷贝到站点目录下
[root@oldboy html]# cd /usr/share/nginx/html/
[root@oldboy html]# rm -rf *
[root@oldboy html]# pwd
/usr/share/nginx/html
[root@oldboy html]# rz
[root@oldboy html]# ll
total 7708
-rw-r--r-- 1 root root 7890373 Dec 6 2024 game.zip
#解压游戏压缩包
[root@oldboy html]# unzip game.zip
#kill -1 和更新方式类似 游戏的在线跟新
[root@oldboy html]# ps axu|grep nginx
root 12595 0.0 0.1 39168 2360 ? Ss 11:52 0:00 nginx: master process /usr/sbin/nginx
nginx 12596 0.0 0.3 50340 6428 ? S 11:52 0:00 nginx: worker process
nginx 12597 0.0 0.3 50340 6416 ? S 11:52 0:00 nginx: worker process
nginx 12598 0.0 0.3 50340 6484 ? S 11:52 0:00 nginx: worker process
nginx 12599 0.0 0.3 50340 6484 ? S 11:52 0:00 nginx: worker process
root 12675 0.0 0.0 213140 880 pts/0 S+ 12:02 0:00 grep nginx
[root@oldboy html]# kill -1 12596
#停机维护
[root@oldboy html]# systemctl stop nginx
案例:killall pkill
杀死进程 语法结构 killall 进程名称
[root@oldboy html]# yum -y install psmisc
[root@oldboy html]# systemctl restart nginx
[root@oldboy html]# ps axu|grep nginx
root 12999 0.0 0.1 39168 2368 ? Ss 12:11 0:00 nginx: master process /usr/sbin/nginx
nginx 13000 0.0 0.3 50340 6432 ? S 12:11 0:00 nginx: worker process
nginx 13001 0.0 0.3 50340 6428 ? S 12:11 0:00 nginx: worker process
nginx 13002 0.0 0.3 50340 6432 ? S 12:11 0:00 nginx: worker process
nginx 13003 0.0 0.3 50340 6420 ? S 12:11 0:00 nginx: worker process
root 13009 0.0 0.0 213140 876 pts/0 S+ 12:11 0:00 grep nginx
#使用killall
[root@oldboy ~]# killall nginx
[root@oldboy ~]# ps axu|grep nginx
root 13023 0.0 0.0 213140 820 pts/0 S+ 12:12 0:00 grep nginx
---------------------------
#使用pkill
[root@oldboy ~]# pkill nginx
[root@oldboy ~]# ps axu|grep nginx
root 13052 0.0 0.0 213140 816 pts/0 S+ 12:13 0:00 grep nginx
02.面试题
必会:如何将一个进程放到后台持续运行
1.screen
2.nohup
1.screen
第一步:安装命令
[root@oldboy ~]# yum -y install screen
第二步:进入到screen
[root@oldboy ~]# screen -S wget_test
在子进程中执行命令
wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/rpm/jenkins-2.557-1.noarch.rpm
第三步:退出wget_test
ctrl+a+d 平滑退出空间戒指,完全退出不用了使用exit
第四步:进入wget_test空间里面
使用-list查看所有的screen
[root@oldboy ~]# screen -list
There is a screen on:
13153.wget_test (Detached)
1 Socket in /run/screen/S-root.
#使用-r加id号或者名字
[root@oldboy ~]# screen -r 13153
2.nohup
[root@oldboy ~]# nohup sleep 600 &
企业使用nohup
python java语音
#在命令行运行Python程序
nohup python3.0 test.py --mysql-ip= --mysql-port --redis=ip &
nohup python3.0 test.py --mysql-ip= --mysql-port --redis=ip &
nohup python3.0 test.py --mysql-ip= --mysql-port --redis=ip &
nohup python3.0 test.py --mysql-ip= --mysql-port --redis=ip &
nohup python3.0 test.py --mysql-ip= --mysql-port --redis=ip &
nohup python3.0 test.py --mysql-ip= --mysql-port --redis=ip &
nohup python3.0 test.py --mysql-ip= --mysql-port --redis=ip &
nohup python3.0 test.py --mysql-ip= --mysql-port --redis=ip &
sh test.sh
#注意使用nohup运行起来的后会在当前产生临时文件nohup.out 一定多关注,产生很多临时日志。
nohup java -jar test.jar --redis-ip --mysql-ip --mysql-password=xx &
#精准批量杀
[root@oldboy ~]# cat stop.sh
ps axu|grep sleep|grep -v grep|awk '{print $2}'|xargs kill -9
03.重点
ps -auxf
top
面试官问:如何将程序放在后台运行
screen -S get
退出:ctrl+a+d
nohup 命令 &
正文完