It seems like the cron jobs are being triggered as expected, but the scripts are not working correctly when run by the cron daemon, even though they work fine when manually executed via SSH. This is a common issue with cron jobs due to differences in the environment between cron and a normal shell session. Here are some troubleshooting steps and suggestions to resolve the issue:
1. Specify Full Path for PHP Executable:
In cron, the environment is minimal, and it might not know where the php
executable is located. Try specifying the full path to the PHP interpreter.
First, find the full path to PHP by running:
which php
Let’s say the result is /usr/bin/php
. Update your cron jobs to use the full path like this:
* * * * * cd /home/marinisilvio.com/rarespot/rs-core/src; /usr/bin/php ether_handler.php >> /var/log/rscore.log 2>&1
* * * * * cd /home/marinisilvio.com/rarespot/rs-core/src/bitquery; /usr/bin/php bq_collections.php >> /var/log/rscore.log 2>&1
This ensures that cron can find the PHP binary.
2. Check Permissions:
Ensure that the user running the cron job has the necessary permissions to execute the script and write to /var/log/rscore.log
. The log file might not be writable by the cron user.
Check and modify permissions if needed:
chmod 755 /home/marinisilvio.com/rarespot/rs-core/src/*.php
chmod 666 /var/log/rscore.log
If the rscore.log
doesn’t exist, try creating it manually first.
3. Check Environment Variables:
The environment variables in cron jobs are limited compared to those in a normal shell. If your PHP script depends on environment variables (like $PATH
or $HOME
), these might not be set properly.
You can try explicitly setting the environment variables at the start of the cron job:
* * * * * export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/home/marinisilvio.com/bin; cd /home/marinisilvio.com/rarespot/rs-core/src; /usr/bin/php ether_handler.php >> /var/log/rscore.log 2>&1
4. Log Errors to Debug:
Redirect both standard output (stdout
) and standard error (stderr
) to the log file by appending 2>&1
at the end of the cron job. This will capture any errors in your rscore.log
file that might help with debugging:
* * * * * cd /home/marinisilvio.com/rarespot/rs-core/src; /usr/bin/php ether_handler.php >> /var/log/rscore.log 2>&1
* * * * * cd /home/marinisilvio.com/rarespot/rs-core/src/bitquery; /usr/bin/php bq_collections.php >> /var/log/rscore.log 2>&1
Then, check the contents of /var/log/rscore.log
for any errors or warnings.
5. Check the PHP Script for Absolute Paths:
If your PHP script uses relative paths to access files or other resources, cron might not run it as expected because cron’s working directory might not be what you expect. Make sure your PHP script uses absolute paths.
Example:
// Change this:
include 'somefile.php';
// To this:
include '/home/marinisilvio.com/rarespot/rs-core/src/somefile.php';
6. Check SELinux or AppArmor:
If you’re using a Linux distribution with SELinux or AppArmor enabled, it’s possible that these security mechanisms are blocking the cron job from running the scripts or accessing the necessary files. You can try temporarily disabling them for debugging.
To check if SELinux is enforcing:
getenforce
If it’s enforcing, try setting it to permissive mode:
setenforce 0
7. Cron User Shell:
Ensure that the shell being used by cron is the same as your regular user shell. You can specify the shell at the top of your cron file like this:
SHELL=/bin/bash
8. Check MySQL Logs:
If the cron job interacts with the database but doesn’t seem to be making any changes, there may be a database issue. Check the MySQL logs for any connection problems or errors when the cron job runs.
9. Try Running the Cron Job Command Manually via Cron:
Sometimes, running the exact command in the cron environment can provide additional clues. You can test the cron job with the following:
sudo -u <cron_user> bash -c "cd /home/marinisilvio.com/rarespot/rs-core/src; /usr/bin/php ether_handler.php"
By applying these suggestions, you should be able to diagnose the issue and get the cron job working correctly. Let me know what you find or if you need more help!