DEV Community

Theerej C
Theerej C

Posted on

Mastering Linux Process Management Like a Pro πŸš€

Linux process management is a critical skill for developers and system administrators. Understanding how processes work, how to control them, and how to automate tasks can make you a command-line ninja. Let’s explore signals, job control, process priorities, job scheduling, and more!


🧠 Understanding Linux Signals

Linux uses signals to communicate with running processes. Signals are predefined messages that notify processes about system events, such as termination requests or completion notices.

Common Linux Signals:

  • SIGINT (2): Interrupt from the keyboard (CTRL+C), stops a process.
  • SIGTSTP (20): Stop signal from the keyboard (CTRL+Z), pauses a process but keeps it in memory.
  • SIGHUP (1): Hang-up signal, often used to restart processes.

Bash Shell Defaults:

  • By default, Bash ignores signals 3 and 15, but it will stop on signal 1.
  • It also passes SIGHUP to child processes when the terminal closes.

Viewing Stopped Jobs:

Use the ps command to see all running or stopped processes:

ps -aux
Enter fullscreen mode Exit fullscreen mode

πŸͺ Trapping Signals

Trapping signals allows you to redefine how your scripts respond to signals. Use the trap command to intercept signals and perform custom actions.

Syntax:

trap "command" SIGNAL
Enter fullscreen mode Exit fullscreen mode

Example:

trap "echo Caught SIGINT!" SIGINT
Enter fullscreen mode Exit fullscreen mode

This command prints a message when SIGINT is received.

Advanced Tips:

  • Modify traps at any point in the script.
  • Disable traps using:
trap -- SIGNAL
Enter fullscreen mode Exit fullscreen mode

πŸš€ Keep Processes Alive with nohup

The nohup command keeps processes running even after the terminal closes.

How It Works:

nohup ./myscript.sh &
Enter fullscreen mode Exit fullscreen mode
  • The process runs in the background.
  • Output is stored in nohup.out by default.

Viewing Background Jobs:

jobs -l
Enter fullscreen mode Exit fullscreen mode

This lists all running background jobs along with their process IDs.


πŸ” Job Management

Restarting Stopped Jobs:

  • bg: Restarts a job in the background.
  • fg: Brings a job back to the foreground.

Example:

bg %1  # Resume job 1 in the background
fg %1  # Resume job 1 in the foreground
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Process Priorities

Understanding Priorities:

Linux assigns CPU time based on priorities, represented by integers from -20 (highest) to +19 (lowest). Lower values get more CPU time.

Commands:

  • nice: Launch a process with a specific priority.
  • renice: Change the priority of an existing process.

Examples:

nice -n -5 ./heavy_process.sh
renice -p 12345 -n 10
Enter fullscreen mode Exit fullscreen mode

Rules:

  • Regular users can only lower process priority.
  • Root can adjust priority in either direction.

⏰ Job Scheduling Like a Boss

One-Time Job Scheduling with at

  • Use the at command for one-time task scheduling.

Syntax:

at -f myscript.sh now +1 hour
Enter fullscreen mode Exit fullscreen mode

Viewing and Managing Jobs:

atq  # View queued jobs
atrm <job_number>  # Remove a job
Enter fullscreen mode Exit fullscreen mode

Note:

  • Default output from at is emailed to the user. Use -M to suppress the email.

Recurring Jobs with cron

  • Use cron for repeated task scheduling.

Steps:

  1. Edit the Cron Table:
   crontab -e
Enter fullscreen mode Exit fullscreen mode
  1. Define Tasks:
   0 5 * * * /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

This runs the script every day at 5:00 AM.

System Cron Directories:

  • /etc/cron.daily (Daily tasks)
  • /etc/cron.weekly (Weekly tasks)

Cron's Limitation:

  • Cron assumes the system runs 24/7. Tasks missed during downtime aren’t executed.

Fix: Use anacron

  • Use anacron to ensure tasks run after system boot if missed.

πŸ”₯ Ready to dominate Linux process management? Share your tips or experiences in the comments below! πŸ’»

Top comments (0)