Why Does My Cron Job Always Restart My Process? The Hidden PATH Pitfall
An investigation reveals that a cron‑scheduled script repeatedly launches a process because the limited PATH in cron’s environment prevents the pidof command from running, causing the script to misinterpret the process count as zero, and the fix is to set the correct PATH.
Problem Origin
The author wanted a program to auto‑restart after a crash, so they added a cron job that runs a monitoring script every minute to check if the video_checkup process is running and start it if not.
Problem Description
The script /home/watch.sh contains the following logic:
#!/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
fiThe cron entry added with crontab -e is: * * * * * /home/watch.sh > /dev/null 2>&1 In practice the video_checkup process multiplied rapidly because the script thought the process was absent each minute.
Problem Analysis
Debugging showed that the condition if [ $pid_count -eq 0 ] was always true; $pid_count was always zero even though the process was running. Logging the environment variables revealed that the PATH inside cron was only /usr/bin:/bin, whereas an interactive shell had a much longer PATH that includes /sbin where the pidof binary resides ( /sbin/pidof).
Because pidof was not found in cron’s PATH, the command failed silently; the failure produced no output for wc -w, which then returned 0, making the script think no process existed.
Problem Solution
Adding the full PATH from the interactive shell to the script fixes the issue. Insert at the beginning of watch.sh:
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"With the correct PATH, pidof is found, the process count is accurate, and the script behaves as intended.
This simple environment‑variable mismatch caused hours of debugging, illustrating how seemingly trivial differences between cron and interactive shells can lead to hidden bugs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
