Operations 6 min read

Why a Crontab Script Miscounts Processes: PATH and pidof Gotchas

A Linux crontab job that should run a monitoring script only when a process is absent repeatedly launches the process because the cron environment lacks the proper PATH, causing pidof to fail silently and always return zero.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why a Crontab Script Miscounts Processes: PATH and pidof Gotchas

Problem Origin

A program needs to restart automatically after a crash, so a Linux crontab entry runs a script every minute to check whether the video_checkup process is running. When the script is executed manually in a shell, it works, but when run by cron it repeatedly starts the process, creating many instances.

Problem Description

The monitoring script /home/watch.sh contains:

#!/bin/sh
shell_log_file=/home/start.log
pid_count=`pidof video_checkup | wc -w`
path=$(cd "$(dirname "$0")"; pwd)
run_command="${path}/video_checkup"
config_path="${path}/config.json"
if [ $pid_count -eq 0 ]; then
    echo `date +%Y-%m-%d_%H:%M:%S` " run $run_command $config_path" >> $shell_log_file
    $run_command $config_path
else
    echo `date +%Y-%m-%d_%H:%M:%S` " video_checkup already running" >> $shell_log_file
fi

The crontab line added is: * * * * * /home/watch.sh > /dev/null 2>&1 Despite the logic, the video_checkup process count keeps increasing because pid_count is always evaluated as zero.

Analysis

Debugging showed that the conditional if [ $pid_count -eq 0 ] is always true when the script runs under cron. Logging the environment with export revealed that PATH in the cron job is limited to /usr/bin:/bin, while an interactive shell has a much richer PATH (e.g.,

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

).

The pidof command resides in /sbin. Because /sbin is missing from the cron PATH, the command is not found. The failure is silent when piped to wc -w; wc receives no input and returns 0, so pid_count becomes zero every time.

Solution

Explicitly set a proper PATH at the beginning of watch.sh so that pidof can be located:

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"

After adding this line, the script correctly detects the running video_checkup process and stops launching duplicate instances.

This case illustrates how differences between the cron environment and an interactive shell can cause subtle bugs in automation scripts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

crontabpidofshell-script
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.