Run a PHP file in a cron job using CPanel
Asked Answered
S

14

112

I am just trying to run a PHP script using a cron job within CPanel - is this the correct syntax:

/usr/bin/php -q /home/username/public_html/cron/cron.php >/dev/null

I am not getting any email notifications stating a cron has been completed, do I need to do anything specific with the PHP file?

Signorino answered 7/9, 2011 at 11:5 Comment(5)
I think it is quite relevant seeing as many projects are hosted on Cpanel, and one would need to know how to run cron jobs if need bePhotoplay
If cPanel isn't allowed on Server Fault or Stack Overflow, where should questions about it be?Research
quite ironic that this question was classed as 'off-topic' for StackOverflow but is the most active question i've ever had one here..... I hope it helped others :)Signorino
Thanx @Tatu Ulmanen It works for me!Lebna
You can get email notifications by removing this part ">/dev/null". Also be sure to add the desired email address as the notification email address in cPanel cronjob section.Disestablish
A
72

In crontab system :

  • /usr/bin/php is php binary path (different in some systems ex: freebsd /usr/local/bin/php, linux: /usr/bin/php)
  • /home/username/public_html/cron/cron.php should be your php script path
  • /dev/null should be cron output , ex: /home/username/stdoutx.txt

So you can monitor your cron by viewing cron output /home/username/stdoutx.txt

Arterio answered 7/9, 2011 at 11:18 Comment(11)
I am using this now and it works but I do not get any email notification any ideas why not? php /home/username/public_html/cron/cron.php note I had to put the following line at the top of the PHP script #! /usr/bin/php -qSignorino
If u want cronjob send emails for u, u must use php mail(), i dont thinks cronjob have automatic mail system, unless u using shell scripting or php.Arterio
When I click on the "cronjobs" tab in cPanel, there are two boxes on the page - one for the command you wish to execute, and another for the e-mail address you want the output to be sent to. This is in cPanel 11.Poem
What will be full command? I mean full command in one line with all options.Pitch
@SomyA please could you tell me the command for run java script file.Arrowhead
@NarenderReddy i thinks this is what you looking for Running Command Line in JavaArterio
@SomyA thanks for replay i want java script using a cron job within CPanel.THe give command for tun php file but iwant to run java script fileArrowhead
which language are cron jobs written in? also, i want to apply a cron job to a python/scrapy script that i wrote which requires 'scrapy crawl crawlername' to be executed from the CLI to run? never done this before. any help would be huge <33Wilen
How i can limit the size of cron job log file?or create a different log file on daily bases?Urey
as in unix system you can use something like logrotate for limiting log file sizeArterio
@RanaNadeem you can name the output file as date and it will create new file for every day like /usr/bin/php /home/username/public_html/yourcronfile.php < /home/username/cron_output_`date +\%d-\%m-\%Y-\%H-\%M`.txtThreefold
E
111

I used this command to activate cron job for this.

/usr/bin/php -q /home/username/public_html/yourfilename.php

on godaddy server, and its working fine.

Excitable answered 13/3, 2012 at 10:37 Comment(3)
What does -q stands for?Siloxane
@BhavikShah According to php.net/manual/en/features.commandline.options.php, -q is for Quiet mode, suppresses HTTP header outputPhotoplay
@Photoplay Thanks. Got it.Siloxane
A
72

In crontab system :

  • /usr/bin/php is php binary path (different in some systems ex: freebsd /usr/local/bin/php, linux: /usr/bin/php)
  • /home/username/public_html/cron/cron.php should be your php script path
  • /dev/null should be cron output , ex: /home/username/stdoutx.txt

So you can monitor your cron by viewing cron output /home/username/stdoutx.txt

Arterio answered 7/9, 2011 at 11:18 Comment(11)
I am using this now and it works but I do not get any email notification any ideas why not? php /home/username/public_html/cron/cron.php note I had to put the following line at the top of the PHP script #! /usr/bin/php -qSignorino
If u want cronjob send emails for u, u must use php mail(), i dont thinks cronjob have automatic mail system, unless u using shell scripting or php.Arterio
When I click on the "cronjobs" tab in cPanel, there are two boxes on the page - one for the command you wish to execute, and another for the e-mail address you want the output to be sent to. This is in cPanel 11.Poem
What will be full command? I mean full command in one line with all options.Pitch
@SomyA please could you tell me the command for run java script file.Arrowhead
@NarenderReddy i thinks this is what you looking for Running Command Line in JavaArterio
@SomyA thanks for replay i want java script using a cron job within CPanel.THe give command for tun php file but iwant to run java script fileArrowhead
which language are cron jobs written in? also, i want to apply a cron job to a python/scrapy script that i wrote which requires 'scrapy crawl crawlername' to be executed from the CLI to run? never done this before. any help would be huge <33Wilen
How i can limit the size of cron job log file?or create a different log file on daily bases?Urey
as in unix system you can use something like logrotate for limiting log file sizeArterio
@RanaNadeem you can name the output file as date and it will create new file for every day like /usr/bin/php /home/username/public_html/yourcronfile.php < /home/username/cron_output_`date +\%d-\%m-\%Y-\%H-\%M`.txtThreefold
G
28

