文章目录
文档配套视频讲解链接地址
- 腾讯课堂视频链接地址 : 41_进程管理_进程的概念1
- 腾讯课堂视频链接地址 : 42_进程管理_ps与kill2
- 腾讯课堂视频链接地址 : 43_进程管理_fg与bg3
第10章 进程管理
10.1 进程的理解与分类
- 进程是一个独立的可调度的任务
进程是一个程序的一次执行的过程
- 进程和程序的区别
程序是静态的,它是一些保存在磁盘上的指令的有序集合,没有任何执行的概念
进程是一个动态的概念,它是程序执行的过程,包括创建、调度和消亡
进程是程序执行和资源管理的最小单位
- 进程的分类
交互进程:该类进程是由shell控制和运行的。交互进程既可以在前台运行,也可以在后台运行。
批处理进程:该类进程不属于某个终端,它被提交到一个队列中以便顺序执行。
守护进程:该类进程在后台运行。它一般在Linux启动时开始执行,系统关闭时才结束。
- 主要的进程标识
进程号(Process Identity Number,PID) , PID唯一地标识一个进程
父进程号(Parent Process ID,PPID)
-
进程的状态
-
运行态:此时进程或者正在运行,或者准备运行。
-
等待态(挂起):此时进程在等待一个事件的发生或某种系统资源。
- v可中断
- v不可中断
-
停止态:此时进程被中止。
-
死亡态:这是一个已终止的进程,但还在进程向量数组中占有一个task_struct结构。
-
-
10.2 进程管理命令
1. ps 查看进程命令
ps命令来自于英文词组”process status“的缩写,其功能是用于显示当前系统的进程状态。使用ps命令可以查看到进程的所有信息,例如进程的号码、发起者、系统资源使用占比(处理器与内存)、运行状态等等。帮助我们及时的发现哪些进程出现”僵死“或”不可中断“等异常情况。
经常会与kill命令搭配使用来中断和删除不必要的服务进程,避免服务器的资源浪费。
语法格式 : ps [参数]
常用参数:
a | 显示现行终端机下的所有程序,包括其他用户的程序 |
---|---|
u | 以用户为主的格式来显示程序状况 |
x | 显示所有程序,不以终端机来区分 |
e | 列出程序时,显示每个程序所使用的环境变量 |
-f | 显示UID,PPIP,C与STIME栏位 |
常用命令
ps aux # 显示进程
ps -ef # 查看进程, 显示pid ppid
#精确查找进程
ps aux |grep sogou #精确查找sogou 输入法进程信息
ps -ef |grep sogou #精确查找sogou 输入法进程信息
# 可以使用kill 进程号 可以杀死进程
kill 进程号
参考实例
# USER : 进程的所有者, 哪个用户运行的进程
# PID : 进程的进程号,1是系统的守护进程, 是系统的超级进程
# %CPU : 占用cpu的百分比
# %MEM : 占用内存的百分比
# VSZ : 虚拟内存的大小
# RSS : 常驻内存集(Resident Set Size),表示该进程分配的内存大小
# TTY : 所属的终端
# STAT : 进程的状态
# START: 启动时间
# TIME : Time是该进程所占用的处理器时间
# COMMAND : 执行的程序
linux@ubuntu:~/work$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.3 225488 7616 ? Ss 8月24 0:06 /sbin/init auto noprompt
root 2 0.0 0.0 0 0 ? S 8月24 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 8月24 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 8月24 0:00 [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 8月24 0:00 [kworker/0:0H-kb]
root 9 0.0 0.0 0 0 ? I< 8月24 0:00 [mm_percpu_wq]
root 10 0.0 0.0 0 0 ? S 8月24 0:00 [ksoftirqd/0]
root 11 0.0 0.0 0 0 ? I 8月24 0:06 [rcu_sched]
root 12 0.0 0.0 0 0 ? S 8月24 0:00 [migration/0]
root 13 0.0 0.0 0 0 ? S 8月24 0:00 [idle_inject/0]
root 14 0.0 0.0 0 0 ? S 8月24 0:00 [cpuhp/0]
root 15 0.0 0.0 0 0 ? S 8月24 0:00 [cpuhp/1]
root 16 0.0 0.0 0 0 ? S 8月24 0:00 [idle_inject/1]
linux 1420 0.0 0.3 356328 6828 tty1 Sl 8月24 0:00 ibus-daemon --xim --panel disable
linux 1424 0.0 0.2 275628 5744 tty1 Sl 8月24 0:00 /usr/lib/ibus/ibus-dconf
linux 1426 0.0 0.7 339336 14868 tty1 Sl 8月24 0:00 /usr/lib/ibus/ibus-x11 --kill-daemon
linux 1429 0.0 0.2 273568 5116 ? Sl 8月24 0:00 /usr/lib/ibus/ibus-portal
linux 1436 0.0 0.2 266436 4888 ? Ssl 8月24 0:00 /usr/libexec/xdg-permission-store
linux 1443 0.0 0.5 689896 10860 ? Sl 8月24 0:00 /usr/lib/gnome-shell/gnome-shell-calendar-server
linux 1447 0.0 0.6 1367104 13536 ? Ssl 8月24 0:00 /usr/lib/evolution/evolution-source-registry
linux 1455 0.0 0.7 785840 14508 ? Sl 8月24 0:00 /usr/lib/gnome-online-accounts
# ps -ef
# UID : 用户名
# PID : 进程号
# PPID : 父进程号
# TTY : 终端
# STIME : Stime指进程启动时间
# TIME : Time是该进程所占用的处理器时间
# CMD : 执行的程序
linux@ubuntu:~/work$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 8月24 ? 00:00:07 /sbin/init auto noprompt
root 2 0 0 8月24 ? 00:00:00 [kthreadd]
root 3 2 0 8月24 ? 00:00:00 [rcu_gp]
root 4 2 0 8月24 ? 00:00:00 [rcu_par_gp]
root 6 2 0 8月24 ? 00:00:00 [kworker/0:0H-kb]
root 9 2 0 8月24 ? 00:00:00 [mm_percpu_wq]
root 10 2 0 8月24 ? 00:00:00 [ksoftirqd/0]
root 11 2 0 8月24 ? 00:00:06 [rcu_sched]
linux 1762 1144 0 8月24 tty1 00:00:04 /usr/bin/gnome-software --gapplication-service
linux 1764 1144 0 8月24 tty1 00:00:01 update-notifier
linux 1901 1380 0 8月24 ? 00:00:00 /usr/lib/gvfs/gvfsd-network --spawner :1.24 /org/gtk/gvfs/exec_spaw/1
linux 1942 1 0 8月24 ? 00:00:17 /opt/sogoupinyin/files/bin/sogoupinyin-watchdog
linux 1949 1 0 8月24 ? 00:00:04 /opt/sogoupinyin/files/bin/sogoupinyin-service
linux 1964 1380 0 8月24 ? 00:00:00 /usr/lib/gvfs/gvfsd-dnssd --spawner :1.24 /org/gtk/gvfs/exec_spaw/7
# 精确查找程序运行的进程信息
linux@ubuntu:~/work$ ps aux |grep sogou
linux 1269 0.0 0.0 11312 40 ? Ss 8月24 0:00 /usr/bin/ssh-agent /opt/sogoupinyin/files/bin/sogouimebs-session /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu
linux 1942 0.0 0.8 612732 16708 ? Sl 8月24 0:17 /opt/sogoupinyin/files/bin/sogoupinyin-watchdog
linux 1949 0.0 3.3 1496596 66832 ? Sl 8月24 0:04 /opt/sogoupinyin/files/bin/sogoupinyin-service
linux 6827 0.0 0.0 16180 1032 pts/0 S+ 10:26 0:00 grep --color=auto sogou
linux@ubuntu:~/work$ ps -ef|grep sogou
linux 1269 1144 0 8月24 ? 00:00:00 /usr/bin/ssh-agent /opt/sogoupinyin/files/bin/sogouimebs-session /usr/bin/im-launch env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu
linux 1942 1 0 8月24 ? 00:00:17 /opt/sogoupinyin/files/bin/sogoupinyin-watchdog
linux 1949 1 0 8月24 ? 00:00:04 /opt/sogoupinyin/files/bin/sogoupinyin-service
linux 6829 3371 0 10:27 pts/0 00:00:00 grep --color=auto sogou
linux@ubuntu:~/work$
# 可以使用kill 进程号 可以杀死进程
linux@ubuntu:~/work$ ps -ef|grep sogou
linux 6860 1 0 10:30 ? 00:00:00 /opt/sogoupinyin/files/bin/sogoupinyin-watchdog
linux 6869 1 3 10:30 ? 00:00:00 /opt/sogoupinyin/files/bin/sogoupinyin-service
linux 6884 3371 0 10:30 pts/0 00:00:00 grep --color=auto sogou
linux@ubuntu:~/work$ sudo kill 6860 6869
linux@ubuntu:~/work$ ps -ef|grep sogou
linux 6888 3371 0 10:31 pts/0 00:00:00 grep --color=auto sogou
linux@ubuntu:~/work$
2. kill 杀死进程命令
kill命令的功能是用于杀死(结束)进程,与英文单词的含义相同。Linux系统中如需结束某个进程,既可以使用如service或systemctl的管理命令来结束服务,也可以使用kill命令直接结束进程信息。
如使用kill命令后进程并没有被结束,则可以使用信号9进行强制杀死动作。
语法格式: kill [参数] 进程号
常用参数:
-l | 列出系统支持的信号 |
---|---|
-s | 指定向进程发送的信号 |
-a | 不限制命令名和进程号的对应关系 |
-p | 不发送任何信号 |
参考实例
列出系统支持的全部信号列表:
linux@ubuntu:~/work$ 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
linux@ubuntu:~/work$
常用方法:
kill -9 -1 # 9 是强制杀死进程 Kill all processes you can kill. 系统立即崩溃
kill -l 11 # Translate number 11 into a signal name.
kill -L # List the available signal choices in a nice table.
kill 123 543 2341 3453 # Send the default signal, SIGTERM, to all those processes. 使用默认信号15 去杀死进程
kill 123 # 是用15信号去杀死进程 ,这个信号可以被忽略,有可能杀不死
kill -9 123 # 是用9信号去杀死进程 , 9信号是强制杀死, 不能被忽略, 9 这个信号一定可以杀死
- 例如
linux@ubuntu:~/work$ ps -ef|grep sogou
linux 6936 3371 0 11:04 pts/0 00:00:00 grep --color=auto sogou
linux@ubuntu:~/work$ ps -ef|grep sogou
linux 6949 1 18 11:04 ? 00:00:00 /opt/sogoupinyin/files/bin/sogoupinyin-service
linux 6962 3371 0 11:04 pts/0 00:00:00 grep --color=auto sogou
linux@ubuntu:~/work$ kill -9 6949
linux@ubuntu:~/work$ ps -ef|grep sogou
linux 6965 3371 0 11:05 pts/0 00:00:00 grep --color=auto sogou
linux@ubuntu:~/work$
# 把系统的进程能杀死的全部杀死 , 自杀
linux@ubuntu:~/work$ kill -9 -1 # 执行后, 立即注销用户了
root@ubuntu:~/work# kill -9 -1 # 执行后, 立即宕机了
3. pstree命令
Linux系统中pstree命令的英文全称是“process tree”,即将所有行程以树状图显示,树状图将会以 pid (如果有指定) 或是以 init 这个基本行程为根 (root),如果有指定使用者 id,则树状图会只显示该使用者所拥有的行程。
语法格式: pstree [参数]
常用参数:
-a | 显示每个程序的完整指令,包含路径,参数或是常驻服务的标示 |
---|
pstree # 显示所有进程的详细信息
pstree -a #显示所有进程的所有详细信息,遇到相同的进程名可以压缩显示
参考实例
linux@ubuntu:~$ pstree
systemd─┬─ModemManager───2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ └─2*[{NetworkManager}]
├─VGAuthService
├─accounts-daemon───2*[{accounts-daemon}]
├─acpid
├─avahi-daemon───avahi-daemon
├─bluetoothd
├─boltd───2*[{boltd}]
├─colord───2*[{colord}]
├─cron
├─cups-browsed───2*[{cups-browsed}]
├─cupsd───dbus
├─2*[dbus-daemon]
├─fcitx───2*[{fcitx}]
├─fcitx-dbus-watc
├─fwupd───4*[{fwupd}]
├─rsyslogd───3*[{rsyslogd}]
├─rtkit-daemon───2*[{rtkit-daemon}]
├─snapd───13*[{snapd}]
├─sogoupinyin-ser───8*[{sogoupinyin-ser}] # 这就是sougou输入法的进程图
├─sogoupinyin-wat───4*[{sogoupinyin-wat}]
├─systemd─┬─(sd-pam)
│ ├─at-spi-bus-laun─┬─dbus-daemon
│ │ └─3*[{at-spi-bus-laun}]
│ ├─at-spi2-registr───2*[{at-spi2-registr}]
│ ├─dbus-daemon
│ ├─ibus-portal───2*[{ibus-portal}]
│ ├─pulseaudio───3*[{pulseaudio}]
│ └─xdg-permission-───2*[{xdg-permission-}]
├─systemd─┬─(sd-pam)
│ ├─at-spi-bus-laun─┬─dbus-daemon
│ │ └─3*[{at-spi-bus-laun}]
│ ├─at-spi2-registr───2*[{at-spi2-registr}]
│ ├─dbus-daemon
│ ├─dconf-service───2*[{dconf-service}]
│ ├─evolution-addre─┬─evolution-addre───5*[{evolution-addre}]
│ │ └─4*[{evolution-addre}]
│ ├─evolution-calen─┬─evolution-calen───8*[{evolution-calen}]
│ │ └─4*[{evolution-calen}]
│ ├─evolution-sourc───3*[{evolution-sourc}]
│ ├─gnome-shell-cal───5*[{gnome-shell-cal}]
│ ├─gnome-terminal-─┬─bash───pstree
│ │ └─3*[{gnome-terminal-}]
│ ├─goa-daemon───3*[{goa-daemon}]
│ ├─goa-identity-se───3*[{goa-identity-se}]
│ ├─gvfs-afc-volume───3*[{gvfs-afc-volume}]
│ ├─gvfs-goa-volume───2*[{gvfs-goa-volume}]
│ ├─gvfs-gphoto2-vo───2*[{gvfs-gphoto2-vo}]
│ ├─gvfs-mtp-volume───2*[{gvfs-mtp-volume}]
│ ├─gvfs-udisks2-vo───2*[{gvfs-udisks2-vo}]
│ ├─gvfsd─┬─gvfsd-trash───2*[{gvfsd-trash}]
│ │ └─2*[{gvfsd}]
│ ├─gvfsd-fuse───5*[{gvfsd-fuse}]
│ ├─ibus-portal───2*[{ibus-portal}]
│ └─xdg-permission-───2*[{xdg-permission-}]
├─systemd-journal
├─systemd-logind
├─systemd-resolve
├─systemd-timesyn───{systemd-timesyn}
├─systemd-udevd
├─udisksd───4*[{udisksd}]
├─unattended-upgr───{unattended-upgr}
├─upowerd───2*[{upowerd}]
├─vmtoolsd───2*[{vmtoolsd}]
├─vmtoolsd───3*[{vmtoolsd}]
├─vmware-vmblock-───2*[{vmware-vmblock-}]
├─whoopsie───2*[{whoopsie}]
└─wpa_supplicant
linux@ubuntu:~$
linux@ubuntu:~$ pstree -a #显示所有进程的所有详细信息,遇到相同的进程名可以压缩显示
systemd auto noprompt
├─ModemManager --filter-policy=strict
│ └─2*[{ModemManager}]
├─NetworkManager --no-daemon
│ ├─dhclient -d -q -sf /usr/lib/NetworkManager/nm-dhcp-helper -pf /run/dhclient-ens33.pid -lf...
│ └─2*[{NetworkManager}]
├─VGAuthService
├─accounts-daemon
│ └─2*[{accounts-daemon}]
├─acpid
├─avahi-daemon
│ └─avahi-daemon
├─bluetoothd
├─boltd
│ └─2*[{boltd}]
├─colord
│ └─2*[{colord}]
├─cron -f
├─cups-browsed
│ └─2*[{cups-browsed}]
├─cupsd -l
│ └─dbus dbus://
├─gnome-keyring-d --daemonize --login
│ └─3*[{gnome-keyring-d}]
├─gsd-printer
│ └─2*[{gsd-printer}]
├─ibus-x11 --kill-daemon
│ └─2*[{ibus-x11}]
├─ibus-x11 --kill-daemon
│ └─2*[{ibus-x11}]
├─irqbalance --foreground
│ └─{irqbalance}
├─kerneloops --test
├─kerneloops
├─networkd-dispat /usr/bin/networkd-dispatcher --run-startup-triggers
│ └─{networkd-dispat}
├─packagekitd
│ └─2*[{packagekitd}]
├─polkitd --no-debug
│ └─2*[{polkitd}]
├─pulseaudio --start --log-target=syslog
│ └─3*[{pulseaudio}]
├─rsyslogd -n
│ └─3*[{rsyslogd}]
├─rtkit-daemon
│ └─2*[{rtkit-daemon}]
├─snapd
│ └─13*[{snapd}]
├─sogoupinyin-ser
│ └─8*[{sogoupinyin-ser}]
├─sogoupinyin-wat
│ └─4*[{sogoupinyin-wat}]
├─systemd --user
│ ├─(sd-pam)
│ ├─at-spi-bus-laun
│ │ ├─dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
│ │ └─3*[{at-spi-bus-laun}]
│ ├─at-spi2-registr --use-gnome-session
│ │ └─2*[{at-spi2-registr}]
│ ├─dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
│ ├─ibus-portal
│ │ └─2*[{ibus-portal}]
│ ├─pulseaudio --daemonize=no
│ │ └─3*[{pulseaudio}]
│ └─xdg-permission-
│ └─2*[{xdg-permission-}]
├─systemd-udevd
├─udisksd
│ └─4*[{udisksd}]
├─unattended-upgr /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
│ └─{unattended-upgr}
├─upowerd
│ └─2*[{upowerd}]
├─vmtoolsd
│ └─2*[{vmtoolsd}]
├─vmtoolsd -n vmusr --blockFd 3
│ └─3*[{vmtoolsd}]
├─vmware-vmblock- /run/vmblock-fuse -o rw,subtype=vmware-vmblock,default_permissions,allow_other,dev,suid
│ └─2*[{vmware-vmblock-}]
├─whoopsie -f
│ └─2*[{whoopsie}]
└─wpa_supplicant -u -s -O /run/wpa_supplicant
linux@ubuntu:~$
4. top 任务管理器命令
top命令的功能是用于实时显示系统运行状态,包含处理器、内存、服务、进程等重要资源信息。工程师们常常会把top命令比作是“加强版的Windows任务管理器”,因为除了能看到常规的服务进程信息以外,还能够对处理器和内存的负载情况一目了然,实时感知系统全局的运行状态,非常适合作为接手服务器后执行的第一条命令。
语法格式: top [参数]
常用参数:
-d <秒> | 改变显示的更新速度 |
---|---|
-c | 切换显示模式 |
-s | 安全模式,不允许交互式指令 |
-i | 不显示任何闲置或僵死的行程 |
-n | 设定显示的总次数,完成后将会自动退出 |
-b | 批处理模式,不进行交互式显示 |
参考实例
top - 14:21:26 up 3:11, 1 user, load average: 0.07, 0.02, 0.00
任务: 337 total, 1 running, 257 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.2 us, 1.9 sy, 0.0 ni, 96.7 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
KiB Mem : 1999368 total, 99572 free, 1333920 used, 565876 buff/cache
KiB Swap: 2097148 total, 2089968 free, 7180 used. 501016 avail Mem
进程 USER PR NI VIRT RES SHR � %CPU %MEM TIME+ COMMAND 1604 linux 20 0 489756 85000 40080 S 5.0 4.3 0:11.92 Xorg 1732 linux 20 0 3556352 210304 94628 S 4.0 10.5 0:18.12 gnome-shell 2086 linux 20 0 693544 47552 35588 S 3.0 2.4 0:05.61 gnome-terminal- 1 root 20 0 225328 8528 6612 S 0.3 0.4 0:03.92 systemd 7 root 20 0 0 0 0 I 0.3 0.0 0:01.63 kworker/0:1-eve 410 root -51 0 0 0 0 S 0.3 0.0 0:00.33 irq/16-vmwgfx 605 root 0 -20 230284 7232 6596 S 0.3 0.4 0:15.11 vmtoolsd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd 3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp 4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
# top -c 显示执行程序完整的路径名
top - 14:23:21 up 3:12, 1 user, load average: 0.01, 0.01, 0.00
任务: 337 total, 1 running, 257 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.9 us, 1.3 sy, 0.0 ni, 97.6 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
KiB Mem : 1999368 total, 99048 free, 1334244 used, 566076 buff/cache
KiB Swap: 2097148 total, 2089968 free, 7180 used. 500612 avail Mem
进程 USER PR NI VIRT RES SHR � %CPU %MEM TIME+ COMMAND 1604 linux 20 0 489756 85000 40080 S 4.3 4.3 0:13.07 /usr/lib/xorg/Xorg vt1 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -backgroun+
1732 linux 20 0 3556268 210308 94628 S 4.3 10.5 0:19.06 /usr/bin/gnome-shell 2086 linux 20 0 693544 47552 35588 S 2.7 2.4 0:06.24 /usr/lib/gnome-terminal/gnome-terminal-server
605 root 0 -20 230284 7232 6596 S 1.3 0.4 0:15.27 /usr/bin/vmtoolsd 1 root 20 0 225328 8528 6612 S 0.3 0.4 0:03.98 /sbin/init auto noprompt
5. fg 切换到后台命令
fg命令是""用于将后台作业(在后台运行的或者在后台挂起的作业)放到前台终端运行。与bg命令一样,若后台任务中只有一个,则使用该命令时,可以省略任务号。
语法格式: fg [参数]
参考实例
test_fg.c实现
#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
while(1)
{
sleep(1);
}
return 0;
}
linux@ubuntu:~/work$ gcc test_fg.c -o test_fg
linux@ubuntu:~/work$ ./test_fg & # 可以把当前正运行的进程放到后台去执行
[2] 2513
linux@ubuntu:~/work$ ps -ef|grep test_fg
linux 2502 2093 0 14:31 pts/0 00:00:00 ./test_fg
linux 2513 2093 0 14:31 pts/0 00:00:00 ./test_fg
linux 2517 2093 0 14:32 pts/0 00:00:00 grep --color=auto test_fg
linux@ubuntu:~/work$ kill 2502 2513
linux@ubuntu:~/work$ ps -ef|grep test_fg
linux 2519 2093 0 14:32 pts/0 00:00:00 grep --color=auto test_fg
[1]- 已终止 ./test_fg
[2]+ 已终止 ./test_fg
linux@ubuntu:~/work$ ps -ef|grep test_fg
linux 2521 2093 0 14:32 pts/0 00:00:00 grep --color=auto test_fg
linux@ubuntu:~/work$ ./test_fg &
[1] 2522 # [1] 表示任务的编号
linux@ubuntu:~/work$ fg 2522 # 这里不能使用pid , 只能有任务编号 [1]里面的数字
bash: fg: 2522: 无此任务
linux@ubuntu:~/work$ fg 1 # 1 是任务编号 , 把任务1 切换到前台
./test_fg
^Z
[1]+ 已停止 ./test_fg # 把这个任务切换到后台并停止运行
6. bg 切换到前台命令
bg命令用于将作业放到后台运行,使前台可以执行其他任务。该命令的运行效果与在指令后面添加符号&的效果是相同的,都是将其放到系统后台执行。
语法格式: bg [参数]
参考实例
linux@ubuntu:~/work$ ps -ef|grep test_fg
linux 2532 2093 0 14:39 pts/0 00:00:00 grep --color=auto test_fg
linux@ubuntu:~/work$ ./test_fg # 在前台运行这个程序
^Z
[1]+ 已停止 ./test_fg # 使用ctrl +z 把进程切换到后台并停止
linux@ubuntu:~/work$ bg 1 # 在使用 bg 1 把这个程序运行起来
[1]+ ./test_fg &
linux@ubuntu:~/work$ fg 1 # 把1 任务切换到前台
./test_fg
^Z
[1]+ 已停止 ./test_fg
linux@ubuntu:~/work$ bg 1
[1]+ ./test_fg &
linux@ubuntu:~/work$ ls
1.1_1.2.patch 1.1.c test_fg test_fg.c
linux@ubuntu:~/work$ fg 1
./test_fg
^C
linux@ubuntu:~/work$