I'm working on a Laravel app deployed on a Kubernetes cluster, and I need to set a cronjob to run php /var/www/html/artisan schedule:run.
I'm using php:7.4-fpm-alpine image.
The following script is used to generate the cronjob:
#!/bin/sh
crontab -l > mycron
touch /var/cronlogs.txt
echo "* * * * * php /var/www/html/artisan schedule:run >> /var/cronlogs.txt 2>&1" >> mycron
crontab mycron
rm mycron
The cronjob is working fine, but suddenly I faced an issue that when some of the php artisan commands executed by the cronjob, it writes the log in 'storage/logs' with root permissions which is deny www-data requests from writing logs.
I tried to change the user who run the cronjob to www-data, but when I did that the cronjob is not running anymore.
What I Did:
1- I changed "crontab -l > mycron" to "crontab -u www-data -l > mycron" and "crontab mycron" to "crontab -u www-data mycron" in the script.
2- I changed the ownership of /var/spool/cron/crontabs/www-data to www-data:www-data
3- I ran crond command.
There were no errors when I ran crond command but the job did not start.
The cronjob in /var/spool/cron/crontabs/root works fine -with the changing log file issue-.
The cronjob in /var/spool/cron/crontabs/www-data doesn't work.
How to make cronjobs run by www-data? and if this is not the solution how to prevent php artisan commands from writing logs by root user?