|

Linux CLI 22 🐧 background/foreground processes and system reboot/shutdown

Linux lets you run commands in the background while you continue working, manage those jobs, and safely shut down or reboot the system.


Background vs Foreground

ModeDescriptionTerminal Blocked?
ForegroundCommand runs normally, you wait for it✅ Yes
BackgroundCommand runs behind the scenes❌ No

Visual:

FOREGROUND:
  Terminal ──→ Command ──→ Output ──→ Prompt
  (you wait here the whole time)

BACKGROUND:
  Terminal ──→ Prompt (immediately)
                  ↓
             Command runs in background
                  ↓
             Output (when ready)

Running Commands in the Background (&)

Add & at the end of a command to run it in the background.

ls &
nano aaa.txt &
nano 1.txt &
CommandDescription
ls &Run ls in the background
nano aaa.txt &Open nano in the background
command &Any command in the background

Example 1: ls &

$ ls &
[1] 12345
file1.txt  file2.txt  notes.txt
[1]+  Done                    ls

What you see:

  • [1]job number (assigned by the shell)
  • 12345PID of the background process
  • Output appears when ready
  • [1]+ Done — job finished

Example 2: nano aaa.txt &

$ nano aaa.txt &
[1] 12346
# nano opens in the background, you can keep typing commands

Use case: Open multiple editors or long-running commands without blocking your terminal.


The nohup Command

NO HANG UP — runs a command that survives the terminal closing.

nohup ls &
cat nohup.out
CommandDescription
nohup ls &Run ls in background, survives terminal close
cat nohup.outView output that was saved
nohup bash -c 'cmd1 && cmd2'Multiple commands with nohup

Why nohup Matters

Normally, when you close a terminal:

  • Background jobs receive SIGHUP (hang up signal)
  • The processes are terminated
  • You lose any unfinished work

With nohup:

  • The process ignores SIGHUP
  • It keeps running after the terminal closes
  • Output is saved to nohup.out (or a file you specify)

Examples

Basic nohup:

$ nohup ls &
[1] 12347
nohup: ignoring input and appending output to 'nohup.out'

$ cat nohup.out
file1.txt  file2.txt  notes.txt

Long-running script:

$ nohup ./backup.sh &
[1] 12348
nohup: ignoring input and appending output to 'nohup.out'

Multiple commands:

$ nohup bash -c 'cd /data && ./process.sh' &

Custom output file:

$ nohup ./script.sh > script.log 2>&1 &

Now you can:

  • Close the terminal
  • Log out
  • Come back later — the process is still running!

Checking nohup Output

$ cat nohup.out
# Shows what the command produced

$ tail -f nohup.out
# Watch output in real-time

The jobs Command

Lists all jobs (background and suspended) started in the current shell.

jobs

Example output:

$ jobs
[1]   Running                 nano aaa.txt &
[2]-  Running                 ./backup.sh &
[3]+  Stopped                 vim notes.txt
SymbolMeaning
[1], [2], [3]Job numbers
+Current job (default for fg)
-Previous job
RunningJob is running
StoppedJob is paused
DoneJob completed

Options for jobs

OptionDescription
-sShow only stopped jobs
-rShow only running jobs
-pShow only PIDs

Examples:

$ jobs -s
[3]+  Stopped                 vim notes.txt

$ jobs -r
[1]   Running                 nano aaa.txt &

$ jobs -p
12345
12346
12347

The fg Command

ForeGround — brings a background or suspended job back to the foreground.

fg
fg %JN
CommandDescription
fgBring the current job to foreground
fg %JNBring specific job number to foreground
fg %1Bring job 1 to foreground

Note: Some distros don’t require the %fg 1 may work.


Examples

Bring current job:

$ jobs
[1]+  Running                 nano aaa.txt &

$ fg
# nano comes to the foreground — you can edit

Bring specific job:

$ jobs
[1]   Running                 nano aaa.txt &
[2]-  Running                 ./backup.sh &
[3]+  Running                 vim notes.txt

$ fg %3
# vim comes to the foreground

Suspending and Resuming Jobs

ShortcutAction
Ctrl + ZSuspend the foreground job
bgResume suspended job in background
fgResume suspended job in foreground

Example:

$ vim notes.txt
# Press Ctrl+Z
[1]+  Stopped                 vim notes.txt

$ jobs
[1]+  Stopped                 vim notes.txt

$ bg
[1]+ vim notes.txt &

$ jobs
[1]+  Running                 vim notes.txt &

The bg Command

BackGround — resumes a suspended job in the background.

bg
bg %JN

Example:

$ nano aaa.txt
# Press Ctrl+Z
[1]+  Stopped                 nano aaa.txt

$ bg
[1]+ nano aaa.txt &

$ jobs
[1]+  Running                 nano aaa.txt &

Killing Jobs

kill %JN

Use % before the job number — not the PID.

CommandDescription
kill %1Kill job 1
kill %2Kill job 2
kill %+Kill current job
kill %-Kill previous job

Example:

$ jobs
[1]   Running                 nano aaa.txt &
[2]-  Running                 ./backup.sh &
[3]+  Running                 vim notes.txt

$ kill %3
[3]+  Terminated              vim notes.txt

System Shutdown and Reboot

sudo shutdown -h now
sudo shutdown -h +n
sudo shutdown -h 0
sudo reboot
sudo reboot -t n
sudo reboot 0
sudo poweroff

The shutdown Command

CommandDescription
sudo shutdown -h nowShut down immediately
sudo shutdown -h +nShut down in n minutes
sudo shutdown -h 0Shut down immediately
sudo shutdown -h 22:00Shut down at a specific time
sudo shutdown -r nowReboot immediately
sudo shutdown -cCancel a pending shutdown

Examples:

# Shut down right now
$ sudo shutdown -h now

# Shut down in 5 minutes
$ sudo shutdown -h +5

# Shut down at 10 PM
$ sudo shutdown -h 22:00

# Reboot in 10 minutes
$ sudo shutdown -r +10

# Cancel a scheduled shutdown
$ sudo shutdown -c

The reboot Command

CommandDescription
sudo rebootReboot immediately
sudo reboot -t nReboot in n seconds
sudo reboot 0Reboot immediately

Examples:

$ sudo reboot
# System reboots immediately after confirmation

$ sudo reboot -t 60
# Reboots in 60 seconds

The poweroff Command

CommandDescription
sudo poweroffPower off immediately

Example:

$ sudo poweroff
# System powers off immediately after confirmation

shutdown vs reboot vs poweroff

CommandAction
shutdown -hHalt — shuts down and powers off
rebootReboots (restarts) the system
poweroffPowers off (same as shutdown -h now)
haltStops the CPU (may need manual power off)

In practice:

  • shutdown -h now = poweroff
  • shutdown -r now = reboot
  • Both shutdown and reboot are aliases for systemctl on modern systems

Behind the scenes:

sudo shutdown -h now    →  sudo systemctl poweroff
sudo reboot             →  sudo systemctl reboot
sudo poweroff           →  sudo systemctl poweroff

Complete Example Session

# ============================================
# PART 1: BACKGROUND JOBS
# ============================================

# Run ls in background
$ ls &
[1] 12345
file1.txt  file2.txt  notes.txt
[1]+  Done                    ls

# Run nano in background
$ nano aaa.txt &
[1] 12346

# Multiple background jobs
$ nano 1.txt &
[2] 12347

$ jobs
[1]-  Running                 nano aaa.txt &
[2]+  Running                 nano 1.txt &

# ============================================
# PART 2: NOHUP
# ============================================

# Run with nohup
$ nohup ls &
[1] 12348
nohup: ignoring input and appending output to 'nohup.out'

# Check output
$ cat nohup.out
file1.txt  file2.txt  notes.txt

# Long-running with nohup
$ nohup ./backup.sh &
[2] 12349

# Close the terminal
# Reopen later
$ cat nohup.out
Backup started...
Backup completed!

# ============================================
# PART 3: JOB CONTROL
# ============================================

# List jobs
$ jobs
[1]   Running                 nano aaa.txt &
[2]-  Stopped                 vim notes.txt
[3]+  Running                 ./backup.sh &

# Only stopped
$ jobs -s
[2]-  Stopped                 vim notes.txt

# Only running
$ jobs -r
[1]   Running                 nano aaa.txt &
[3]+  Running                 ./backup.sh &

# Just PIDs
$ jobs -p
12346
12348
12349

# ============================================
# PART 4: FG AND BG
# ============================================

# Bring default job to foreground
$ fg
# Brings job 3 (current) to foreground

# Bring specific job
$ fg %1
# Brings job 1 to foreground

# Suspend a job
$ vim notes.txt
# Press Ctrl+Z
[1]+  Stopped                 vim notes.txt

# Resume in background
$ bg
[1]+ vim notes.txt &

# Resume in foreground
$ fg %1
# vim comes back to foreground

# ============================================
# PART 5: KILLING JOBS
# ============================================

$ jobs
[1]   Running                 nano aaa.txt &
[2]-  Running                 ./backup.sh &
[3]+  Running                 vim notes.txt

# Kill job 3
$ kill %3
[3]+  Terminated              vim notes.txt

# Kill job 1
$ kill %1
[1]+  Terminated              nano aaa.txt

# ============================================
# PART 6: SHUTDOWN AND REBOOT
# ============================================

# Shut down immediately
$ sudo shutdown -h now

# Shut down in 5 minutes
$ sudo shutdown -h +5
Shutdown scheduled for Mon 2024-01-15 10:40:00 UTC, use 'shutdown -c' to cancel.

# Cancel shutdown
$ sudo shutdown -c

# Reboot
$ sudo reboot

# Power off
$ sudo poweroff

# Schedule reboot in 10 minutes
$ sudo shutdown -r +10

# Reboot with timeout
$ sudo reboot -t 30

Quick Reference

Background Jobs

CommandDescription
cmd &Run in background
nohup cmd &Run in background, survives terminal close
jobsList background jobs
jobs -rRunning jobs only
jobs -sStopped jobs only
fgBring current job to foreground
fg %NBring job N to foreground
bgResume current job in background
bg %NResume job N in background
Ctrl + ZSuspend current job
kill %NKill job N

