Your Scheduled Task PATH Is Nearly Empty, So It Can’t Find Your Command

Date:

You run the script by hand and it works perfectly. You put the exact same script on a schedule, a cron entry, a Task Scheduler job, a service, and it fails with command not found, or a tool that was clearly installed suddenly “isn’t there.” You run it by hand again to be sure: still fine. The script didn’t change, the machine didn’t change, the tool is installed. What changed is the one thing you can’t see in the script itself: the PATH the job runs with.

The short answer: a scheduled job does not inherit your interactive shell’s environment. When you type a command in a terminal, your PATH has been built up by your login profile, by tool installers that appended their directories, and by things you exported along the way, so bare commands like node, python, or a CLI you installed just resolve. A scheduled task PATH is nothing like that: cron, systemd, and Windows Task Scheduler each start the job with a minimal system PATH that doesn’t include your shell’s additions. So a bare command the scheduler can’t locate fails with command not found, even though the identical command works when you run it. The fix is to stop relying on an ambient PATH in automation: call tools by their absolute path, or set PATH explicitly inside the job.

The reason it’s so hard to believe is that by hand it works feels like proof the environment is fine. It isn’t proof of anything about the scheduler, because the shell you tested in and the environment the scheduler uses are two different worlds that merely share a disk.

A scheduled task doesn’t inherit your shell

Your interactive PATH is an accumulation. When you open a terminal, a login shell reads your profile (.bash_profile, .zshrc, a PowerShell profile) and whatever those scripts add to PATH. On top of that, every tool you installed that said “we added this to your PATH” wrote into your user environment. By the time you’re typing commands, PATH is long, and every tool you use day to day is on it. That richness is why a bare command name is all you ever need.

A scheduler starts from nothing like that. It doesn’t open a login shell, doesn’t read your interactive profile, and in many cases doesn’t even run as you. It launches your job with a small, fixed environment, and PATH is the part that bites, because the one thing your script quietly depends on is that bare commands resolve.

Why the scheduled task PATH is nearly empty

Each scheduler sets a deliberately minimal PATH, and none of them is the one your shell uses:

  • cron sets PATH to a short default, typically just /usr/bin:/bin, and nothing from your profile. The crontab(5) manual states outright that cron supplies a default PATH and that it is not your login PATH. A tool in /usr/local/bin or in a per-user directory is simply off the map.
  • systemd services run with a minimal PATH defined by the service manager, not your shell’s, unless the unit sets one. The unit also runs as whatever User= it specifies.
  • Windows Task Scheduler runs the task with the environment of the account the task is configured to run as, which carries the system PATH plus that account’s user PATH, not yours. If your tool went onto your user PATH and the task runs as SYSTEM or another user, your PATH additions are invisible to it. It’s the same family of surprise as the other quiet Windows toolchain mismatches, or an SSH key that’s loaded in the wrong agent, where the environment you typed into isn’t the one the job runs in.

The scheduler didn’t lose your tool. It was never told where to look, because the directory was only ever on your shell’s PATH.

The tell: make the task print the PATH it actually sees

You don’t have to guess which directories are missing. Make the scheduled job print its own PATH to a file, then compare it with your interactive one. The gap is the bug, in full.

# 1) Your interactive PATH - long, full of your tool directories:
echo "$PATH"                 # Linux / macOS
#   $Env:PATH                # PowerShell

# 2) The PATH the SCHEDULER sees - have the job write it somewhere you can read:
#    cron: add a temporary entry to the crontab -
* * * * * echo "$PATH" > /tmp/cron_path.txt
#    systemd: inspect the unit's environment -
#    systemctl show -p Environment <service>
#    Windows Task Scheduler: set the task action to -
cmd /c "echo %PATH% > C:\temp\task_path.txt"
Code language: Bash (bash)

Open the two side by side and the scheduled PATH is short, and the directory your tool lives in isn’t on it. That’s the entire fault, and it explains the misleading symptom: a green run by hand is a result that speaks only for the environment it ran in, not for the one the scheduler will use.

Where the command goes missing between your shell and the schedulerThe script is identical – only the PATH it inherits is different
1. By hand, the PATH is rich

Your profile and installers built a long PATH, so a bare 'node' or 'python' resolves – e.g. in /usr/local/bin.

2. On a schedule, it's minimal

cron, systemd, Task Scheduler start with a short default PATH, often just /usr/bin:/bin – and maybe a different account.

3. The bare command isn't found

The same script calls the tool by name, the thin PATH has no directory holding it, and the job fails: command not found.

The script didn't change, its PATH did. Call tools by absolute path, or set PATH explicitly inside the job.

Why it only breaks on a schedule, never by hand

The asymmetry is the whole confusion, and it’s exactly backwards from how a bug usually feels. Normally the hard case is the one you can’t reproduce; here the easy case, running it yourself, is the one that hides the defect. When you run the script by hand you bring your rich interactive PATH with you, so the bare commands resolve and everything passes. The scheduler brings its own thin PATH, so the same commands miss. Nothing about the script is wrong in isolation; it’s wrong only about the environment it assumed. This is the same shape as any defect where the thing works locally and fails where it actually runs: two environments that look identical until the one difference that matters surfaces.

