Cronjobs getting the best of me ... Command line works, same command in Crontab does not!

November 8, 2019 187 views
Ubuntu 18.04 Linux Commands PHP

Hi …

I’ve installed Mautic (email program). It needs 3 cronjobs to run properly.

  1. php /var/www/mydomainname/htdocs/app/console mautic:segments:update
  2. php /var/www/mydomainname/htdocs/app/console mautic:campaigns:update
  3. php /var/www/mydomainname/htdocs/app/console mautic:campaigns:trigger

I am on ubuntu 18.04 and am using PHP 7.0x.

When I do : sudo php /var/www/mydomainname/htdocs/app/console mautic:campaigns:trigger - or any other of the 3 listed above, they work without problem. I get the correct output and it’s executed.

However, when I put the same commands in a crontab, nothing happens.

I’ve tried everything. In the root crontab, in the www-data crontab, in my user crontab, with absolute paths to the PHP7.0 command (in /usr/bin/php7.0). Even tried first chaning to the php directory and what not.

For example :

  • * * * /usr/bin/php7.0 /var/www/mydomainname/htdocs/app/console mautic:segments:update

or

  • * * * sudo /usr/bin/php7.0 /var/www/mydomainname/htdocs/app/console mautic:segments:update

or

  • * * * cd /usr/bin sudo php7.0 /var/www/mydomainname/htdocs/app/console mautic:segments:update

Nothing does the trick when using crontab.

Anyone know why?

1 Answer

Hello,

This is quite interesting. One thing that I’m noticing from your description is that when running the command manually you use sudo, would you mind confirming if the cronjobs that you are setting are under the root user or your low privileged user? My guess would be that your low privileged user does not have privileges to access the files in the /var/www/mydomainname/ directory.

Also, another suggestion here would be to add the following to your cronjobs: >>/path/to/somefile.log 2>&1 that way in the logs you would see some more information regarding what might be wrong with the jobs.

Hope that this helps!
Regards,
Bobby

Have another answer? Share your knowledge.