Skip to content

Instantly share code, notes, and snippets.

@AlexNDRmac
Last active April 29, 2019 18:19
Show Gist options
  • Save AlexNDRmac/fd39af647944346cfea701db869718be to your computer and use it in GitHub Desktop.
Save AlexNDRmac/fd39af647944346cfea701db869718be to your computer and use it in GitHub Desktop.
Using Jobs via BASH

If you’ve just started a huge process (like backupping a lot of files) using an ssh terminal and you suddenly remember that you need to do something else on the same server, you might want to get the huge process to the background. You can do this by pressing Ctrl + z, which will suspend the process, and then executing the bg command:

$ bg
[1]+ hugeprocess &

This will make the huge process continue happily in the background, allowing you to do what you need to do. If you want to background another process with the huge one still running, just use the same steps. And if you want to get a process back to the foreground again, execute fg:

$ fg
hugeprocess

But what if you want to foreground an older process that’s still running? In a case like that, use the jobs command to see which processes bash is managing:

$ jobs
[1]- Running hugeprocess &
[2]+ Running anotherprocess &

Note: A “+” after the job id means that that job is the ‘current job’, the one that will be affected if bg or fg is executed without any arguments. A “-” after the job id means that that job is the ‘previous job’. You can refer to the previous job with “%-“.

Use the job id (the number on the left), preceded by a “%”, to specify which process to foreground / background, like this:

$ fg %3
$ bg %7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment