pidof
Overview
The pidof
command finds process IDs (PIDs) of running programs. It displays the process IDs of named programs.
Syntax
pidof [options] program [program...]
Common Options
Option | Description |
---|---|
-s |
Single shot - return one PID |
-x |
Scripts too - return script PIDs |
-o omitpid |
Omit given PID |
-c |
Only return PIDs in chroot |
-n |
Newest process only |
-o |
Oldest process only |
-z |
Skip zombies |
-w |
Show PIDs with same name |
Key Use Cases
- Process identification
- Script automation
- Process monitoring
- Service management
- System administration
Examples with Explanations
Example 1: Basic Usage
pidof nginx
Find all nginx process IDs
Example 2: Single Process
pidof -s firefox
Find one firefox process ID
Example 3: Omit PID
pidof -o 1234 process_name
Find PIDs except 1234
Understanding Output
- Space-separated list of PIDs
- No output if process not found
- Error messages for:
- Invalid options
- Permission issues
- Process not found
Common Usage Patterns
Kill process:
kill $(pidof program)
Check if running:
if pidof program >/dev/null; then echo "Running"; fi
Monitor newest:
pidof -n program
Performance Analysis
- Fast execution
- Minimal system impact
- Process table lookup
- Name matching
- Multiple process handling
Additional Resources
Best Practices
- Verify process names
- Handle multiple PIDs
- Error checking
- Script safety
- Regular monitoring
Use Cases
- Service scripts
- Process control
- System monitoring
- Automation tasks
- Dependency checking