【进程/作业管理】篇章二:Linux系统作业控制(jobs)

时间:2021-11-09 21:52:16

作业:jobs

 分类:

  前台作业(foregroud):通过终端启动,且启动后会一直占据终端
  后台作业(backgroud):可以通过终端启动,但启动后即转入后台运行(释放终端)



 

如何让作业运行于后台?

  (1),对于运行中的作业,

      Ctrl + z

      [注意:作业送往后台,作业会转为停止状态]

  (2),对于尚未启动的作业

      # COMMAND &

      [注意:此类作业虽然被送往后台,但其依然与终端相关]

    如果希望把送往后台的作业剥离与终端的关系: # nohup COMMAND &



查看所有的作业: jobs命令

  

[root@test1 ~]# jobs
[1] Stopped vim xx.txt      [1] 代表作业号
[2]- Stopped vim /etc/fstab   [-] 代表默认被管理的
[3]+ Stopped htop          [+] 代表下一个就被调回来
[root@test1 ~]#

 



可实现作业控制的常用命令:

# fg [[%]JOB_NUM]:把指定的作业调回前台; # bg [[%]JOB_NUM]:让送往后台的作业在后台继续运行; # kill %JOB_NUM:终止指定的作业;


示例1:创建作业时就掉到后台

[root@test1 ~]# htop &
[3] 14097

 

示例2:查看所有的作业

[root@test1 ~]# jobs
[1]   Stopped                 vim /etc/fstab
[2]-  Stopped                 vim /etc/yum.conf
[3]+  Stopped                 htop

 

示例3:把后台作业调回前台

[root@test1 ~]# jobs
[1]   Stopped                 vim /etc/fstab
[2]-  Stopped                 vim /etc/yum.conf
[3]+  Stopped                 htop
[root@test1 ~]# fg   【默认调回的是第三个作业】
[root@test1 ~]# fg 1    

#
# /etc/fstab
# Created by anaconda on Wed May 23 16:31:43 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=a314ef77-f6bf-451a-9d42-2b2e68b96d6b /                       ext4    defaults        1 1
UUID=2ee9e381-2283-44a7-9e5a-25ff00b6c91b /boot                   ext4    defaults        1 2
UUID=61b418bf-9d80-4285-9b6f-e6a6d23acd6b swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

[root@test1 ~]# fg %1

#
# /etc/fstab
# Created by anaconda on Wed May 23 16:31:43 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=a314ef77-f6bf-451a-9d42-2b2e68b96d6b /                       ext4    defaults        1 1
UUID=2ee9e381-2283-44a7-9e5a-25ff00b6c91b /boot                   ext4    defaults        1 2
UUID=61b418bf-9d80-4285-9b6f-e6a6d23acd6b swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

 

 

示例4:终止指定作业

[root@test1 ~]# jobs
[1]+  Stopped                 vim /etc/fstab
[2]-  Stopped                 vim /etc/yum.conf
[3]   Stopped                 htop
[root@test1 ~]# kill %2

[2]-  Stopped                 vim /etc/yum.conf