Running PHP script using crontab

I understand that SO is a question, but no matter how many tutorials I looked at, I can not get my crontab to work, and I create a website that will rely on crontab to reset a specific parameter to my database every night .

Here is my crontab file:

 # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron system # daemon notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 am every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # mh dom mon dow command * * * * * /usr/bin/php -q /var/www/html/cron/index.php 

If I try to cd before /usr/bin/php get

 -bash: cd: /usr/bin/php: Not a directory 

So, I cd just /usr/bin/ , and here is what I found:

 -rwxr-xr-x 1 root root 27216 Feb 10 15:08 pgrep* lrwxrwxrwx 1 root root 21 Jun 13 09:36 php -> /etc/alternatives/php* -rwxr-xr-x 1 root root 9049256 Jul 2 11:57 php5* 

If I cd before /etc/alternatives , I find:

 lrwxrwxrwx 1 root root 13 Jun 13 09:36 php -> /usr/bin/php5* 

Back to the bin file, php5 has the * character and green.

 -rwxr-xr-x 1 root root 9049256 Jul 2 11:57 php5* 

My PHP script. Very simple. It checks the cookie, and if it exists, it increments it by one. Then I check the results on another page. Manually this works. With crontab it is not possible to make it work.

 if (!empty($_COOKIE['cronTest'])) { $int = $_COOKIE['cronTest']; $int++; setcookie("cronTest", $int, time()+3600); } 
+4
source share
1 answer
  • Most likely, your script inside /var/www/html/cron belongs to the www-data user. Depending on your installation, the user running cronjob does not have permission to run this file.

  • There is no $ _COOKIE on the command line. A cookie is sent by the users browser. Since cli is not a browser, you cannot read the cookie value. Although you can access $ _SESSION users, this is another story. Look here. Is it possible to read cookie / session value when executing PHP5 script via command line? for more information.

Your cronjob line looks valid, so the problem will be one of the points above. To check the first, try something without using $ _COOKIE in your file, just

 mkdir('/var/www/html/cron/testdir'); 

to see if a file is available and a directory is created inside the specified directory. If access to it is not possible, create a new group and add as the current user (find out using

 ls -al 

in / var / www / html / cron) and the user running cronjob for the group, then create the file for this group that you want to run. See the accepted answer to this question: Set user rights | The Artisan command will not run in code, but works fine on the command line I posted some time ago on how to do this.

For the $ _COOKIE problem, you will need to find another solution. For example, use Redis or Memcached as a caching service, which can be accessed both online and in cli settings.

Suppose add

 1>> /dev/null 2>&1 

to the end of the cronjob line, if you do not, and let cron run it every minute, you will get a ton of log files.

To fully understand the possible errors when working with php and cli, always provide the full path to your files.

+6
source

All Articles