All about background tasks

This post will cover how to

  1. start running a programme in a terminal’s background

  2. monitor the programme and

  3. stop the programme

I decided to write this post beacuse I did not find any comprehensive tutorial to cover this aspect.

  1. start running a programme in a terminal’s background To do so, simply add & at the end of the line. What occurs after the enter is hit is that the first line will be run at the background and the terminal will prompt you another line of input.

  2. monitor the programme When the programme is run in the background, you can check its status by enter jobs. Usually it will show either Running, Stopped or Done. If there is no current jobs and the you have seen all the status of jobs that are done, jobs will give no output.

  3. stop the programme You will see a job ID when you run jobs. To kill that specific job (say its ID is 3), use kill %3

Additional tips:

  • Standard stream: Even after you have directed the output of the programme using >, many programmes are designed to prompt you the error or just the progress of the programme using standard error. This may be distracting because even after you have run the programme in the background and continue to typing other commands, the standard errors will be printed to the screen, sometimes to the line where you are typing. To stop this and only view all the errors when you want to, use 2> to redirect the standard errors to a new file.

  • Server If you are using a server, you can use nohup at the start of the command so that even if the connection between your local machine and the server breaks down, the programme will still be running.

Not-so-pro Tip: to test on how to run a background task, you can install a programme called yes using sudo apt install yes. This is a programme that continously outputs y to your terminal and can be used to give confirmation to other programmes (e.g. when installing other software).

be cautious when you save the command (yes) output to a file. I tried to run it for around 20 seconds and the file size became 16GB…

Timing Liu
Computational Biologist & Medical Student

Personalizing medicine