How to Execute PHP, Python and Perl Scripts Using Cron Job

As a Linux administrator you may already know about crontab, as this is one of the regular activity for you.

You have done a lot of work using crontab for shell scripts.

Did you know that cron job can be scheduled for other scripts such as Perl, Python and PHP?

Have you ever tried this?

Did you get a chance to do that?

If not, we will help you understand this in detail.

You can schedule other scripts like shell scripts and there is no big difference.

Alternatively, you can use command-line download managers like wget and CURL, or use command-line web browser Lynx to schedule it.

To do so, you must find the absolute path of a program using the which command or whereis command as follows.

# which php
/usr/local/bin/php

# whereis python3
/usr/bin/python3

# which perl
/usr/local/bin/perl

1) How to Execute Python3 Script Using Cron Job

Use one of the following formats to run Python3 scripts using the cron job. It runs the “python-test-script.py” Python script at 9’o clock every morning.

# crontab -e

* 9 * * * /usr/bin/python3 /opt/scripts/python-test-script.py >> ~/python-cron.log 2>&1

Alternatively, you can use the following format.

# crontab -e

* 9 * * * $(which python3) /opt/scripts/python-test-script.py >> ~/python-cron.log 2>&1

2) How to Run Perl Script Using Cron Job

Use one of the following formats to run Perl scripts using the cron job. It runs the “perl-test-script.pl” Perl script at 9’o clock every morning.

# crontab -e

* 9 * * * /usr/local/bin/perl /opt/scripts/perl-test-script.pl >> ~/perl-cron.log 2>&1

Alternatively, you can use the following format.

# crontab -e

* 9 * * * $(which perl) /opt/scripts/perl-test-script.pl >> ~/perl-cron.log 2>&1

3) How to Run PHP Scripts Using Cron Job

Use one of the following formats to run PHP scripts using the cron job. It runs the “php-test-script.php” PHP script at 9’o clock every morning.

# crontab -e

* 9 * * * /usr/local/bin/php /opt/scripts/php-test-script.php >> ~/php-cron.log 2>&1

Alternatively, you can use the following format.

# crontab -e

* 9 * * * $(which php) /opt/scripts/php-test-script.php >> ~/php-cron.log 2>&1

3a) How to Run PHP Scripts with the lynx Command Using the Cron Job

If you can use your php script as a URL, you can schedule a job with crontab using the lynx command-line web browser.

# crontab -e

0 14 * * * lynx -dump https://www.2daygeek.com/cron/test/index.php >> ~/php-cron.log 2>&1

The cron job above executes the PHP script at 11am daily by retrieving the web page using the Lynx text browser. It operates in interactive mode and dumps the output of the web page to the standard output.

3b) How to Run PHP Scripts with the curl Command Using the Cron Job

If you can use your php script as a URL, you can execute a job with crontab using the curl command-line download manager.

In this example, we will run the php script by calling the web page using the curl command. By default it displays output in the standard output. But you can store the output to a file by adding the -o option with the curl command.

# crontab -e

0 15 * * * /usr/bin/curl -o output.txt https://www.2daygeek.com/cron/test/index.php >> ~/php-cron.log 2>&1

3c) How to Run PHP Scripts with the wget Command Using the Cron Job

If you can use your php script as a URL, you can execute a job with crontab using the wget command-line download manager.

In this example, we will run the php script by calling the web page using the wget command. By default it displays output in the standard output. But you can save the output to a file by adding the -O option with the wget command.

# crontab -e

0 16 * * * /usr/bin/wget -q -O output.txt https://www.2daygeek.com/cron/test/index.php >> ~/php-cron.log 2>&1

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...