The Art of Linux Process Management

Keeping Your Commands in Line

Hey there,

Welcome to another edition of our bi-weekly newsletter!

You already know we’re all about making your cloud-native and DevOps journey less stressful and more productive.

Today, we’re talking about a core skill that separates the pros from the rest: Linux process management.

Whether you're just stepping into the world of Linux or you've been managing infrastructure for years, your ability to control how your commands execute is fundamental. We know jobs run in the foreground by default, which means your single shell access is held hostage till the job is done. 

That doesn’t sound like much of a problem if patience is your strong suit. Well…until you’re having to wait for hours😅.

Meme Source: EverythingDevOps

We’ll be highlighting some sets of commands to navigate and manage processes, so that you’re not like the guy above. 

Let’s get into it. 

Essential Linux Process Management Commands

A solution to freeing up your shell immediately is running jobs in the background. This way, you’re able to execute other commands and multitask without interruption while making efficient use of your time.  Some commands include: 

  • Sending a task to the background (&): Add (&) to a command when you launch it (e.g., sleep 300 &). This starts the process in the background from the beginning, giving you your terminal prompt back instantly.

  • Bringing a background job to the foreground (fg): If you need to interact with a background process, the fg command allows you to pull it back to the forefront. You'll refer to it by its job number, assuming the number was 1(fg %1).

  • Suspending a running foreground job (CTRL + Z): Pressing CTRL + Z while a foreground command is running will put the process into a "stopped" mode, temporarily pausing its execution and freeing up your terminal so you can run other commands.

  • Resuming suspended jobs (bg): Once a foreground job is suspended (stopped), the bg command can be used to resume its execution and move it to the background using bg %1.

  • Identifying job status (+ and -): The +attached to the job number indicates the "current" job, and the - indicates the "previous" job, helping you quickly identify their status. 

It’s great to know how to manage processes efficiently within your active terminal session. But what about those critical tasks that need to run continuously, even after you've closed your terminal window or lost your connection? What next? 

Keeping Jobs Alive Beyond the Terminal Session

By default, any job associated with a terminal session will be terminated when the terminal window is closed because the terminal sends a "hang-up" (HUP) signal to all processes it started upon exit. 

This is where commands like nohup and disown come in handy. 

  • nohup (No Hang Up): This command allows you to launch a process in such a way that if you start a command with nohup (e.g., nohup sleep 300 &), it will continue running in the background even if you close your terminal window or lose your network connection. It's ideal when you know before starting a long job that you'll need it to continue.

  • disown: What if you forgot to use nohup? The disown command comes to the rescue! If you have a job already running in the background, disown allows you to disconnect it from the current session. This way, when you close your terminal, it will continue its execution.

These are just a few – ok, not few 😅– commands to help you get started. If you’re ready to gain full control, access the tutorial: ‘Linux Background and Foreground Process Management’ alongside helpful recommendations here

If you found today’s edition helpful, share this link with your colleagues and fellow DevOps engineers. 

Until next time. 

Divine Odazie
Founder of EverythingDevOps