Run Command In Background on Linux

A background process is the process which executes behind the scenes. Suppose you have a long-running task that you need to run, but you also want to get some other work done. Linux lets you start a task in the background and keep on doing other things from the command prompt. 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.
Run Command In Background on Linux

Adding & the end of the command

On Linux/Unix based systems you can put a task (such as command or script) in a background by appending a & at the end of the command line. In other words you can say that easily send any process in backgroud.

# command &
# script-name &
# find / -name *.c > /root/output.txt &

Using & with nohup

You can use nohup command line-utility which allows to run command/process or shell script that can continue running in the background after you log out from a shell:

# nohup command-name &
# nohup find / -size +1k > /root/output.txt &

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.

# find / -size +1k > /root/output.txt &

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

Enjoy it!

No Responses

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.