Skip to content

Process Management (ps, jobs, nohup, kill)

Category: Intermediate Linux Commands
Type: Linux Commands
Generated on: 2025-07-10 03:08:25
For: System Administration, Development & Technical Interviews


Process Management Cheatsheet (Linux - Intermediate)

Section titled “Process Management Cheatsheet (Linux - Intermediate)”

This cheatsheet covers essential process management commands in Linux: ps, jobs, nohup, and kill. It’s designed for both system administrators and developers who need to monitor, control, and manage processes in production environments.

1. Command Overview

  • ps (Process Status): Displays information about active processes. Use it to identify processes, their IDs (PIDs), resource usage, and status.
  • jobs: Lists background jobs associated with the current shell. Useful for managing processes started in the background.
  • nohup (No Hang Up): Runs a command that will continue to execute even after you log out or close the terminal. Essential for long-running tasks.
  • kill: Sends signals to processes. Most commonly used to terminate a process, but can also be used for other purposes like pausing or resuming.

2. Basic Syntax

  • ps [options]
  • jobs [options]
  • nohup command [arguments] & (The & puts the command in the background)
  • kill [options] PID(s)

3. Practical Examples

  • ps: Shows processes associated with the current user and terminal.

    Terminal window
    ps
    PID TTY TIME CMD
    1234 pts/0 00:00:00 bash
    5678 pts/0 00:00:00 ps
  • jobs: Lists background jobs.

    Terminal window
    sleep 60 &
    jobs
    [1]+ Running sleep 60 &
  • nohup: Runs a command that continues after logout.

    Terminal window
    nohup ./my_long_script.sh > output.log 2>&1 &

    Explanation: Runs my_long_script.sh in the background, redirects standard output to output.log, and redirects standard error to the same file. nohup.out is used if you don’t specify the output file.

  • kill: Terminates a process.

    Terminal window
    ps aux | grep my_process
    user 12345 0.0 0.1 5000 2000 ? S 10:00 0:00 ./my_process
    Terminal window
    kill 12345

    Explanation: First, find the PID of my_process using ps aux and grep. Then, use kill to terminate the process with PID 12345.

4. Common Options

  • ps:

    • -e or a: Show all processes.
    • -f: Full listing (more details).
    • -u <user>: Show processes owned by a specific user.
    • aux: Combination of options for a more comprehensive listing (user, PID, CPU%, MEM%, command). ps aux is a common and useful command.
    • --sort <key>: Sort processes by a specific key (e.g., --sort %cpu for CPU usage, --sort rss for memory usage).
  • jobs:

    • -l: List process IDs in addition to the normal information.
    • -p: List process IDs only.
    • -r: Restrict output to running jobs.
    • -s: Restrict output to stopped jobs.
  • nohup:

    • (None particularly common beyond the implicit redirect)
  • kill:

    • -9 or -KILL: Forcefully terminate a process (SIGKILL). Use as a last resort. Can cause data loss.
    • -15 or -TERM: Gracefully terminate a process (SIGTERM). This is the default signal sent by kill. Allows the process to clean up before exiting.
    • -HUP: Hangup signal. Often used to restart daemons/servers after configuration changes. e.g., kill -HUP <pid>
    • -l : List all signal names. kill -l

