How to run php file using cron jobs

Our team had developed the php script, to send mail to our registered customers. We know how to schedule the jobs at cron using .sh (shell) file, but most of them don’t know, how to add the php file to cron tab. It’s not critical one, but you should know the php installation location (path) on your server, then only you can add the php file to cron jobs with php installation location. we can perform this action with four methods.

1) How to find the php location ?

Use the below command to find-out the php location.

[email protected] [~]# find / -name "php"
/usr/local/bin/php

Most of the servers, php was installed at default location, the default location is “/usr/local/bin/php” my server also, the same default location.

2) Execute the php file using cron jobs ?

We have a newsletter, php mail script at below location and it will be execute at 1’o clock (IST). This will run every day at 1’o clock.

[email protected] [~]# crontab -e
  GNU nano 2.0.9                                        File: /tmp/crontab.sC5jee


0 13 * * * /usr/local/bin/php /home/2daygeekc/public_html/cron/newsletter/index.php

3) Execute the php file with lynx command using cron jobs ?

We have a newsletter, php mail script at below location and it will be execute at 2’o clock (IST). lynx is text based browser on linux command line. When time is 2’0 clock the cron will be execute the job and url is open using lynx command and perform the action according that. Output is stored in standard format using -dump command.

[email protected] [~]# crontab -e
  GNU nano 2.0.9                                        File: /tmp/crontab.sC5jee


0 13 * * * /usr/local/bin/php /home/2daygeekc/public_html/cron/newsletter/index.php
0 14 * * * lynx -dump https://www.2daygeek.com/cron/test/Index.php

4) Execute the php file with curl command using cron jobs ?

We have a newsletter, php mail script at below location and it will be execute at 3’o clock (IST). When time is 3’0 clock the cron will be execute the job and url is called by curl command and perform the action according that. Output is stored at output.txt file.

[email protected] [~]# crontab -e
  GNU nano 2.0.9                                        File: /tmp/crontab.sC5jee


0 13 * * * /usr/local/bin/php /home/2daygeekc/public_html/cron/newsletter/index.php
0 14 * * * lynx -dump https://www.2daygeek.com/cron/test/Index.php  
0 15 * * * /usr/bin/curl -o output.txt https://www.2daygeek.com/cron/test/Index.php

5) Execute the php file with wget command using cron jobs ?

We have a newsletter, php mail script at below location and it will be execute at 4’o clock (IST). When time is 4’0 clock the cron will be execute the job and url is called by wget command and perform the action according that. Output is stored at output.txt file.

[email protected] [~]# crontab -e
  GNU nano 2.0.9                                        File: /tmp/crontab.sC5jee

  
0 13 * * * /usr/local/bin/php /home/2daygeekc/public_html/cron/newsletter/index.php
0 14 * * * lynx -dump https://www.2daygeek.com/cron/test/Index.php  
0 15 * * * /usr/bin/curl -o output.txt https://www.2daygeek.com/cron/test/Index.php
0 16 * * * /usr/bin/wget -q -O output.txt https://www.2daygeek.com/cron/test/Index.php

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...