Executing PHP with Crontab

Execute PHP script in cron job

Automated Tasks: Cron

Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.
You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.

Configuring Cron Tasks

In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:

*/10 * * * * /usr/bin/php /opt/test.php

In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.

* is a wildcard, meaning "every time".

Start with finding out your PHP binary by typing in command line:

whereis php

The output should be something like:

php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz

Specify correctly the full path in your command.

Type the following command to enter cronjob:

crontab -e

To see what you got in crontab.

EDIT 1:

To exit from vim editor without saving just click:

Shift+:

And then type q!

Executing php with crontab

Start by typing at a command line:

whereis php

Do this as the user that the cron job will be run under. This will show you the path to your executable. You can then use that path (if it's not already in your PATH variable) in your cron entry:

5 * * * * /your/path/to/php /var/www/some/path/script.php

Edit: you may need to install the php5-cli (Ubuntu package name) package if all you have is the Apache PHP module installed. This will give you the binary executable that you can run from a command line.

Running PHP file using crontab

To execute devicecheck.php every 1 hour try the following:

Method A :: Execute the script using php from the crontab

# crontab -e
00 * * * * /usr/bin/php/var/www/devicecheck.php

Method B: Run the php script using URL from the crontab

If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.

The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.

00 * * * * lynx -dump http://www.yourwebsite.com/yourscript.php

The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.yourwebsite.com/yourscript.php

The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.yourwebsite.com/yourscript.php

UPDATE::

# chmod a+x /home/username/yourscript.php
# crontab -e
00 * * * * /home/username/yourscript.php

php execute crontab script every 1 minute

I managed to get it working. My solution was to check if the crontab was actually running by appending the crontab job with >>/tmp/auto-update.log 2>&1 which allowed me to further investigate the issue.

I found that the crontab was indeed running, but as a different user (hence why when I was manually calling crontab -e I could not see the job since I am calling it as my own username.

The crontab was also actually invoking my PHP script, where I could then find out the errors in the auto-update.log, which happened to be due to incorrectly stating the require paths.

Multiple PHP scripts run at same time in Cron

If you have the root permission you can try this way.

* 22 * * * /root/sbin/allDomainsCron.sh

Where allDomainsCron.sh contains your domains cron.php one by one:

#!bin/bash
php /var/www/domain1/cron.php
php /var/www/domain2/cron.php
php /var/www/domain3/cron.php
php /var/www/domain4/cron.php
...

So you can ensure that only one cron.php run at a time.

If you would run the script once a day at 22:00 this code will do the work:

0 22 * * * /root/sbin/allDomainsCron.sh

How to run a PHP script using a Cron job

try

0/2 * * * * curl http://[your_id:port]/kyrill/filetest.php


Related Topics



Leave a reply



Submit