Linux pidof Command

Linux<span>pidof</span> Command

1. Overview

In a Linux system, a process is an instance of a program that is currently running. Each process has a unique Process ID (PID). When performing system administration, performance debugging, resource control, and security protection, it is often necessary to know the PID corresponding to a specific program.<span>pidof</span> is a command used to obtain the PID of a specific program.

<span>pidof</span> command is part of the <span>sysvinit-tools</span> or <span>procps</span> package, which returns all PIDs for a specified program name.

2. Basic Syntax and Option Description

2.1 Basic Syntax

pidof [options] program_name

2.2 Common Options

Option Description
<span>-s</span> Returns only one PID (the first found)
<span>-c</span> Returns only processes that belong to the same session as the current process
<span>-x</span> Also searches for scripts (e.g., shell scripts)
<span>-o PID</span> Ignores the specified PID (can be used multiple times)
<span>-q</span> Quiet mode, does not output any information, only returns the exit code
<span>-h</span> or <span>--help</span> Displays help information
<span>-V</span> or <span>--version</span> Displays version information

3. Common Usage Examples

3.1 Find a Single Process PID

$ pidof sshd1234

3.2 Return the First Matching PID (using -s)

$ pidof -s sshd1234

3.3 Ignore Specified PID

$ pidof sshd1234 5678
$ pidof -o 1234 sshd5678

3.4 Find the PID of a Script (-x)

$ ./test.sh &amp;$ pidof -x test.sh4321

4. Advanced Usage

4.1 Batch Termination of Certain Processes with kill

kill -9 $(pidof firefox)

4.2 Multi-Process Check Script

#!/bin/bash
if pidof -s myapp &gt; /dev/null; then
    echo "myapp is running"
else
    echo "myapp is not running, restarting"
    /usr/bin/myapp &amp;
fi

4.3 Use -x to Check Script Running Status

pidof -x backup.sh

4.4 Combine with watch to Monitor PID in Real-Time

watch -n 2 'pidof nginx'

5. Comparison with Other Commands

5.1 Differences Between pidof and pgrep

Item pidof pgrep
Matching Method Program file name Supports regular expression matching
Output Content PID PID, optional process name
Supports Script Recognition Yes (-x) No
Cross-User Capability Yes Defaults to only check the current user’s processes, can specify with <span>-u</span>
Suitability for Scripts Simpler More powerful

Example Comparison:

# Using pidof
pidof nginx
# Using pgrep
pgrep -f "nginx: worker process"

6. Practical Cases of pidof

6.1 Check the Running Status of Multiple Services

services=("nginx" "mysqld" "sshd")
for svc in "${services[@]}"; do
    if pidof -s "$svc" &gt; /dev/null; then
        echo "$svc is running normally"
    else
        echo "$svc is not running"
    fi
done

6.2 Script Auto-Daemon Mechanism

#!/bin/bash
SCRIPT="/opt/jobs/daily_sync.sh"
if ! pidof -x "$SCRIPT" &gt; /dev/null; then
    echo "Task is not running, starting"
    bash "$SCRIPT" &amp;
else
    echo "Task is already running"
fi

6.3 Prevent Duplicate Execution in Scheduled Tasks

# crontab task: * * * * * /usr/local/bin/check_job.sh

7. Usage Limitations and Precautions

  1. Depends on the /proc filesystem
  2. Program name must be exact
  3. Does not support viewing processes isolated by containers
  4. Risk of conflict with aliases
  5. Script names cannot include paths

8. Advanced Shell Usage Techniques

8.1 Check and Record Running PID

pid=$(pidof -s myapp)
if [ -n "$pid" ]; then
    echo "myapp is running, PID: $pid"
    echo $pid &gt; /var/run/myapp.pid
else
    echo "myapp is not running"
fi

8.2 One-Click Termination of All Specified Processes (including scripts)

for prog in "nginx" "backup.sh" "worker"; do
    kill -9 $(pidof -x $prog 2&gt;/dev/null)
done

8.3 Combine with systemctl

# If there is no systemd service
if ! pidof -s nginx &gt; /dev/null; then
    systemctl start nginx
fi

9. Source Code Analysis

In the source code of the <span>sysvinit</span> toolset, the core process of <span>pidof</span> is as follows:

  1. Open the <span>/proc</span> directory.
  2. Traverse all numeric directories (i.e., process PIDs).
  3. For each PID, perform a <span>readlink</span> on <span>/proc/[pid]/exe</span>.
  4. Obtain the executable file name and compare it with the target program name.
  5. If matched, output the PID.

10. Summary and Recommendations

Advantages Description
Concise Single command to check PID, few parameters, easy to use
Strong Compatibility Almost all mainstream distributions support it by default
Supports Script Recognition <span>-x</span><span> is its unique advantage</span>
Supports Excluding Specified PIDs Convenient for conditional matching

Recommended Usage Scenarios

  • Monitor whether processes are alive when writing shell scripts
  • Prevent duplicate execution in crontab tasks
  • Simple daemon process implementation
  • Batch termination of certain processes
  • Introduction to process tools for Linux beginners

Appendix: Related Command Recommendations

Command Description
<span>ps</span> Displays current processes
<span>top</span>/<span>htop</span> Dynamically view process information
<span>pgrep</span>/<span>pkill</span> Use regular expressions to match processes
<span>kill</span> Terminate specified PID
<span>lsof</span> View files opened by processes
<span>systemctl</span> systemd service management

Leave a Comment