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
and15
, but it will stop on signal1
. - 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
πͺ 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
Example:
trap "echo Caught SIGINT!" SIGINT
This command prints a message when SIGINT
is received.
Advanced Tips:
- Modify traps at any point in the script.
- Disable traps using:
trap -- SIGNAL
π Keep Processes Alive with nohup
The nohup
command keeps processes running even after the terminal closes.
How It Works:
nohup ./myscript.sh &
- The process runs in the background.
- Output is stored in
nohup.out
by default.
Viewing Background Jobs:
jobs -l
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
π 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
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
Viewing and Managing Jobs:
atq # View queued jobs
atrm <job_number> # Remove a job
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:
- Edit the Cron Table:
crontab -e
- Define Tasks:
0 5 * * * /path/to/script.sh
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)