A command that succeeds in an interactive shell can fail under cron because cron starts with a smaller environment and a different working directory. Make the required paths and environment explicit in a reviewed wrapper.
IN THIS ARTICLE
Gather the details you will need
Record the interactive command and the directory from which it succeeds. Identify required configuration files, executables and environment variables without copying secrets into the public website or email output.
01 Fix a cron job that works only from an interactive shell
- Inspect the cron command and replace relative interpreter and script paths with their verified absolute paths.
- If the application expects a project working directory, use a controlled
cd /absolute/project/path && /absolute/commandsequence so the command runs only if changing directory succeeds. - Put complex logic in a private script with an appropriate interpreter line and permissions. Keep secrets in the application’s protected configuration rather than command-line arguments.
- Set only required environment variables and use the application’s documented configuration-loading method. Do not assume interactive shell profile files will be read.
- Capture output privately and compare the next scheduled run with the interactive test under the same hosting identity.
02 Example: make the working directory explicit in a private wrapper
For an application whose scheduled PHP script must run from its project folder, a small wrapper can make that dependency visible. The following is a template: replace CPANEL_USER, the project folder, PHP executable and script with the verified values required by your application.
#!/bin/sh
cd /home/CPANEL_USER/project || exit 1
exec /absolute/path/to/php /home/CPANEL_USER/project/cron.php
The command stops if it cannot enter the project directory. Otherwise, it starts the required PHP script from that directory. Retain any arguments required by the application. Store the wrapper outside public web directories, give it the permissions appropriate to its execution method, and schedule its verified absolute path using the supported interpreter.
Keep the application's secrets in its protected configuration. Capture scheduled output in the private log or notification route you chose, then check the application's actual last-run result. Test manually only when that task is safe to repeat.
Confirm the expected result
The task should locate its configuration and data files consistently regardless of the shell directory from which it is launched. Confirm output files appear in the intended private location.
If the problem continues
If the script still fails, inspect its interpreter, line endings, path permissions and missing environment variables. Avoid sourcing an entire unrelated interactive profile merely to hide the dependency.
Should I put every variable in the cron command?
No. Long commands are harder to review and can expose secrets in process listings or administration screens. Use a short scheduled entry that invokes a protected, documented application wrapper.