run linux command in background

How to Run Linux Command in Background

Sometimes you may need to run Linux command in background so that you can run other commands on your terminal, or close your terminal and end the session, while the command is still running. In this article, we will look at how to run Linux command in background. It is particularly useful for long running commands.

How to Run Linux Command in Background

It is very easy to run a Linux command in Background. You just need to add a & sign after the command with a space in between.

$ command &

For example, if you want to run a script at /home/ubuntu/ in background, here is the command to do it.

$ sudo /home/ubuntu/ &
[1] 2312

When you run a command in background, Linux will return its process ID as shown above. It is useful to monitor the process, or kill the command if you don’t need it to run.

For example, you can directly use kill command to kill the above process

$ sudo kill -9 2312

Even though your process runs in the background, it will continue to display outputs & error messages to the terminal as long as it is running. If you wish to suppress them, then use the following syntax

command > /dev/null 2>&1 &

For example,

$ sudo /home/ubuntu/ > /dev/null 2>&1 &

This will redirect stderr outputs to stdout and stdout to /dev/null which is a null file that discards anything written to it.

Also read : Show Commands with Date & Time in Linux History

List background processes

Use the jobs command to list all the processes that are running in background. It will display the process ID and actual command being executed, for each background process.

$ jobs -l
[1]+ 2312 Running                 sudo /home/ubuntu/ &

Also read : How to Limit Requests Per IP in Apache

Move a process to foreground

If you want to bring a background process to foreground, you can do so with fg command.

$ fg

If you have multiple background processes and want to bring only 1 process to foreground, then add % and job id after fg command.

$ fg %1

Also read : How to Use Linux History Command

Run Process in Background after Logout

When you run a process in background, it will terminate when you close the session or you get disconnected from your terminal. Here are some ways to run process in background even after logout.

You can use nohup keyword before the command, while running it background.

$ nohup command &

For example,

$ nohup sudo /home/ubuntu/ &

You will see the following output.

nohup: ignoring input and appending output to 'nohup.out'

You can also call the disown command to remove a process from shell’s control.

$ disown

The above command will remove all background processes from shell’s control. If you want to remove any specific process just include % and job id after disown command.

$ disown %1

That’s it. As you can see it is very easy and useful to run a process in background in Linux.

Also read : How to Change XAMPP Apache Server Port

Leave a Reply

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