How to run a command without having to wait in Linux/Ubuntu

You may have encountered some commands which take a while to complete processing. There are some tricks that could help gain control once you launch the command so that you can do other things meanwhile.

1 – First you can append the sign ‘&’ to the end of command so that it will run in the background as follows:

your_command &

You can hit Ctrl+Z in order to suspend it. To put it in the background, use the ‘bg’ tool:

your-command
[Ctrl+Z]
bg

2 – The command below :

nohup your_command &

Will run the process in background but it also generates a log ( nohup.out in your current directory or in your home directory otherwise).
Furthermore, if you exit your current shell, the process is not killed.

Read: Monitoring system processes in Ubuntu using htop.

3 – Using also disown as shown below :

your_command &
disown

The process here will detach itself from the current shell so that you can carry out other tasks….

4 – Redirection. The command :

your_command > output_file.log 2>&1 &

This will launch the process related to your command and redirect stdout and stderr to a file output.log. In case you do not need the output, you can replace the output file with /dev/null.

Read: How to execute commands in parallel in Linux

The sign ‘&’ if for background processing .

2>&1 is used to redirect stderr to stdout so that the output can be retrieved.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

This Post Has One Comment

  1. Rodrigo Barretos

    I’m using a ‘system’ command in a Ruby script and for me the 4th was the only one that worked perfectly:

    4 – Redirection. The command :
    your_command > output_file.log 2>&1 &

    Thank you for the post!

Leave a Reply