Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

raviteja155's avatar

How to correctly setup CRON so that Log will work from both schedules jobs and normal HTTP requests

Current user: "raviteja" With my current setup, Log::info() is only working from CRON jobs - because it is the first to trigger Log class and write to logs folder. Hence the file permissions are coming as: -rw-r--r-- raviteja raviteja Crontab is with the current user.

If there is any Log from HTTP requests, they are not being recorded and getting 500 error. If HTTP requests fre the first to hit, the file permissions are like: -rw-r--r-- www-data www-data

How to setup correctly so that Log from CRON jobs and HTTP requests are recorded.

0 likes
3 replies
LaryAI's avatar
Level 58

It seems like the issue you're facing is related to file ownership and permissions. When the CRON job creates the log file, it's owned by the raviteja user, and when the HTTP request creates the log file, it's owned by the www-data user. This discrepancy can cause permission issues, as one process may not have the necessary permissions to write to a file owned by the other user.

To resolve this, you can ensure that both the CRON job and the HTTP requests run under the same user, or you can adjust the file permissions so that both users can write to the log files.

Here's a solution that ensures both CRON jobs and HTTP requests can write to the log files:

  1. Set the correct ownership and permissions for the log directory and files. You can set the group of the log files to a common group that both raviteja and www-data are members of, and then set the permissions to allow group write access.

First, find or create a common group, for example, webapp, and add both raviteja and www-data to it:

sudo groupadd webapp
sudo usermod -a -G webapp raviteja
sudo usermod -a -G webapp www-data

Then, change the group ownership of the log directory and set the correct permissions:

sudo chown -R raviteja:webapp /path/to/your/laravel/storage/logs
sudo chmod -R 775 /path/to/your/laravel/storage/logs

This will set the group to webapp and allow both owner and group members to read, write, and execute (in the case of directories) within the logs directory.

  1. Modify your CRON job to ensure it runs with the correct permissions. You can use the crontab of the raviteja user, but make sure that the user is a member of the webapp group and that the group has write permissions to the log files.

Your CRON entry might look something like this:

* * * * * cd /path/to/your/laravel && php artisan schedule:run >> /dev/null 2>&1
  1. Ensure that your Laravel application sets the correct permissions for newly created log files. You can do this by setting the umask in your application's bootstrap process. In the bootstrap/app.php file, you can add the following line to set the umask so that new files are created with group write permissions:
umask(0002); // This will set the default permission to 775 for directories and 664 for files

By following these steps, you should be able to configure your system so that both CRON jobs and HTTP requests can write to the log files without permission issues. Remember to replace /path/to/your/laravel with the actual path to your Laravel application.

Snapey's avatar
Snapey
Best Answer
Level 122

you should make the cron owned by the www-user. you can do this with

crontab -e -u www-data 

and then remove the cron entry owned by raviteja

This will fix the issue of log file being created under the wrong user

1 like

Please or to participate in this conversation.