Skip to content

Run a process in background on Linux

Simple background process

command &

e.g.

top &

use jobs to see background PID

[1] 14814

Switch between foreground and background

Ctrl - Z

Process suspended, and return to the terminal

Check background process

jobs

Switch to background process

fg <job编号>

Let background process run in the background

bg <job编号>

Stop background process

kill %<job编号>

Run a background process and keep it running after logout

For example, a program that continuously outputs test.sh, as follows

while :
do
    echo hello
    sleep 1
done

hope to continue to execute after closing the terminal

nohup ./test.sh &

The default program output is in the nohup.out file

Exit the terminal and log in again to check the process

ps aux | grep test.sh

杀死进程,使用kill + 进程号

screen

yum install screen
apt install screen

Input screen, enter a new virtual terminal, and execute the task.

Detach the current screen

<Ctrl> + <A>, <D>

See all background terminals

screen -list

Resume the last closed terminal

screen -r

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.me, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try iOS App