Shutdown / Reboot

CommandDescription
shutdown -h nowShut down immediately
shutdown -h +NShut down in N minutes
shutdown -h HH:MMShut down at specific time
shutdown -r nowReboot immediately
shutdown -cCancel scheduled shutdown
rebootReboot immediately
reboot -t NReboot in N seconds
poweroffPower off immediately

Best Practices

Do This:

# Use & for quick background tasks
long-running-script.sh &

# Use nohup for long jobs that must survive
nohup backup.sh &

# Always check jobs before killing
jobs
kill %1

# Use fg to bring back when ready
fg %1

# Schedule shutdown with warning for users
sudo shutdown -h +10 "System maintenance in 10 minutes"

# Cancel shutdown if needed
sudo shutdown -c

# Use Ctrl+Z to suspend temporarily
# (better than killing and restarting)

Don’t Do This:

# Don't forget & for long jobs
./backup.sh            # ❌ Terminal blocked!
./backup.sh &          # ✅

# Don't use nohup when you need to interact
nohup vim file.txt &   # ❌ No input possible!
vim file.txt           # ✅

# Don't kill jobs by PID when you have job numbers
kill 12345             # ⚠️ Need to look up PID
kill %1                # ✅ Easier

# Don't shutdown with -h now without warning users
sudo shutdown -h now   # ⚠️ Warn first!
sudo shutdown -h +5 "Reboot for maintenance"   # ✅ Better

# Don't forget to save work before shutdown
# Always save open editors/documents

Common Pitfalls

PitfallProblemSolution
Forgot &Terminal blockedAdd & or Ctrl+Z then bg
Closing terminalBackground job killedUse nohup
Wrong job numberKill wrong processCheck with jobs first
Can’t interact with nohup jobNo terminal inputOnly use nohup for non-interactive
Shutdown too fastUsers lose workSchedule with warning
Forgot to save workData lossSave before rebooting

Real-World Examples

1. Long-Running Backup

# Start backup that survives logout
$ nohup ./backup.sh > backup.log 2>&1 &

# Later, check status
$ tail -f backup.log

2. Edit Multiple Files

# Open multiple files in the background
$ nano file1.txt &
[1] 12345
$ nano file2.txt &
[2] 12346

# Switch between them
$ jobs
$ fg %1

3. Development Server

# Start server in background
$ npm start &

# Keep working
$ vim code.js

# Check server output
$ jobs
[1]+  Running                 npm start &

4. Scheduled Maintenance

# Notify users
$ sudo shutdown -h +10 "Server maintenance in 10 minutes"

# Users see:
Broadcast message from root@olympos:
The system is going down for power-off at 10:45!

# Cancel if needed
$ sudo shutdown -c

5. Run Process That Ignores Hangups

$ nohup python long_task.py &
[1] 12349
nohup: ignoring input and appending output to 'nohup.out'

# Close terminal
# Come back tomorrow
$ cat nohup.out
Task completed successfully!

6. Suspend and Resume

$ vim important.txt
# Urgent interruption!
# Press Ctrl+Z
[1]+  Stopped                 vim important.txt

# Handle the interruption
$ ls

# Come back to vim
$ fg
# vim is right where you left it!

Visual: Job Control Flow

┌─────────────────────────────────────────────────────┐
│                   Foreground                        │
│                                                     │
│  $ ./script.sh          ← Command running           │
│     (you wait)                                      │
│                                                     │
│  Ctrl+Z  ─────────────→  [1]+ Stopped               │
│                              │                      │
│                              ├── bg ──→  Running &  │
│                              │                      │
│                              └── fg ──→  Foreground │
│                                                     │
│  cmd &  ─────────────→  [1] 12345  Running &        │
│                              │                      │
│                              └── fg ──→  Foreground │
│                                                     │
│  nohup cmd &  ────────→  Survives terminal close    │
└─────────────────────────────────────────────────────┘

Summary

ConceptCommand
Run in backgroundcmd &
Run and survive logoutnohup cmd &
List jobsjobs
Bring to foregroundfg / fg %N
Resume in backgroundbg / bg %N
SuspendCtrl + Z
Kill jobkill %N
Shut downshutdown -h now
Rebootreboot / shutdown -r now
Power offpoweroff

Key takeaways:

  • & runs a command in the background — terminal stays free
  • nohup makes a job survive the terminal closing — saves output to nohup.out
  • jobs lists all background jobs with their numbers
  • fg %N brings a specific job to the foreground
  • bg %N resumes a suspended job in the background
  • Ctrl + Z suspends the current job (paused, not killed)
  • kill %N kills a job by its job number
  • Use shutdown -h +N "message" to schedule a shutdown with warning
  • reboot and poweroff are shorthand for shutdown -r and shutdown -h
  • Always save your work before shutting down or rebooting

Remember: Job control is one of the terminal’s superpowers. & for quick background tasks, nohup for long jobs you need to survive logout, jobs to see what’s running, and fg/bg to switch between foreground and background. And when it’s time to shut down — schedule it with a warning (shutdown -h +10 "message") instead of pulling the rug out from under your users!


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!