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
| Mode | Description | Terminal Blocked? |
|---|---|---|
| Foreground | Command runs normally, you wait for it | ✅ Yes |
| Background | Command 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 &
| Command | Description |
|---|---|
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)12345— PID 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
| Command | Description |
|---|---|
nohup ls & | Run ls in background, survives terminal close |
cat nohup.out | View 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
| Symbol | Meaning |
|---|---|
[1], [2], [3] | Job numbers |
+ | Current job (default for fg) |
- | Previous job |
Running | Job is running |
Stopped | Job is paused |
Done | Job completed |
Options for jobs
| Option | Description |
|---|---|
-s | Show only stopped jobs |
-r | Show only running jobs |
-p | Show 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
| Command | Description |
|---|---|
fg | Bring the current job to foreground |
fg %JN | Bring specific job number to foreground |
fg %1 | Bring 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
| Shortcut | Action |
|---|---|
Ctrl + Z | Suspend the foreground job |
bg | Resume suspended job in background |
fg | Resume 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.
| Command | Description |
|---|---|
kill %1 | Kill job 1 |
kill %2 | Kill 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
| Command | Description |
|---|---|
sudo shutdown -h now | Shut down immediately |
sudo shutdown -h +n | Shut down in n minutes |
sudo shutdown -h 0 | Shut down immediately |
sudo shutdown -h 22:00 | Shut down at a specific time |
sudo shutdown -r now | Reboot immediately |
sudo shutdown -c | Cancel 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
| Command | Description |
|---|---|
sudo reboot | Reboot immediately |
sudo reboot -t n | Reboot in n seconds |
sudo reboot 0 | Reboot immediately |
Examples:
$ sudo reboot
# System reboots immediately after confirmation
$ sudo reboot -t 60
# Reboots in 60 seconds
The poweroff Command
| Command | Description |
|---|---|
sudo poweroff | Power off immediately |
Example:
$ sudo poweroff
# System powers off immediately after confirmation
shutdown vs reboot vs poweroff
| Command | Action |
|---|---|
shutdown -h | Halt — shuts down and powers off |
reboot | Reboots (restarts) the system |
poweroff | Powers off (same as shutdown -h now) |
halt | Stops the CPU (may need manual power off) |
In practice:
shutdown -h now=poweroffshutdown -r now=reboot- Both
shutdownandrebootare aliases forsystemctlon 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
| Command | Description |
|---|---|
cmd & | Run in background |
nohup cmd & | Run in background, survives terminal close |
jobs | List background jobs |
jobs -r | Running jobs only |
jobs -s | Stopped jobs only |
fg | Bring current job to foreground |
fg %N | Bring job N to foreground |
bg | Resume current job in background |
bg %N | Resume job N in background |
Ctrl + Z | Suspend current job |
kill %N | Kill job N |
Shutdown / Reboot
| Command | Description |
|---|---|
shutdown -h now | Shut down immediately |
shutdown -h +N | Shut down in N minutes |
shutdown -h HH:MM | Shut down at specific time |
shutdown -r now | Reboot immediately |
shutdown -c | Cancel scheduled shutdown |
reboot | Reboot immediately |
reboot -t N | Reboot in N seconds |
poweroff | Power 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
| Pitfall | Problem | Solution |
|---|---|---|
Forgot & | Terminal blocked | Add & or Ctrl+Z then bg |
| Closing terminal | Background job killed | Use nohup |
| Wrong job number | Kill wrong process | Check with jobs first |
| Can’t interact with nohup job | No terminal input | Only use nohup for non-interactive |
| Shutdown too fast | Users lose work | Schedule with warning |
| Forgot to save work | Data loss | Save 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
| Concept | Command |
|---|---|
| Run in background | cmd & |
| Run and survive logout | nohup cmd & |
| List jobs | jobs |
| Bring to foreground | fg / fg %N |
| Resume in background | bg / bg %N |
| Suspend | Ctrl + Z |
| Kill job | kill %N |
| Shut down | shutdown -h now |
| Reboot | reboot / shutdown -r now |
| Power off | poweroff |
Key takeaways:
&runs a command in the background — terminal stays freenohupmakes a job survive the terminal closing — saves output tonohup.outjobslists all background jobs with their numbersfg %Nbrings a specific job to the foregroundbg %Nresumes a suspended job in the backgroundCtrl + Zsuspends the current job (paused, not killed)kill %Nkills a job by its job number- Use
shutdown -h +N "message"to schedule a shutdown with warning rebootandpoweroffare shorthand forshutdown -randshutdown -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!