📖 Scenario: You want to run a command on a Linux server that keeps running even if you close your terminal or get disconnected. This is useful when running long tasks like backups or downloads.
🎯 Goal: You will learn how to use the nohup command to run a process that stays alive after you log out. You will also learn how to check the output and confirm the process is running.
📋 What You'll Learn
Create a simple command that runs for a while
Use nohup to run the command persistently
Redirect output to a file
Check the output file to see the command's result
💡 Why This Matters
🌍 Real World
System administrators and developers often need to run scripts or commands that take a long time. Using <code>nohup</code> ensures these tasks keep running even if the terminal closes.
💼 Career
Knowing how to run persistent processes is essential for managing servers, running backups, or deploying applications in real environments.
Progress0 / 4 steps
1
Create a simple long-running command
Write a command that uses sleep 30 to pause for 30 seconds.
Linux CLI
Hint
The sleep command pauses the terminal for the number of seconds you specify.
2
Run the command with nohup to keep it running after logout
Use nohup before the command sleep 30 and redirect output to a file called output.log.
Linux CLI
Hint
nohup runs the command immune to hangups. Redirect output to output.log and run in background with &.
3
Check the output file for command result
Use cat output.log to display the contents of the output file.
Linux CLI
Hint
The cat command shows the contents of a file.
4
Confirm the process is running with ps
Use ps aux | grep sleep to check if the sleep 30 process is running.
Linux CLI
Hint
The ps aux command lists running processes. Use grep to filter for sleep.
Practice
(1/5)
1. What is the main purpose of using nohup in Linux command line?
easy
A. To speed up the execution of a command
B. To stop a running process immediately
C. To display the output of a command in color
D. To run a command that keeps running after logout or terminal closes
Solution
Step 1: Understand the function of nohup
nohup stands for 'no hang up' and is used to run commands that continue running even after the user logs out or the terminal closes.
Step 2: Compare options with this definition
Only "To run a command that keeps running after logout or terminal closes" matches this purpose. The other options describe unrelated functions.
Final Answer:
To run a command that keeps running after logout or terminal closes -> Option D
Quick Check:
nohup = persistent command run [OK]
Hint: Remember nohup means 'no hang up' to keep process alive [OK]
Common Mistakes:
Thinking nohup speeds up commands
Confusing nohup with output coloring
Assuming nohup stops processes
2. Which of the following is the correct syntax to run my_script.sh persistently in the background using nohup?
easy
A. nohup my_script.sh &
B. nohup & my_script.sh
C. my_script.sh nohup &
D. nohup my_script.sh
Solution
Step 1: Recall the correct nohup syntax
The correct way is to write nohup command & to run the command persistently in the background.
Step 2: Check each option
nohup my_script.sh & matches the correct syntax. nohup & my_script.sh places & before the command incorrectly. my_script.sh nohup & misplaces nohup. nohup my_script.sh runs nohup but not in background.
Final Answer:
nohup my_script.sh & -> Option A
Quick Check:
nohup + command + & = correct syntax [OK]
Hint: Use nohup command & to run in background [OK]
Common Mistakes:
Placing & before the command
Omitting & to run in background
Misplacing nohup after the command
3. What will be the output file created when you run nohup sleep 60 & without redirecting output?
medium
A. No file is created
B. nohup.out
C. output.txt
D. sleep.log
Solution
Step 1: Understand nohup default output behavior
If no output redirection is given, nohup writes stdout and stderr to a file named nohup.out in the current directory.
Step 2: Match this with options
Only nohup.out matches the default output file. Others are incorrect or no file.
Final Answer:
nohup.out -> Option B
Quick Check:
Default nohup output file = nohup.out [OK]
Hint: Default nohup output file is nohup.out if not redirected [OK]
Common Mistakes:
Assuming no file is created
Thinking output goes to sleep.log
Confusing output.txt as default
4. You run nohup my_script.sh & but the process stops after you logout. What is the most likely reason?
medium
A. You did not redirect output, causing terminal hangup
B. You forgot to add & to run in background
C. You ran the command without nohup
D. You closed the terminal before the command started
Solution
Step 1: Understand nohup and output redirection
Without redirecting output, nohup writes to nohup.out, but if permissions or disk issues prevent this, the process may stop on logout.
Step 2: Analyze options
You did not redirect output, causing terminal hangup correctly points to output redirection issues causing hangup. You forgot to add & to run in background is wrong because & was added. You ran the command without nohup is wrong because nohup was used. You closed the terminal before the command started is unlikely as process starts immediately.
Final Answer:
You did not redirect output, causing terminal hangup -> Option A
Quick Check:
Output redirection needed to avoid hangup [OK]
Hint: Always redirect output to avoid hangup with nohup [OK]
Common Mistakes:
Ignoring output redirection importance
Assuming & alone keeps process alive
Thinking nohup always works without output file
5. You want to run backup.sh persistently, save all output to backup.log, and avoid terminal clutter. Which command should you use?
hard
A. ./backup.sh > backup.log 2>&1 &
B. nohup ./backup.sh & > backup.log 2>&1
C. nohup ./backup.sh > backup.log 2>&1 &
D. nohup ./backup.sh 2>&1 > backup.log &
Solution
Step 1: Understand output redirection with nohup
To save both standard output and errors to a file, use command > file 2>&1. The ampersand & runs the command in background.
Step 2: Check each option's correctness
nohup ./backup.sh > backup.log 2>&1 & correctly places redirection before &. nohup ./backup.sh & > backup.log 2>&1 misplaces & before redirection. ./backup.sh > backup.log 2>&1 & lacks nohup. nohup ./backup.sh 2>&1 > backup.log & reverses redirection order, which is incorrect.
Final Answer:
nohup ./backup.sh > backup.log 2>&1 & -> Option C
Quick Check:
nohup + redirect stdout/stderr + & = correct [OK]
Hint: Redirect output before & to save logs with nohup [OK]
Common Mistakes:
Placing & before output redirection
Forgetting nohup for persistence
Reversing stderr and stdout redirection order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.