Loading...

Lab 35: Processes, Jobs, and Scheduling

Investigate a resource-heavy process, control it using job management, and terminate it safely when needed. Then schedule follow-up actions using one-time and recurring schedulers.

core troubleshooting services

Scenario

A developer reports that the dev server keeps “freezing” during their data run. You suspect a runaway job is consuming memory, and you need to identify it, control it, and prevent similar issues by scheduling monitoring and cleanup tasks.

Operator context

This is the workflow you use when you need evidence first, then controlled intervention: observe, isolate, stop, then automate guardrails.

Objective

  • List processes and identify memory-heavy offenders.
  • Start a long-running job in the background.
  • Inspect jobs, foreground them, and suspend them safely.
  • Terminate a stuck job using SIGTERM.
  • Schedule a one-time cleanup job with at.
  • Create a daily cron job for monitoring.

What You’ll Practice

  • Process listing and sorting with ps.
  • Job control primitives: &, jobs, fg, Ctrl+Z.
  • Safe termination using kill (SIGTERM).
  • One-time scheduling with at.
  • Recurring scheduling with crontab.

Walkthrough

Step 1 : Display all active processes sorted by memory usage.
Command
ps -eo pid,user,%mem,comm --sort=-%mem

This gives you an evidence-first view of resource usage so you can identify the likely offender before you touch anything.

PID   USER     %MEM COMMAND
2387  alice    13.2 code
2194  alice     9.8 firefox
2381  dev       6.9 python3
Step 2 : Start the data job in the background.
Command
python3 data_job.py &

Appending & starts the job in the background and returns control of the shell immediately.

[1] 2381
Step 3 : List all background jobs.
Command
jobs

jobs shows processes managed by your current shell session (not system-wide processes).

[1]+  Running                 python3 data_job.py &
Step 4 : Bring the job to the foreground, then suspend it.
Command
fg %1

Foregrounding the job allows interactive control. In a real session, you suspend it with Ctrl+Z to stop it without killing it.

python3 data_job.py
[1]+  Stopped                 python3 data_job.py
Step 5 : Kill the stuck job with SIGTERM.
Command
kill %1

Default kill sends SIGTERM, which is the “polite” termination signal. If a process ignores it, that is when you consider escalation, but SIGTERM is the correct first move in most admin workflows.

# No output on success
Step 6 : Schedule a cleanup script to run once in 2 minutes.
Command
echo '/usr/local/bin/cleanup.sh' | at now + 2 minutes

at is for one-off scheduling. This is useful for deferred cleanup, delayed restarts, or running a script once after a short buffer.

job 4 at Sat Jul 20 21:59:00 2025
Step 7 : Create a cron job to run monitor.sh every day at midnight.
Command
(crontab -l; echo '0 0 * * * /usr/local/bin/monitor.sh') | crontab -

This preserves existing entries, then appends the new daily schedule. Midnight daily is a common baseline for checks, reports, and housekeeping tasks.

# crontab installs silently on success

Reference

  • ps -eo ... --sort=-%mem: Lists processes with selected fields, sorted by memory usage.
  • jobs: Lists background/managed jobs in the current shell session.
  • fg %N: Brings job %N to the foreground.
  • Ctrl+Z: Suspends the current foreground job (stops it).
  • kill %N: Sends a signal to job %N (default SIGTERM).
  • at now + 2 minutes: Schedules a one-time job to run in two minutes.
  • crontab -l / crontab -: Reads current cron entries and installs a new crontab.