>/dev/null stops cron from sending mails.

actually to my mind it's better to make php script itself to care about it's logging rather than just outputting something to cron

Gifford answered 7/9, 2011 at 11:15 Comment(2)
may I ask how you do that? Doesn't cronTabs output to something anyway? I do >/dev/null 2>&1 at the ending as well...Darnley
@Darnley if you make a task like this /path/to/file.php > /dev/null it's gonna be silent.Gifford
W
21

This is the easiest way:

php -f /home/your_username/public_html/script.php

And if you want to log the script output to a file, add this to the end of the command:

>> /home/your_username/logs/someFile.txt 2>&1

Washbowl answered 6/5, 2015 at 11:51 Comment(0)
S
17

This is the way:

/usr/bin/php -q /home/username/public_html/yourfilename.php >/dev/null
Sipper answered 25/1, 2014 at 20:30 Comment(0)
F
13

This cron line worked for me on hostgator VPS using cpanel.

/usr/bin/php -q /home/username/public_html/scriptname.php
Fontenot answered 12/4, 2016 at 21:27 Comment(0)
N
11

I've had problems using /usr/bin/php on CPanel as it is compiled as a "cgi-fcgi" binary and not "cli". Try using /usr/local/bin/php or, as it is first in the path anyway, just use 'php' instead:

php /path/to/script.php

If you want to run the script as an executable, give it +x perms and use the following as the first line of the script:

#!/usr/bin/env php
Nellynelms answered 21/1, 2016 at 11:42 Comment(0)
C
7

I hope your problem is with path & php binary as well. If you have fixed the path as per older answers, please use php-cli instead of php command while running cron job.

It may be possible php_sapi_name() is not returning cli. Its returning something else like cgi-fcgi etc.

/usr/bin/php-cli -q /home/username/public_html/cron/cron.php >/dev/null

I hope it will help.

Countrified answered 2/8, 2016 at 8:1 Comment(0)
A
4

This works fine and also sends email:

/usr/bin/php /home/xxYourUserNamexx/public_html/xxYourFolderxx/xxcronfile.php

The following two commands also work fine but do not send email:

/usr/bin/php -f /home/Same As Above

php -f /home/Same As Above

Acidity answered 5/10, 2017 at 9:51 Comment(0)
F
3

Suggested By Experts.

/usr/local/bin/php /home/username/public_html/path/to/cron/script
Fair answered 30/7, 2018 at 7:2 Comment(0)
E
2

It is actually very simple,

php -q /home/username/public_html/cron/cron.php
Elora answered 20/4, 2019 at 16:22 Comment(0)
N
1

For domain specific Multi PHP Cron Job, do like this,

/usr/local/bin/ea-php56 /home/username/domain_path/path/to/cron/script

In the above example, replace “ea-php56” with the PHP version assigned to the domain you wish to use.

Hope this helps someone.

Nate answered 14/2, 2019 at 21:29 Comment(0)
F
0

On a Hostgator CPANEL this worked for me:

php /home/here_your_user_name/public_html/cronJob.php
Florilegium answered 8/10, 2020 at 14:55 Comment(0)
C
0

So here's what happened to me since these solutions are quite correct in term of defining the new Cron job, there was an issue for me that was causing the script to not run, and I'm mentioning to this because at some point, people might confuse if the correct entry is : /usr/local/bin/php or /usr/bin/php or even php.

So basically I had this working using both above entries, until I moved my project to a new VPS with Cpanel, and script was working, everything was correct except the Cron Job. So here's the steps to fix the issue.

  1. Check the server time zone.

I had to set my server time to Asia/Tehran which before setting this my Cron wasn't executing at the correct moment and you can set time zone using WHM, Server time.

  1. Check if Cron job is ran using : grep "replace with file or whatever you're calling" /var/log/cron

  2. Cron job is running, great, let's check if script is working, so go to the location of the script and run the script, which at this point mine was running a simple php artisan in Laravel directory, and then I've got the real issue.

Your PHP requirement version for version 8 show up.

So in Cpanel when you try to define a version for a domain/sub-domain, you can see above it say, the default PHP version is x.xx and at this point I found out I should change it which in WHM, in Software and then MultiPHP Manager, set the PHP version to desired version that Laravel need to ran.

Defining PHP version in Cpanel isn't enough since Cron is not working like that and it's relying on the default version that's defined in WHM or Server.

Columnar answered 27/9, 2023 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.