Create/delete objects in scheduled jobs (cron)

I have some custom methods run by a scheduled job (cron). Everything works fine. Once I add in that method a create/delete method like this: $object->create($user) or $object->delete($user), the scheduled job fails to run and never ends (processing stays on 1 in the db).
This behavior does not appear when I run the job manually, it only appears when the job is run by the cron. I suspect it has something to do with $_SERVER not being set when a cron is executed but I am not sure. Has anyone had this issue? Any hints on what is causing that? I tried on dolibarr 16.0.3 and 17.0.2

Thanks!

Hello,

I think that $user is not initialized when called by cron.