How to set up a cron job via PHP (not CPanel)?
Most Linux systems with crond installed provides a few directories you can set up jobs with:
/etc/cron.d/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
...
The idea here is to create a file in one of these directories. You will need to set the proper permissions/ownership to those (or one of those) directories so that the user launching the PHP script can write to it (Apache user if it's a web script, or whatever CLI user if CLI is used).
The easiest thing is to create an empty file, assign proper permission/ownership to it, and have the PHP script append/modify it.
Per example:
$ touch /etc/cron.d/php-crons
$ chown www-data /etc/cron.d/php-crons
Then in PHP:
$fp = fopen('/etc/cron.d/php-crons', 'a');
fwrite($fp, '* 23 * * * echo foobar'.PHP_EOL);
fclose($fp);
If what you're getting at is dynamically adding lots of jobs to crontab form your application, a better way to do that is manually add ONE cron job:
php -f /path/to/your/runner.php
Store your jobs that you would be adding to cron manually in a table (or one table per task-type), and then have your runner go through the table(s) every minute/hour/day/whatever and execute all the ones that should be executed at that time.
From pure PHP I will create deamon that will manage this (those) cron job(s).
how to create it:
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/ to start with
Finding crontab file isn't easy on shared hosting and there's no certainty that cron will read that file again while it's already running.
Actually I the best way is to use corntab command.
If you don't have access to shell you can use for example PHPShell. Try this.
Uplode a txt file via FTP with jobs in crontab fomat for example
5 * * * * /some/file/to/run.sh > /dev/null
(remember to put a newline at the end of that line)
Log in to your PHPShell and run
crontab uploded_filename.txt
Remember to change file permissions
chmod 775 uploded_filename.txt
Check your cron jobs using
crontab -l
Cheers
There is an embargo on the use of PHP to edit crontabs which has been in place since 2004. You may not be allowed to do this if you live outside of the United States, check with your local government agency.
But seriously, you could always call "crontab -" with a system call. If you need to do this for some user other than the webserver, you'll need some ssh or sudo magic. But it all seems like a bad idea.
Related
As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
Since your script resides in your public_html directory you can use wget for your Cron Job
wget -O - -q https://yoursite.com/wallboard/update.php
-O - output is written to the standard output in this case it will go to the email address you specify in CPanel
-q quiet mode
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME
I have to work with cron job and i read something about that like
* * * * * /usr/bin/wget http://blablaba/cron.php
(i know how set time m ex. run every minute).
My question is where i have to put that code?
in php index?
like:
You should put all cron jobs into crontab with
crontab -e
As far as I know it's the same for all Linux distros and Mac OS. As for Windows, just save yourself a trouble and go for Vagrant virtual machine
I usually put this command in WEBMIN.
Ask your hosting, where do you need to configure crons jobs.
For example in CPANEL:
You can't edit it with PHP directly.
You need to add new cron jobs using console command:
crontab -e
or add new files to the /etc/cron.d directory.
Best if you create a new file under /etc/cron.d directory which is where you should put your own cron jobs.
I want to set a cron job using PHP file, but don't know where the problem is coming in my process.
When I wrote below (in PHP file and executed it on browser), it shows me the result. (It lists all cron jobs)
echo shell_exec('crontab -l');
But when I wrote below line, then it didn't set any cron job:
echo shell_exec('crontab /home/testsite/public_html/crons/crons.txt');
But If I run the same command (crontab /home/testsite/public_html/crons/crons.txt) via puTTY on my CentOS Dedicated Server, then it updates the crons list from crons.txt file.
I also tried passing -u as I saw on several stackoverflow questions:
echo shell_exec('crontab -u testsite /home/testsite/public_html/crons/crons.txt');
Can anyone help?
I'm not sure what exactly you are trying to do, and the way you are trying to change the crontab is not the best way either. Let me show you how I would do it:
First, get the user you are when executing the PHP by putting into the PHP script:
echo get_current_user();
Depending on the cron software you use (I use anacron, like most other ppl I guess), you go the cron config directory and put a single file for you up with write access:
touch /etc/cron.d/testsite
chown <user from above>:<user(=group) from above> /etc/cron.d/testsite
With PHP, you can directly read and write to that file now (file_get_contents(); file_put_contents()) and crontab will immediately use it.
I wanted to implement two cronjobs with different execution time. One cron job is for sending emails and second cron job for validating my application subscriptions.
I write one crontab file and write to two cronjob as follows:
2 * * * * path to mailCronjob mail.php
20 * * * * path to check my application's subscriptions sub.php
The problem is first cronjob is working fine. Mail will delivers fine, but the second cronjob
is not working. I tried to run second job manually, its also working fine.
I am using command to set cronjob as:
crontab crontab_file
when I give command crontab -l
it also shows both cronjob in command line.
I wanted to ask, am I missing something here, or what should I do to run those cronjobs.
FACT: you can run as many cron jobs from a single crontab file as you wish.
FACT: you can also run different jobs as different users, each with their own crontab file.
SUGGESTION:
1) Just debug what's wrong with your second job.
2) It could be path, it could be permissions; it's more than likely environment (the environment for "cron" can be different from the environment for the same user from a command line).
PS:
Try this, too:
How to simulate the environment cron executes a script with?
Debugging crontab jobs
Check the owning user's email and see if an error report has been sent to it.
If you need to be a certain user and have that user's environment change your call to
su - -c "/path/to/sub.php" SubScriptUser
If your script only works from a certain directory use
cd /path/to/ && ./sub.php
I recall the same problem. For some reason, I had to press enter after my second cron job. Even in the first cron job, if it is the only job, needs a CR/LF (enter). Cursor needs to be on second line (if there is one cron job)... cursor needs to be on the third line, if there is two cron jobs. I guess, needs to read the file completely to interpret the cron job command. I just share this, because if you dont do that, only the first job will be executed, and skips the second one totally unless enter is pressed at the end of the second job. Best regards and cheers... Test that and let us know.
You need to add an empty line to the end of the config file
I never did 2 actuall cronjobs in one cron-tab file, but rather had the one cronjob execute every 15 minutes and query the database or look into a config file what tasks are there to execute, maybe this concept helps you.
i set my php file in cron jobs with command /home/findia/public_html/ifs/ifs-admin/reminder_mail.php.
But i am getting mail by cron jobs it contains,
/bin/sh: /home/findia/public_html/ifs/ifs-admin/reminder_mail.php: Permission denied
Inside your cron tab put this command instead:
/usr/bin/php -q /home/findia/public_html/ifs/ifs-admin/reminder_mail.php
Assuming your php binary is in /usr/bin; change it accordingly, if needed.
I put this line /usr/bin/curl http://YOURDOMAIN/whmcs/crons/cron.php
(change YOURDOMAIN for the right domain or IP address) n the crontab file and it works
You need to make sure the file permissions are set correctly for the user that is executing the cron job.
php CLI scripts need to be run with /usr/bin/php
so for example, your "cron" entry would be (assuming php exists in /usr/bin)..
[insert frequency of execution here>] /usr/bin/php /home/findia/public_html/ifs/ifs-admin/reminder_mail.php