day23
1. 进程的概述
程序运行,这个程序运行的过程就是进程?
系统会给进程分配了 内存空间
分配了安全属性 运行的身份和权限
分配了系统的资源 文件描述符 进程描述符 网络端口 ID号 PID PPID
系统会记录进程的运行中的状态 STATAE
进程和程序有什么区别
程序是指令和数据的有序集合 静态的概念 永久存在的
进程是程序在主机上面运行的一个过程 动态的概念 会随着程序的创建,运行,终止,消除而终止 临时
进程是有生命周期
进程的运行过程:
用户运行了一个程序,系统会给进程分配任务,进程会通过fork一个子进程,子进程会继承父进程的衣钵,子进程处理具体的任务,父进程就会进入等待状态,子进程任务结束,回来向父进程交任务,子进程正常退出
子进程在执行任务时,父进程因为意外原因退出了,那么子进程就会变成无人管理,就是僵尸进程
每个进程都有自己的代号 ID号 子进程 PID 父进程 PPID
2. 进程的状态
ps
a
u
x
aux
-e
-f
-ef
[ root@qls ~]
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 125420 2112 ? Ss Aug03 0 :03 /usr/lib/systemd/systemd --switched-root --system --des
root 2 0.0 0.0 0 0 ? S Aug03 0 :00 [ kthreadd]
root 3 0.0 0.0 0 0 ? S Aug03 0 :00 [ ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Aug03 0 :00 [ kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Aug03 0 :00 [ migration/0]
root 8 0.0 0.0 0 0 ? S Aug03 0 :00 [ rcu_bh]
root 9 0.0 0.0 0 0 ? R Aug03 0 :07 [ rcu_sched]
root 10 0.0 0.0 0 0 ? S< Aug03 0 :00 [ lru-add-drain]
root 11 0.0 0.0 0 0 ? S Aug03 0 :00 [ watchdog/0]
USER
PID
PPID
%CPU
%MEM
VSZ
RSS
TTY
?
tty1
pts/0
STAT
S
R
D
T
Z
s
<
N
l
+
START
TIME
COMMAND
[ ]
无[ ]
[ root@qls ~]
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Aug03 ? 00:00:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0 0 Aug03 ? 00:00:00 [ kthreadd]
root 3 2 0 Aug03 ? 00:00:00 [ ksoftirqd/0]
root 5 2 0 Aug03 ? 00:00:00 [ kworker/0:0H]
root 7 2 0 Aug03 ? 00:00:00 [ migration/0]
root 8 2 0 Aug03 ? 00:00:00 [ rcu_bh]
root 9 2 0 Aug03 ? 00:00:07 [ rcu_sched]
root 10 2 0 Aug03 ? 00:00:00 [ lru-add-drain]
案例1
[ root@qls ~]
[ root@qls ~]
root 12592 0.2 0.2 149168 4996 pts/0 S+ 09:04 0 :00 vim test.log
[ root@qls ~]
root 12592 0.1 0.2 149168 4996 pts/0 S+ 09:04 0 :00 vim test.log
[ root@qls ~]
[ 1 ] + Stopped vim test.log
[ root@qls ~]
root 12592 0.0 0.2 149168 4996 pts/0 T 09:04 0 :00 vim test.log
案例二
[ root@qls ~]
tar: Removing leading ` /' from member names
tar: Removing leading ` /' from hard link targets
[ root@qls ~]
root 12617 7.0 0.0 123408 1352 pts/0 R+ 09:08 0 :00 tar czf etc.tar.gz /usr /etc/ /var
[ root@qls ~]
root 12617 6.0 0.0 123408 1352 pts/0 D+ 09:08 0 :00 tar czf etc.tar.gz /usr /etc/ /var
[ root@qls ~]
root 12617 6.2 0.0 123540 1352 pts/0 R+ 09:08 0 :00 tar czf etc.tar.gz /usr /etc/ /var
[ root@qls ~]
root 12617 5.8 0.0 123540 1352 pts/0 D+ 09:08 0 :00 tar czf etc.tar.gz /usr /etc/ /var
[ root@qls ~]
root 12617 5.5 0.0 123540 1352 pts/0 D+ 09:08 0 :00 tar czf etc.tar.gz /usr /etc/ /var
[ root@qls ~]
root 12617 6.5 0.0 123540 1352 pts/0 S+ 09:08 0 :00 tar czf etc.tar.gz /usr /etc/ /var
[ root@qls ~]
root 12617 5.8 0.0 123540 1352 pts/0 S+ 09:08 0 :00 tar czf etc.tar.gz /usr /etc/ /var
[ root@qls ~]
root 12617 6.1 0.0 123540 1352 pts/0 R+ 09:08 0 :00 tar czf etc.tar.gz /usr /etc/ /var
[ root@qls ~]
root 7193 0.0 5.4 225028 111152 pts/0 Ss Aug03 0 :04 -bash
root 12575 0.0 0.1 115572 2120 pts/1 Ss+ 09:04 0 :00 -bash
root 12641 0.0 5.4 225028 110280 pts/0 R+ 09:09 0 :00 -bash
案例三
[ root@qls ~]
int main( int argc, char *argv[ ] )
{
pid_t pid;
pid = fork( ) ;
if ( pid == 0 ) {
int iPid = ( int) getpid( ) ;
fprintf( stderr,"I am child,%d\n " ,iPid) ;
sleep( 1 ) ;
fprintf( stderr, "Child exits\n " ) ;
return EXIT_SUCCESS;
}
int iPid = ( int) getpid( ) ;
fprintf( stderr,"I am parent,%d\n " ,iPid) ;
fprintf( stderr, "sleep....\n " ) ;
sleep( 600 ) ;
fprintf( stderr, "parent exits\n " ) ;
return EXIT_SUCCESS;
}
[ root@qls ~]
[ root@qls ~]
total 1218672
-rwxr-xr-x 1 root root 8696 Aug 4 09:11 a.out
-rw-r--r-- 1 root root 199327744 Aug 4 09:09 etc.tar.gz
-rw------- 1 root root 1048576000 Aug 3 11 :22 swap.txt
-rw-r--r-- 1 root root 547 Aug 4 09:10 test.c
[ root@qls ~]
I am parent,12655
sleep.. ..
I am child,12656
Child exits
^C
[ root@qls ~]
root 12655 0.0 0.0 4208 352 pts/0 S+ 09:11 0 :00 ./a.out
root 12656 0.0 0.0 0 0 pts/0 Z+ 09:11 0 :00 [ a.out] < defunct>
动态显示进程状态
top htop
[ root@qls ~]
[ root@qls ~]
top - 09:33:58 up 23 :31, 2 users, load average: 0.24 , 0.10 , 0.07
Tasks: 113 total, 1 running, 112 sleeping, 0 stopped, 0 zombie
%Cpu( s) : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2028088 total, 155648 free, 363844 used, 1508596 buff/cache
KiB Swap: 2097148 total, 2069756 free, 27392 used. 974036 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
top - 09:33:58 up 23 :31, 2 users, load average: 0.24 , 0.10 , 0.07
09:33:58
up 23 :31
2 users
load average: 0.24 , 0.10 , 0.07
Tasks: 113 total, 1 running, 112 sleeping, 0 stopped, 0 zombie
任务:
113 total,
1 running
112 sleeping
0 stopped
0 zombie
%Cpu( s) : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
系统各个状态所占用CPU的时间百分比
0.0 us
0.0 sy
0.0 ni
100.0 id
0.0 wa
0.0 hi
0.0 si
0.0 st
KiB Mem : 2028088 total, 155648 free, 363844 used, 1508596 buff/cache
物理内存 总的 剩余的 已使用的 缓存和缓冲中的内存
KiB Swap: 2097148 total, 2069756 free, 27392 used. 974036 avail Mem
虚拟内存
PID
USER
PR
NI
VIRT
RES
SHR
S
%CPU
%MEM
TIME+
COMMAND
top命令的内部指令
h
q
P
M
1
top选项:
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
total used free shared buff/cache available
Mem: 1980 265 907 12 807 1114
Swap: 2047 29 2018
[ root@qls ~]
[ root@qls ~]
3. 终止进程
kill
[ root@qls ~]
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
1
2
9
15
18
20
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
root 13063 0.0 0.1 120796 2096 ? Ss 10 :36 0 :00 nginx: master process /usr/sbin/nginx
nginx 13064 0.2 0.1 121180 3128 ? S 10 :36 0 :00 nginx: worker process
[ root@qls ~]
[ root@qls ~]
root 13063 0.0 0.2 121452 5248 ? Ss 10 :36 0 :00 nginx: master process /usr/sbin/nginx
nginx 13070 0.0 0.1 121876 3836 ? S 10 :37 0 :00 nginx: worker process
[ root@qls ~]
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 13063 /nginx: master
[ root@qls ~]
[ root@qls ~]
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:81 0.0 .0.0:* LISTEN 13063 /nginx: master
[ root@qls ~]
[ root@qls ~]
root 13115 0.0 0.1 161988 2212 pts/1 S+ 10 :41 0 :00 top
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
root 13144 0.0 0.1 161984 2212 pts/1 S+ 10 :44 0 :00 top
[ root@qls ~]
kill
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
nginx: no process found
[ root@qls ~]
root 13264 0.0 0.2 149168 4972 pts/0 S+ 10 :53 0 :00 vim 123 .txt
root 13265 0.1 0.2 149168 4972 pts/1 S+ 10 :53 0 :00 vim 123 .log
[ root@qls ~]
pkill
[ root@qls ~]
root 13278 0.0 0.1 161988 2216 pts/0 S+ 10 :56 0 :00 top
root 13279 0.1 0.1 122396 2112 pts/1 S+ 10 :56 0 :00 htop
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
root 6935 0.0 0.0 112756 1276 ? Ss Aug03 0 :00 /usr/sbin/sshd -D
root 7191 0.0 0.0 161364 1616 ? Ss Aug03 0 :00 sshd: root@pts/0
root 7193 0.0 5.4 225028 111168 pts/0 Ss+ Aug03 0 :05 -bash
root 12573 0.0 0.2 161364 5936 ? Ss 09:04 0 :00 sshd: root@pts/1
root 12575 0.0 0.1 115572 2196 pts/1 Ss 09:04 0 :00 -bash
root 13215 0.0 0.2 161364 6056 ? Ss 10 :50 0 :00 sshd: root@pts/2
root 13217 0.0 0.1 115572 2184 pts/2 Ss 10 :50 0 :00 -bash
root 13296 0.0 0.0 113176 1196 pts/2 S+ 10 :59 0 :00 sh 123 .txt
root 13299 0.0 0.0 113176 1216 ? Ss 10 :59 0 :00 /bin/sh -c /usr/sbin/ntpdate ntp.aliyun.com &> /dev/null
root 13302 0.0 0.0 112708 976 pts/1 R+ 10 :59 0 :00 grep --color= auto sh
[ root@qls ~]
[ root@qls ~]
systemd─┬─NetworkManager───2*[ { NetworkManager} ]
├─VGAuthService
├─auditd───{ auditd}
├─crond───crond───sh───ntpdate
├─dbus-daemon
├─firewalld───{ firewalld}
├─login───bash
├─master─┬─pickup
│ └─qmgr
├─ping
├─polkitd───6*[ { polkitd} ]
├─rsyslogd───2*[ { rsyslogd} ]
├─sshd───sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─tuned───4*[ { tuned} ]
├─vmtoolsd
└─vsftpd
4. 管理进程的后台
[ root@qls ~]
tar: Removing leading ` /' from member names
tar: Removing leading ` /' from hard link targets
Connection closed.
Disconnected from remote host(虚拟机-10.0.0.100) at 11:13:33.
Type ` help ' to learn how to use Xshell prompt.
[C:\~]$
Connecting to 10.0.0.100:22...
Connection established.
To escape to local shell, press ' Ctrl+Alt+] '.
Last login: Tue Aug 4 11 :12:44 2020 from 10.0 .0.1
[ root@qls ~]
total 35408
-rw-r--r-- 1 root root 36257792 Aug 4 11 :13 etc.tar.gz
[ root@qls ~]
total 35M
-rw-r--r-- 1 root root 35M Aug 4 11 :13 etc.tar.gz
[ root@qls ~]
tar: Removing leading ` /' from member names
tar: Removing leading `/' from hard link targets
^C
[ root@qls ~]
total 128M
-rw-r--r-- 1 root root 104M Aug 4 11 :14 etc.tar.gz
[ root@qls ~]
1 . nohup &
[ root@qls ~]
[ 1 ] 13517
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ 1 ] 13592
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
[ root@qls ~]
root 13592 0.0 0.0 149968 1988 pts/1 S 11 :17 0 :00 ping www.baidu.com
root 13594 0.0 0.0 112708 976 pts/1 R+ 11 :17 0 :00 grep --color= auto 13592
[ root@qls ~]
[ 1 ] + Running nohup ping www.baidu.com &
[ root@qls ~]
[ 2 ] + Stopped vim test.log
[ root@qls ~]
[ 1 ] - Running nohup ping www.baidu.com &
[ 2 ] + Stopped vim test.log
[ root@qls ~]
[ root@qls ~]
[ 2 ] + ping www.baidu.com &
[ root@qls ~]
PING www.a.shifen.com ( 112.80 .248.75) 56 ( 84 ) bytes of data.
64 bytes from 112.80 .248.75 ( 112.80 .248.75) : icmp_seq = 1 ttl = 128 time = 16.5 ms
64 bytes from 112.80 .248.75 ( 112.80 .248.75) : icmp_seq = 2 ttl = 128 time = 13.7 ms
64 bytes from 112.80 .248.75 ( 112.80 .248.75) : icmp_seq = 3 ttl = 128 time = 13.8 ms
^Z
[ 1 ] + Stopped ping www.baidu.com
[ root@qls ~]
[ 1 ] + Stopped ping www.baidu.com
[ root@qls ~]
ping www.baidu.com
64 bytes from 112.80 .248.75 ( 112.80 .248.75) : icmp_seq = 4 ttl = 128 time = 14.2 ms
64 bytes from 112.80 .248.75 ( 112.80 .248.75) : icmp_seq = 5 ttl = 128 time = 14.5 ms
64 bytes from 112.80 .248.75 ( 112.80 .248.75) : icmp_seq = 6 ttl = 128 time = 14.7 ms
^C
--- www.a.shifen.com ping statistics ---
7 packets transmitted, 6 received, 14 % packet loss, time 48609ms
rtt min/avg/max/mdev = 13.700 /14.604/16.559/0.955 ms
2 . screen
[ root@qls ~]
No Sockets found in /var/run/screen/S-root.
[ root@qls ~]
[ root@qls ~]
[ detached from 13659 .vim]
[ root@qls ~]
There is a screen on:
13659 .vim ( Detached)
1 Socket in /var/run/screen/S-root.
[ root@qls ~]
[ root@qls ~]
[ detached from 13659 .vim]
ctrl + a + d
exit
5. 系统平均负载
[ root@qls ~]
top - 11 :54:48 up 1 day, 1 :52, 3 users, load average: 0.00 , 0.01 , 0.05
[ root@qls ~]
11 :54:59 up 1 day, 1 :52, 3 users, load average: 0.00 , 0.01 , 0.05
[ root@qls ~]
11 :55:09 up 1 day, 1 :53, 3 users, load average: 0.00 , 0.01 , 0.05
1 分钟 5 分钟 15 分钟 系统的平均负载
平均负载跟系统CPU的使用率没有太大关系
在单位时间内,系统正在运行中的进程数或者不可中断的进程数量 进程的活跃数量
跟CPU的核心数有关系
当负载为2的时候
核心数为4 50 %
核心数为2 100 %
核心数为1 200 %
当1分钟的值小于5分钟和15分钟的时候 说明你之前的平均负载过高,当前正在降低
当1分钟跟5分钟和15分钟的值很近的时候,平均负载很稳定
当1分钟的值大于5分钟和15分钟的时候,说明现在的负载正在慢慢的变大
当值达到70% 就要开始分析问题
6. day21作业
1 .某系统管理员需要每天做一定的重复工作,编制一个解决方案:
( 1 ) .从下午4:50删除/abc目录下的全部子目录和全部文件;
50 16 * * * /bin/rm -rf /abc/* &> /dev/null
( 2 ) .每逢周一下午5:50将/data目录下的所有目录和文件归档并压缩为文件backup.tar.gz;
50 17 * * 1 /bin/tar czf $( date +\ %F) _backup.tar.gz /data &> /dev/null
2 .在每个月的第一天压缩/etc目录下的所有内容,存放在/root/backup目录里,且文件名为当前系统时间?
00 00 1 * * /bin/tar czf /root/backup/$( date +\ %F) _etc.tar.gz /etc &> /dev/null
3 .每周三01:00~04:00每3分钟执行一次/home/data/demo.sh
*/3 1 -4 * * 3 /bin/bash /home/data/demo.sh &> /dev/null
4 .每周一到周六的凌晨3点20分,运行tar命令对/etc/目录进行存档另存,存储位置为/data/bak,文件名为“system_+当前系统时间”
20 3 * * 1 -6 /bin/tar czf /data/bak/system_$( date +\ %F) .tar.gz /etc/ &> /dev/null
5 .每日凌晨2点30分,运行cp命令对/etc/fstab文件拷贝到/tmp下
30 2 * * * /bin/cp /etc/fstab /tmp/fstab_$( date +\ %F) &> /dev/null
6 .每月每天凌晨3点30分和中午12点20分执行test.sh脚本
30 3 * * * /bin/bash test.sh &> /dev/null
20 12 * * * /bin/bash test.sh &> /dev/null
7 .每月每天每隔6小时的每30分钟执行test.sh脚本
*/30 */6 * * * /bin/bash test.sh &> /dev/null
8 .每月每天早上8点到下午18点每隔2小时的每30分钟执行test.sh脚本
*/30 8 -18/2 * * * /bin/bash test.sh &> /dev/null
9 .每月每天晚上21点30分执行test.sh脚本
30 21 * * * /bin/bash test.sh &> /dev/null
10 .每月1号、10号、22号凌晨4点45分执行test.sh脚本
45 4 1,10 ,22 * * /bin/bash test.sh &> /dev/null
11.8 月份周一、周日凌晨1点10分执行test.sh脚本
10 1 * 8 1,7 /bin/bash test.sh &> /dev/null
12 .每月每天每小时整点执行test.sh脚本
00 * * * * /bin/bash test.sh &> /dev/null
13 .每月的4号或每周一到周三的11点重启vsftpd服务
00 11 4 * 1 -3 /bin/systemctl restart vsftpd &> /dev/null
14 .解释如下命令的含义
30 3,12 * * * /bin/sh /scripts/oldboy.sh
30 */6 * * * /bin/sh /scripts/oldboy.sh
30 8 -18/2 * * * /bin/sh /scripts/oldboy.sh
30 21 * * * /usr/sbin/nginx -s reload
45 4 1,10 ,22 * * /usr/sbin/nginx -s reload
10 1 * * 6,0 /usr/sbin/nginx -s reload
0,30 18 -23 * * * /usr/sbin/nginx -s reload
00 11 * 4 1 -3 /usr/sbin/nginx -s reload
15 .写一条计划任务,在每天晚上的3:05分执行脚本/usr/script/tongji.sh
05 3 * * * /bin/bash /usr/script/tongji.sh &> /dev/null
16 .在每周6的凌晨3:15执行/home/shell/collect.pl,并将标准输出和标准错误输出到/dev/null设备,请写出crontab中的语句
15 3 * * 6 /bin/bash /home/shell/collect.pl &> /dev/null