Skip to content

Run process in background on Linux

homepage-banner

Introduction

Linux is a powerful operating system that provides users with a wide range of features and capabilities. One of the most useful features of Linux is the ability to run processes in the background, allowing users to continue working on other tasks without interruption. In this blog post, we will explore how to run a process in the background in 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 No.>

Let background process run in the background

bg <job No.>

Stop background process

kill %<job No.>

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 the process, use kill + process number

using 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

Conclusion

Running a process in the background is a useful feature that can help you multitask more efficiently on your Linux system. In this blog post, we explored three different methods for running processes in the background: using the & operator, using the nohup command, and using the screen command. With these methods, you can run processes in the background and continue working on other tasks without interruption.

Leave a message