Dynamically create cron jobs with php - php

I need to do tweets on twitter through php on time ,day set by user. For that i am planning to use Cron jobs that is created dynamically through php .
Can any body please tell me how can i create cron jobs through PHP only.

Set cron url in your server .Also set Interval.

Why Don't You Use Cronjob ? Cron Job + Twitter
if you want to create cron jobs through only PHP, You have to run the php file by means of you have to trigger.
your main index.php file of website's.
<?php
if (hour == 14) include cron.php and call function or class method etc.
?>

Related

How to run php script on cron job in cpanel

I schedule a new job on cronjob. But it's not working. Is there any way to run cronjob in cpanel using commands.
There is a bunch of way to running script with CRON. I do use this way on my web application most of the time when I need to use CRON.
The first step is to choose a setting. The setting means when you want to run this script.
and after then simply just use CURL with Cron to get this work properly like this: curl http://example.com/script.php on command box and click on the add button and whola! You have successfully added your first CRON Task.
You may have a look at this picture below to get it properly
Go to CRON JOB option on your cpanle menus
Set url like this and set time according to your functionality
php /home/username/public_html/filename.php
Run it in putty
/usr/bin/php /var/www/html/project_name/artisan schedule:run 1>> /dev/null 2>&1 this is for laravel project
you can simply use like this - /usr/local/bin/ea-php74 /home/username/domainname.com/script_path/yourfile.php
replace username to your cpanel username, domain name to your domain name, script path where you want to run it, then replace the main file for corn job in yourfile.php

Add custom task to Moodle cron job

I'm working with Moodle 2.9, and trying to add some new task to cron.
Inside my moodle/theme/portal folder I added a portal_cron() function to lib.php file, then I run cron manually from command line but it's not working ?!
Cron work normally but ignored my new task!
So what I'm doing wrong and how can I add new task to cron ?
Have you got a cron value in moodle/theme/portal/version.php eg:
$plugin->cron = 60; // Every 60 seconds.
Although the cron function is still available, you should use scheduled tasks from Moodle 2.7+
https://docs.moodle.org/dev/Task_API
Solved,
Just changed the function name from portal_cron() to theme_portal_cron() in lib.php, because this file is in moodle/theme/portal/lib.php directory.

cron jobs bring down the server?

I am using the paid host Hosting24 to run my website. I got a cron job which execute the following code every 1 minute.
<?php
require_once('connect.php');
for($c = 0; $c < 60; $c=$c+5)
{
// php to mysql queries SELECT/ UPDATE/ INSERT etc...
sleep(5);
}
mysql_close($my_connection);}
?>
I tried to use the for loop to allow the script to run for 1 minute. Eventually my script should run for as long as I want it to be because the server will execute it every 1 min.
However, I opened my website for a short while and then I cannot connect to it. I cannot even access my cpanel.
Is my cron job script overheating the system, so the system is down?
How should I set up my cron job script to let it run every 1 min and lasts for 1 min?
Thanks.
It's been my experience that cron jobs that need to include files should contain the full path to that file (the CLI environment can differ greatly from the environment inside the web server). Try that and see if it helps.
If not, the next thing you need to do is turn the cron job off and run it from the CLI yourself, using top to look at the process usage. See how long it takes for your cron to run.

Compare current time with 00:00:00 in php

I want to perform something in my php web service when it is midnight i.e. current time is 24:00:00 / 00:00:00
How can I do that ?
You could use localtime() in order to fetch current time of day, and then work on that using an if statement.
Eg.
$localtime = localtime();
if ($localtime[2] == 0) {
code;
}
Keep in mind that this only works on script that is continually executed (eg. index.php), if you want a script to execute regardless of whether it's currently being executed at the desired time, you need to use chron jobs.
Create a scheduled task which runs at a certain time and runs your script as:
php -f o/script.php
Use PHP Cron Jobs with cPanel
cPanel Simple Cron
you can get started with the simple cron tool built into cPanel.
1)Go to Cpanel
2)click cron job tab
3)Set time and page url for it is: https://www.yoursite.com/page.php
The command to run:
/usr/local/bin/php -f /home/(username)/public_html/page.php

How to set interval for cron file using PHP

Im new to this.I have a cron file called xml.php and i want to run this file at a particular interval.This interval is set by admin on admin panel page.This interval will be stored in the database and i want to fetch that data from db.According to that the xml.php should run.If anybody know how to do this please help.Thanks in advance
Create a script that checks the DB for the last run of the file and the current time difference. If the interval is correct then run the xml script if not then exit. Use cron to run the new script every minute (so every minute it will check if the script needs to be run and run it only if it's time).
What you will need to do is save it as you wish in DB... but there is no way to dynamically change a crontab... What you can do tho is at point of saving to DB you can use PHP to manipulate the crontab using the details that are saved/to be saved you can construct an appropriate crontab entry - see this for more details: Use PHP to create, edit and delete crontab jobs?
It will also depend on the hosting company on their permissions if they will let you execute shell commands from within PHP.

Categories