Lock issue with segment update

Your software
My Mautic version is: 5.0.3
My PHP version is: 8.2
My Database type and version is: MariaDB 10.5

Your problem
My problem is: when doing /usr/bin/php8.1 bin/console mautic:segments:update the command fails.

These errors are showing in the log:

/usr/bin/php8.1 bin/console mautic:segments:update

In ModeratedCommand.php line 161:
                                                                       
  flock(): Argument #1 ($stream) must be of type resource, bool given 

Steps I have tried to fix the problem:
Since it seems related to a file lock issue, I tried with lock by pass and it worked! So it seems there’s a hiccup with the PID file, but I don’t know how to fix it.

/usr/bin/php8.1 bin/console mautic:segments:update --bypass-locking
Rebuilding contacts for segment…
0 total contact(s) to be added in batches of 300
0 total contact(s) to be removed in batches of 300
0 contact(s) affected

Does segments update run via cron also? The issue you are facing indicates that there could be another segments:update run on the system.

Make sure permissions on the filesystem are correct and no other process is running the segments:update.

Hi @mzagmajster !

Yes, they are:

1,11,21,31,41,51 * * * * cd /path/to/mautic && /usr/bin/php8.1 bin/console mautic:segments:update > /dev/null

But I have the error regardless when I’m doing the command. I checked and it seems I have no “stuck” segment-update command while typing it manually. Do you know where to look about the lock file path so I can double check nothing is wrong in the folder?

What is mostly likely happening is that you try to run the command while cron is executing.

If you disable the cron and then run the command again, do you still have the issue described above. If yes, then most likely permissions on the filesystem are not set correctly.

Disabling the action in the crontab doesn’t change anything at all. Regarding permissions, I don’t know where to look exactly.

Some files have to be owned by the right user. Usually that user is www-data, but it really depends on your server config.

Anyway cronjob for segments needs to be run by www-data user and when you are executing the segments:update command you also have to watch out that you are executing it as www-datra user.

I cannot provide you the exact commands to try since they depend on server configuration, but this should give you an angle to explore.

I have www-data all around and all permissions are OK for this user in the Mautic folder.

edit: it was the case initially, but I re-did a chown www-data:www-data -R mautic and now it seems to work. Well, it’s weird those permissions changed by themselves at some point, but I would consider it as solved. Thanks for the tip @mzagmajster !

If the permissions have changed, the cause can be because cron is running as different user.

Make sure to run mauticcron jobs as www-data