Linux Commands in Practical DevSecOps Labs: Success, Failure, and Background Execution
Across the Practical DevSecOps courses, one of the first hands-on exercises you'll encounter is learning the basics of Linux commands. This article covers what you need to know about how commands behave when they succeed or fail, and how to run long-running operations in the background.
Table of Contents
Successful vs Unsuccessful Command Runs
A key concept in Linux is that successful command execution typically produces no output, while unsuccessful execution displays an error message.
Listing directories with ls
ls
When run in a directory that exists, ls returns a list of files and directories. This is a read operation, so it returns visible output on success.
If you run ls on a directory that does not exist:
ls -l abracadabra
ls: cannot access 'abracadabra': No such file or directory
An error is shown because the read operation failed.
Creating and removing directories
mkdir my-directory
When this succeeds, no output is printed to the terminal. Silence means success.
Running the same command again:
mkdir my-directory
mkdir: cannot create directory 'my-directory': File exists
This time an error is shown because the directory already exists.
The same pattern applies to rmdir:
rmdir my-directory # succeeds silently
rmdir my-directory # fails: No such file or directory
Key takeaway
| Outcome | Terminal Output |
|---|---|
| Success | Typically no output (silent) |
| Failure | Error message explaining why it failed |
Tip: You can revisit your command history using the up and down arrow keys.
Blocking vs Non-Blocking Command Runs
Some commands take a long time to complete. During that time, your terminal is "blocked" and you cannot run other commands.
Blocking (foreground) execution
pip3 install ansible==9.13.0 ansible-lint==4.3.7
While this command runs, your terminal is occupied. You must wait for it to finish before you can type another command.
Non-blocking (background) execution
Add an ampersand (&) at the end of the command to run it in the background:
pip3 install ansible==9.13.0 ansible-lint==4.3.7 &
This returns control to your terminal immediately, allowing you to do other work while the installation proceeds in the background.
Note: You can combine
nohupwith&to ensure the background process continues even after you log out. Read more at Stack Overflow: nohup vs &.
Common Questions
| Question | Answer |
|---|---|
| How do I know if a command succeeded? | If there's no error output, it likely succeeded. Use echo $? to check the exit code (0 = success). |
Why doesn't mkdir show any confirmation? |
Linux follows the principle: "no news is good news." Successful operations are silent. |
| Can I run multiple commands in the background? | Yes, append & to each. Use jobs to list background jobs and fg to bring one to the foreground. |
| What happens if I close the terminal with a background job running? | The background job may be terminated. Use nohup to prevent this. |
Wrap-Up
Understanding how Linux commands signal success and failure is foundational for every DevSecOps task. Remember: silence is success, and errors explain failure. Use & to keep your terminal free during long-running operations. Happy scripting!