5. Advanced Usage

  • Killing multiple processes:

    Terminal window
    kill $(ps aux | grep "my_process" | awk '{print $2}')

    Explanation: This command finds all processes matching “my_process” using ps aux and grep, then extracts the PIDs using awk (column 2), and finally passes those PIDs to kill.

    WARNING: Be very careful with this command, as it can kill unintended processes if the grep pattern is too broad. Double-check the output of ps aux | grep "my_process" before running the kill command.

  • Restarting a process with nohup and kill:

    Terminal window
    kill -HUP $(pidof my_process) # Send HUP signal to restart (graceful restart)
    # OR
    kill $(pidof my_process) && nohup ./my_process > output.log 2>&1 & # Terminate and restart

    Explanation: Uses pidof to find the process ID of my_process. Then, either sends a HUP signal (if the process handles it for graceful restart) or terminates the process and restarts it using nohup.

  • Monitoring a process’s resource usage:

    Terminal window
    ps -p <PID> -o %cpu,%mem,rss,vsz,etime,args

    Explanation: Shows CPU usage (%cpu), memory usage (%mem), resident set size (RSS), virtual memory size (VSZ), elapsed time (etime), and arguments (args) for the process with the specified PID.

  • Using xargs for complex process management:

    Terminal window
    ps aux | grep "java" | awk '{print $2}' | xargs kill -9

    Explanation: Finds all Java processes, extracts their PIDs, and then uses xargs to pass those PIDs to kill -9. This is a powerful but potentially dangerous way to kill multiple processes.

6. Tips & Tricks

  • Use top or htop for real-time process monitoring: These commands provide a dynamic view of system resource usage and process activity. htop is often preferred for its more user-friendly interface.

  • Use pgrep and pkill for simpler process finding and killing:

    Terminal window
    pgrep my_process # Find the PID of my_process
    pkill my_process # Kill all processes matching the name "my_process"

    Explanation: pgrep finds PIDs by name or other attributes. pkill sends signals to processes matching a name.

  • Chain commands with && or || for conditional execution:

    Terminal window
    kill $(pidof my_process) && echo "Process terminated successfully" || echo "Process termination failed"

    Explanation: If kill succeeds (returns 0), the first echo command is executed. If kill fails (returns non-zero), the second echo command is executed.

  • Use process groups to manage related processes: Processes can be grouped together, allowing you to send signals to the entire group at once. This is often used for complex applications with multiple child processes. kill -<signal> -<process_group_id> (Note the negative sign before the process group ID)

  • Backgrounding a running process: If you start a process in the foreground and then realize you want it to run in the background, you can press Ctrl+Z to suspend it, then use bg to resume it in the background.

    Terminal window
    ./my_long_script.sh # Start the script in the foreground
    Ctrl+Z # Suspend the script
    bg # Resume the script in the background

7. Troubleshooting

  • “Permission denied” error when killing a process: You may not have sufficient privileges to kill the process. Try using sudo kill <PID>.
  • Process doesn’t terminate after using kill: The process may be ignoring the SIGTERM signal. Try using kill -9 <PID> as a last resort. Be aware of the potential for data loss.
  • nohup command still terminates when you log out: Make sure you’re redirecting both standard output and standard error (e.g., nohup command > output.log 2>&1 &). Also, check if the process itself has an internal mechanism that causes it to terminate after a certain period of inactivity or connection loss.
  • Too many arguments to kill: If trying to kill many processes at once, xargs is useful. Also, ensure you are only passing PIDs to the kill command.

8. Related Commands

  • top / htop: Real-time process monitoring.
  • nice: Adjusts the priority of a process. nice -n 10 ./my_process (lower nice value = higher priority, requires root for negative values).
  • renice: Changes the priority of a running process. renice -n 10 -p <PID>
  • screen / tmux: Terminal multiplexers that allow you to detach and reattach to terminal sessions. Useful for running long-running processes without keeping a terminal window open.
  • systemd: A system and service manager (modern replacement for init). Used to manage services and processes in a more structured way. Commands like systemctl start <service>, systemctl stop <service>, systemctl status <service>.
  • watch: Execute a program periodically, showing output fullscreen. watch -n 1 ps aux | grep my_process - will run the ps aux command every second and highlight changes.
  • lsof: Lists open files. Can be used to identify which processes are using a particular file or resource. lsof /path/to/file

This cheatsheet provides a solid foundation for managing processes in Linux. Remember to always exercise caution when using commands like kill -9, and double-check your commands before executing them, especially when dealing with critical production systems. Understanding the signals and how processes react to them is crucial for effective process management.