Most php tasks are expected to be run as the userid that runs apache/nginx processes. On Ubuntu, that would www-data almost always.
Cron doesn't have a complete environment like an interactive desktop. That means most environment variables are NOT set. If you want any set, then your script needs to do that.
So ... to run a task periodically, using the www-data userid, I do it this way:
First, we have to edit the www-data crontab. Not your personal userid or root's crontab:
Code:
sudo -e -u www-data
then we add the entry in the correct format:
Code:
# m h dom mon dow command
3 */2 * * * /usr/bin/nextcloud-news-updater --threads 2 --mode singlerun /var/www/html/nextcloud >> /dev/null 2>&1
My example runs the nextcloud-news-updater every even hour at 3 minutes after every day. This runs as the www-data userid.
You would use something like this:
Code:
15 * * * * /usr/bin/php /var/www/html/modules/mod_sv_reminders_sms/reminders_cron_sms.php >> /dev/null 2>&1
If the script assumes a PWD, it won't work. Cron doesn't set the pwd/cwd. All scripts should run from any directory on the system exactly the same. Any files to be read or written must be able to be accessed by the www-data userid. The php.ini file used will probably matter too. Different versions of php can be installed on the same system, which can leave php scripts with a split brain problem. Ensure the correct php.ini is being used however that is accomplished in php.
Running php scripts as root is dangerous. I wouldn't do it.
If you don't want to use crontab -e, which writes crontabs to specific userid files in /var/spool/cron/crontabs/, then you can use the /etc/crontab file and specify the userid to be used inside it. /etc/crontab uses a slightly different format, but there should be 3+ examples to follow already in there.
If you are still having issues, check the environment between the interactive session that works and the cron environment that doesn't
Code:
env > /tmp/interactive-env
or put that into the different crontab -e versions as
Code:
/usr/bin/env > /tmp/$USER-env
Next use any file comparison tool to compare the output files. I like meld, which is probably the best comparison tool on any platform.
Bookmarks