Labs ICT
Pro Login

Background Jobs

Some commands take a while — a long build, a large file copy, or a server you want to keep running. The terminal normally blocks until the command finishes. But Unix gives you ways to run things in the background and keep working.

Running in the background with &

Add an ampersand & at the end of a command to run it in the background instantly. The shell gives you back your prompt right away. It prints a job number in brackets and a PID.

$ sleep 30 &
[1] 12345
$ echo "I can still type while sleep runs"
I can still type while sleep runs
Try it Yourself →

Suspending and resuming with Ctrl+Z, bg, and fg

If you forgot to add & and a command is already running, hit Ctrl+Z to suspend it. The process pauses and you get your prompt back. Then use bg to resume it in the background or fg to bring it back to the foreground.

$ sleep 30
^Z
[1]+  Stopped                 sleep 30
$ bg
[1]+ sleep 30 &
$ fg
sleep 30
Try it Yourself →

Managing jobs with the jobs command

jobs lists everything you have running or suspended in the current terminal session. Each job gets a number. You can reference it with %1, %2, etc.

$ sleep 30 &
[1] 12345
$ sleep 60 &
[2] 12346
$ jobs
[1]-  Running                 sleep 30 &
[2]+  Running                 sleep 60 &
$ fg %1
sleep 30
Try it Yourself →

Keeping processes alive after logout with nohup

When you log out or close the terminal, the shell sends a SIGHUP signal to all child processes, which kills them. nohup (no hangup) makes a command immune to this. Output goes to nohup.out by default.

$ nohup long-running-script.sh &
[1] 12347
$ exit

When you log back in, the script is still running. nohup is the simplest way to keep something alive on a remote server without needing tools like screen or tmux.

Try it Yourself →