Score:0

Cronjobs are not running by www-data

eh flag

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?

guiverc avatar
cn flag
You've not provided any OS/release details; are we to guess?
in flag
[Here is the droid you are looking for](https://askubuntu.com/a/210545/1222991).
Obada Qafisheh avatar
eh flag
@guiverc NAME="Alpine Linux" ID=alpine VERSION_ID=3.16.3 PRETTY_NAME="Alpine Linux v3.16"
Obada Qafisheh avatar
eh flag
@matigo /etc/crontab not found, should I create it manually?
guiverc avatar
cn flag
Please refer https://askubuntu.com/help/on-topic, Ubuntu and *official* flavors of Ubuntu (https://ubuntu.com/download/flavours) are on-topic on this site. The on-topic link provides alternate SE sites for non-Ubuntu OSes.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.