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 psPID TTY TIME CMD1234 pts/0 00:00:00 bash5678 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.shin the background, redirects standard output tooutput.log, and redirects standard error to the same file.nohup.outis used if you don’t specify the output file. -
kill: Terminates a process.Terminal window ps aux | grep my_processuser 12345 0.0 0.1 5000 2000 ? S 10:00 0:00 ./my_processTerminal window kill 12345Explanation: First, find the PID of
my_processusingps auxandgrep. Then, usekillto terminate the process with PID 12345.
4. Common Options
-
ps:-eora: 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 auxis a common and useful command.--sort <key>: Sort processes by a specific key (e.g.,--sort %cpufor CPU usage,--sort rssfor 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:-9or-KILL: Forcefully terminate a process (SIGKILL). Use as a last resort. Can cause data loss.-15or-TERM: Gracefully terminate a process (SIGTERM). This is the default signal sent bykill. 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 auxandgrep, then extracts the PIDs usingawk(column 2), and finally passes those PIDs tokill.WARNING: Be very careful with this command, as it can kill unintended processes if the
greppattern is too broad. Double-check the output ofps aux | grep "my_process"before running thekillcommand. -
Restarting a process with
nohupandkill:Terminal window kill -HUP $(pidof my_process) # Send HUP signal to restart (graceful restart)# ORkill $(pidof my_process) && nohup ./my_process > output.log 2>&1 & # Terminate and restartExplanation: Uses
pidofto find the process ID ofmy_process. Then, either sends aHUPsignal (if the process handles it for graceful restart) or terminates the process and restarts it usingnohup. -
Monitoring a process’s resource usage:
Terminal window ps -p <PID> -o %cpu,%mem,rss,vsz,etime,argsExplanation: 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
xargsfor complex process management:Terminal window ps aux | grep "java" | awk '{print $2}' | xargs kill -9Explanation: Finds all Java processes, extracts their PIDs, and then uses
xargsto pass those PIDs tokill -9. This is a powerful but potentially dangerous way to kill multiple processes.
6. Tips & Tricks
-
Use
toporhtopfor real-time process monitoring: These commands provide a dynamic view of system resource usage and process activity.htopis often preferred for its more user-friendly interface. -
Use
pgrepandpkillfor simpler process finding and killing:Terminal window pgrep my_process # Find the PID of my_processpkill my_process # Kill all processes matching the name "my_process"Explanation:
pgrepfinds PIDs by name or other attributes.pkillsends 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
killsucceeds (returns 0), the firstechocommand is executed. Ifkillfails (returns non-zero), the secondechocommand 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+Zto suspend it, then usebgto resume it in the background.Terminal window ./my_long_script.sh # Start the script in the foregroundCtrl+Z # Suspend the scriptbg # 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 theSIGTERMsignal. Try usingkill -9 <PID>as a last resort. Be aware of the potential for data loss. nohupcommand 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,
xargsis 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 likesystemctl 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 theps auxcommand 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.