Skip to content

How To Run Command In Background on Linux

On this page

A background process is the process which executes behind the scenes. These processes run without user intervention. There are various way to do it for example using & or nohup or screen command. The most popular and frequently used are & and nohup. Including of these two options this tutorial will also describes you to use of screen command.

1. Using &

On Linux/Unix based systems we can easily send any process in background by appending & in command.

$ ls -l &
$ tar czf temp.tar.gz /tmp &

In this way if you start a command in background and exited from shell, then shell will kill this background process with SIGHUP signal.

2. Using nohup

To prevent termination of background processes after shell termination use nohup command. nohup command simply use at prefix of command like. All the output generated with commands will save in nohup.out file in current directory.

$ nohup tar czf temp.tar.gz /tmp &

3. Using Screen

Screen is better option for long running processes. Using screen you can simply get the same session running on remote server even after logout from ssh. For this test you can do the following

$ screen

Now start your process in background.

$ tar czf temp.tar.gz /tmp &

Let’s detach your screen session with keyboard shortcut CTRL+a+d or ctrl+alt+d. It will return you to command line. Now exit your ssh session and again connect system through ssh and execute following command to get old screen session running.

$ screen -r

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top