官术网_书友最值得收藏!

Process management

Since we have understood the command to check processes, we will learn more about managing different processes.

  • In a Bash shell, when we enter any command or start any program, it starts running in the foreground. In such a situation, we cannot run more than one command in the foreground. We need to create many Terminal windows for starting many processes. If we need to start many processes or programs from the same Terminal, then we will need to start them as background processes.
  • If we want to start a process in the background, then we need to append the command in the Bash shell by &.
  • If I want to start my Hello program as the background process, then the command would be as follows:
    $ Hello &
  • If we terminate any command by &, then it starts running as the
    background process.

For example, we will issue a simple sleep command, which creates a new process. This process sleeps for the duration, which is mentioned in the integer value next to the sleep command:

  1. The following command will make the process sleep for 10,000 seconds. This means we will not be able to run any other command from the same Terminal:
    $ sleep 10000
  1. Now, you can press the Ctrl + C key combination to terminate the process created by the sleep command.

  1. Now, use the following command:
    $ sleep 10000 &

The preceding command will create a new process, which will be put to sleep for 10000 seconds; but this time, it will start running in the background. Therefore, we will be able to enter the next command in the Bash Terminal.

  1. Since the newly created process is running in the background, we can enter new commands very easily in the same Terminal window:
    $ sleep 20000 &
    $ sleep 30000 &
    $ sleep 40000 &
  1. To check the presence of all the processes, enter the following command:
    $ jobs

The jobs command lists all the processes running in the Terminal, including foreground and background processes. You can clearly see their status as running, suspended, or stopped. The numbers in [] show the job ID.
The + sign indicates which command will receive fg and bg commands by default. We will study them in the following topics.

  1. If you want to make any existing background process run in the foreground, then use the following command:
      $ fg 3    

The preceding command will make the job number 3 run in the foreground instead of the background.

If we want to make the process stop executing and get it suspended, then press Ctrl + Z. This key combination makes the foreground process stop executing. Please note that the process has stopped, but is not terminated.

  1. To make the stopped process continue running in the background, use the following command:
    $ bg job_number
    $ bg 3

The preceding command will make suspended job process number 3 run in the background.

  1. If you wish to terminate the process, you can use the job ID or process ID as follows:
    $ jobs -l         //  This will list jobs with pid
    $ kill pid        // or
    $ kill %job_id    // This will kill job
    $ kill %3
主站蜘蛛池模板: 乌什县| 万盛区| 吉安市| 上高县| 卓资县| 库车县| 宁津县| 澎湖县| 广昌县| 拉孜县| 鹤壁市| 曲阳县| 苍梧县| 会宁县| 光泽县| 攀枝花市| 弥渡县| 桓仁| 敖汉旗| 阿鲁科尔沁旗| 离岛区| 文水县| 克什克腾旗| 高密市| 南充市| 连南| 屯门区| 康马县| 河池市| 万源市| 昌邑市| 鄢陵县| 阳高县| 孟连| 洛隆县| 长葛市| 龙游县| 通辽市| 滦平县| 灵武市| 成都市|