Decoding Supervisord's "Exit Status 1" with PHP Scripts
When you see exit status 1; not expected in your supervisord logs, it is a binary signal: the process terminated before supervisord could mark it as running. In the case of a PHP script that runs perfectly via CLI, the issue almost invariably lies in the environment context or how the child process is spawned, rather than the code itself.
The logs provided show a classic "FATAL state" scenario where supervisord has given up after multiple rapid retries. The configuration snippet reveals several immediate red flags that often trip up developers transitioning from local testing to production orchestration.
The Root Cause: Missing PHP Binary Path
The most critical misconfiguration in the provided setup is the command line combined with the environment variable.
[program:worker1]
command=php myScript.php
directory=/home/path/to/script/
user=root
...
environment=PATH="/usr/bin"
When you run php myScript.php directly in your terminal, your shell looks for the php executable in directories listed in the system's PATH environment variable (usually /usr/local/bin, /usr/bin, etc.). However, when supervisord spawns a process, it does not inherit the interactive shell's modified environment. It starts with a minimal environment.
The line environment=PATH="/usr/bin" explicitly tells PHP to look only in /usr/bin for its binaries. While this might work if your specific installation of PHP is strictly inside /usr/bin, many modern installations place the binary in /usr/local/bin or /usr/bin/php. If the php command cannot be resolved within that constrained path, the OS returns exit code 127 ("command not found").
Supervisord interprets any non-zero exit code as a failure. Since 1 is the generic error code for "unexpected termination," supervisord logs it as such and immediately attempts to restart the process. This loop continues until the max_starts limit (default is often 3 or 5) is hit, triggering the FATAL state seen in your logs.
Verifying the Execution Context
You mentioned that running the script via CLI works. This is expected because your interactive shell has a full PATH. To debug this without guesswork, you need to inspect the environment inside the supervisord context.
Add the following line to your supervisor config temporarily:
environment=PATH="/usr/bin:/usr/local/bin:/bin"
Then restart the service and check the output log immediately after the process starts:
tail -f /var/log/worker1.out.log
If the script prints "test", you have resolved the path issue. If it still fails, look for a traceback in the .err.log. The absence of logs in your original post suggests the crash happened before PHP even initialized or that the log rotation was configured to truncate old entries too aggressively.
User and Permission Nuances
Your config sets user=root, but the file permissions are set to -rwxrwxr-x 1 root apache. This is a subtle security smell, though likely not the cause of the exit code 1. More importantly, running PHP scripts as root is generally discouraged unless absolutely necessary, as it can lead to permission escalation vulnerabilities if the script is compromised.
If you switch to user=apache, ensure that the PHP binary itself has execute permissions for that user and that any libraries the script depends on are accessible within the apache user's context. Sometimes, open_basedir restrictions in php.ini prevent scripts from reading specific directories when run as a different user than root.
Handling Long-Running Processes
In your update, you modified the script to an infinite loop with a sleep:
while(true){
echo "test".PHP_EOL;
sleep(10);
}
This change confirms that the issue is not related to PHP exiting after a specific task completion (which would be exit code 0). The fact that it still crashes immediately indicates the environment setup is fatal to the interpreter itself.
If you are managing long-running jobs, ensure your command does not contain quotes around the script unless the path contains spaces. For example:
# Incorrect if path has spaces (though unlikely for /home/...)
command=php "myScript.php"
However, a more robust approach for complex scripts is to use an absolute path to the binary explicitly:
command=/usr/bin/php /home/path/to/script/myScript.php
When to Use startsecs and stopsignal
If your PHP script requires time to initialize (loading a large database or warm-caching), you might want to configure startsecs=5. This tells supervisord to wait 5 seconds before declaring the process failed if it hasn't produced output yet. However, for simple scripts, this is rarely needed and can mask real errors.
If you successfully get the script running but need to gracefully stop it later, ensure your stopsignal is set appropriately (e.g., stopasgroup or SIGTERM). By default, supervisord sends a kill signal which terminates processes instantly, potentially corrupting data if files are being written.
Summary of Fixes
- Expand the PATH: Ensure the environment variable includes both
/usr/local/binand/usr/bin. - Use Absolute Paths: Replace
phpwith the full path to the binary (e.g.,/usr/bin/php) to bypass path resolution entirely. - Check
open_basedir: Verify thatphp.inidoes not restrict access to the script's directory when running under the target user. - Review Log Rotation: Ensure logs are not being truncated before you can inspect them for PHP stack traces.
Once these environment variables are corrected, the "not expected" exit status will disappear, and your worker will transition from FATAL to RUNNING, allowing supervisord to manage the lifecycle correctly.