And the failure is rarely a clean command not found. A script that calls a missing tool may get an empty result, a non-zero exit it doesn’t check, or a downstream error three steps later, so you end up debugging the wrong thing entirely, the data, the arguments, the server, when the real problem was that step one never ran.

Fix: stop depending on an ambient PATH

The durable cure is to make the job’s environment explicit, so it never relies on a PATH that happens to be set. There are two good ways, and they combine:

# A) Call the tool by its ABSOLUTE path - the surest fix, works under any scheduler:
/usr/local/bin/node /home/me/job.js
#   "C:\Program Files\nodejs\node.exe" C:\jobs\job.js        (Windows)

# B) Or set PATH explicitly inside the job, so bare commands resolve:
#    cron - put a PATH line at the top of the crontab (it overrides cron's default):
PATH=/usr/local/bin:/usr/bin:/bin
#    a shell script - export PATH before the first tool call:
export PATH="/usr/local/bin:$PATH"
#    systemd unit - set it in the service definition:
#    Environment="PATH=/usr/local/bin:/usr/bin:/bin"
Code language: Bash (bash)

On Windows there’s one extra move: if the task runs as SYSTEM or a different user, put the tool’s directory on the system PATH rather than your personal user PATH, because the task will never see your user PATH. Better still, give the task the full path to the executable and sidestep PATH entirely.

Whichever you choose, the principle is the same: automation should carry its own environment, not borrow one. A job that names its tools by absolute path, or sets its own PATH before the first command, runs the same way no matter who launches it or how empty the inherited environment is.

The short version

A scheduled job doesn’t inherit your interactive shell’s PATH. Your terminal’s PATH was built up by your login profile and by tool installers, so bare commands resolve; cron, systemd, and Windows Task Scheduler instead start the job with a minimal system PATH, and under cron it’s often just /usr/bin:/bin. So the identical script that works when you run it by hand fails under the scheduler with command not found, because the directory holding the tool was only ever on your shell’s PATH. Confirm it by having the scheduled job print its own PATH to a file and comparing. Fix it by not depending on an ambient PATH at all: call tools by their absolute path, or set PATH explicitly inside the job.

Frequently Asked Questions

Why does my script work manually but fail as a scheduled task?

Because running it by hand uses your interactive shell’s PATH, which your login profile and tool installers have filled with the directories your tools live in, and a scheduled task uses a different, minimal PATH set by the scheduler. The script is identical; the environment isn’t. When a bare command like node or a CLI you installed resolves by hand, it’s because your shell’s PATH points at it; under the scheduler that directory isn’t on PATH, so the command isn’t found. Have the scheduled job print its own PATH to a file and compare it with your shell’s to see the missing directory.

What PATH does cron actually use?

A short default, not your login PATH. The crontab(5) manual documents that cron sets PATH to a minimal value, commonly /usr/bin:/bin, with none of the additions your interactive profile makes. That’s why a cron job can’t find a tool in /usr/local/bin or in a per-user install directory even though you use it constantly in a terminal. You can override it by putting a PATH= line at the top of the crontab, or by calling tools by their absolute path in the job.

Is this the same reason a Windows Task Scheduler job can’t find my command?

Yes, with a Windows twist. Task Scheduler runs the task under a specific account, and the task gets that account’s environment: the system PATH plus that account’s own user PATH. If you installed a tool and it was added to your user PATH, but the task runs as SYSTEM or another user, your PATH additions aren’t present, so the command isn’t found. Put the tool on the system PATH, or give the task the full path to the executable, and it resolves regardless of which account runs it.

What’s the most reliable fix across cron, systemd, and Task Scheduler?

Call tools by their absolute path. It’s the one fix that works identically everywhere, because it doesn’t depend on PATH at all, the job names exactly which executable to run. Setting PATH explicitly inside the job (a PATH= line in the crontab, an export PATH at the top of the script, an Environment= line in a systemd unit) is the other good option and keeps bare command names readable. The anti-pattern is assuming the scheduler inherited the PATH you see in a terminal; it never does.

Share post:

Subscribe

Popular

More like this
Related

Can AI CLI Conversation History Leak Answers Into a Blind Test?

You use an AI command-line tool to grade or answer hundreds of questions, one fresh run each. Then you find out it keeps every conversation on disk, answer keys included. Is run 200 quietly reading run 199?

The Code Snippets 403 That Comes From Your Host’s Firewall, Not WordPress

You change a few lines in a snippet, press Save, and get "Could not update snippet. Request failed with status code 403". Press Save on the unchanged snippet and it works. Your login is fine. Something between you and WordPress is reading the code itself.

Why a Flaky Test Passes Alone and Fails When the Machine Is Busy

Run the test by itself and it passes, every time. Run the whole suite in parallel and it fails, but only some days, and its failure looks exactly like a real bug. In our case the code was fine. The test was waiting on a clock.

Why Multisite Subsite Registration Sends Visitors to the Main Site

You put a "create an account" link on one site of a WordPress multisite. Visitors who click it land on the main site, which either says "Registration has been disabled" or signs them up there instead.