php run script in particular time - php

I am very wonder how to run php script always or specific time WITHOUT connection or call of user/admin. For example, I am making a push-messaging alarm application. This app sends message to user when time is 5:00 PM. But push-message system requires php server page that requests FCM(push-message server) request.(This app is just example, so please don't answer me how to make push message.)
This is not the only situation I have to solve. If I have many advertisers who want to advertise on my website, and some of advertisers pay for limited-time advertise, I have to stop providing that AD after 5:00 PM.
What I want to ask is: How to run php code in background? (likely purpose to make time-alart push message) and run code in PARTICULAR TIME or ALL TIME?

You can Use cronjob to run your file in specific time

What you need is a CRON job to run in background. CRON are scheduled jobs which are set to be executed at a particular time depending on your need.
Basically it will be just a file stored on your server, which will be executed by the cron daemon. (a background process)
Refer below link for an example :
https://code.tutsplus.com/tutorials/managing-cron-jobs-with-php--net-19428

Related

Scheduled email in php

I have a program written in php/html where the user will input a job then after submit, will notify the admin and other users through email. What I wanted is to have a scheduled email where it will notify the admin/users that the job has not yet been done after 12 hrs. I've read about cronjob or cron tab but I don't understand hoe to use it well. Is there another way to do it using phpmailer and not crontab?
yes and no.
PHP by its nature is a stateless script that runs when a user or service requests a url.
The php script runs, does it stuff and then ends, normally there is relatively short timeout either in the PHP config or on the webserver, so you cant have a script running in the background waiting for a specific time to send out your email.
In your situation, you would create a cron job (or a scheduled task on windows) this task/job generally runs on the webserver (although is doesn't have to), the task will load a php script, either as a command or via wget.
So set the task to start at the intervals you require, it will envoke the PHP script, send the emails or what ever you want it to do then the php script will end, ready to run on the next schedule.

Make a cron like schedule

My webhost doesn't have cron in it so I cannot set up cron jobs.
There is a certain php script in my website that should be ran every 24 hours to write daily logs etc.
I tried looking for external schedulers but I don't trust them.
How can I do such thing without cron?
I thought I maybe could use a mysql server and have an event or something?
I do NOT want it to rely on when a user uses my website due to the exact daily timing that I need.
Help is appreciated.
From a different host, set up a cronjob that does a wget of a page on your web-site. When that page is loaded, it does your script.

php function to schedule execution of a task at a certain time

I want to create a web based app, written in PHP that work like a reminder app, so the user enters a line of text and a time to reminded, app gets these two and at the specified time reminds the user. I implemented the program completely, but for scheduling, I have a problem: the program should do other jobs in between setting reminder and reminding user, but the only function that I find that can do something similar is sleep(), but it seems that sleep() is not the best choice because in between setting the reminder and reminding the user, the program could not to do anything.
Is there a function in php that enables scheduling tasks and at the specified time, the PHP program runs the task.
I do not want cron, crontab , scheduled task or any other OS-dependent solution.
It isn't possible to execute a task at specified time in PHP only.
You may create a condition comparing current time with the time the task should be executed and run it on every request, but that doesn't guarantee the execution at the exact time, since there may be no user requests at all.
This is what cron is for.
This is not possible in PHP.
You may check if your host provide a cron-like option (mine provide an URL call scheduling).
Another option, if users are always connected to your web app, is to manage alerts in javascript and/or ajax.

How to automatically call a php script every day to send out automatic emails

Is there a way where I can automatically call a php script after a specified interval.
I have a php script(say remindusers.php) that uses mysql to query a database where people have submitted their weekly reports. This script automatically queries the database and sends an email reminder to people who have not sent in their weekly reports yet.
What I am now supposed to do is give the ADMIN an option to set a reminder start and reminder end date during which calls should be automatically made to my remindusers.php script and cease on reminder end date.
What I learnt from SO/google is that I can setup cron (in LINUX) to automatically call my remindusers.php, but I dont have any shell access to do this.
Else Can I write another php script to essentially sleep every 24 hours and automatically wake up to call my remindusers.php script.
Are there any other built-in methods ?
Any ideas?
Use your site's visitors to trigger the event. Send a message and then check if 1 day elapsed. Then send another. You still need to pay attention not to double/triple/... send deu to synchronization.
When the time has elapsed use a MySQL (or system) MUTEX to ensure only one send occurs.
Yes you can! What you need to do is to use cron jobs. Cron jobs are essentially telling the server to execute a script (PHP or otherwise) at regular intervals. Cron jobs are very powerful and customizable, as you can set virtually any interval for your cron.
If you are using CPanel to manage your site, there is a button in CPanel to view all the cron jobs you have set. There is also a tutorial on that page.
Hope this helps.
Try with this PHPCron
PHPCron is a simple PHP script which lets you run multiple tasks on a schedule or timer. It can be run either from the command-line or via a web browser. Its behaviour is very similar to the popular cron program for UNIX.
http://katyscode.wordpress.com/2006/10/17/phpcron-running-scheduled-tasks-from-php-on-a-web-server/
I understand that you don't have Shell access but have you had a look at the cPanel to see whether there is an option to setup a cron job in there?

Creating PHP Bots for site maintenance

I have a PHP website in which, when a member visits a page, a series of database maintenance actions are made.
For example, in a common page, I've included a PHP script which checks how many posts every user has made and updates the database giving them points accordingly.
The problem with this method is that my website has 100+ members, and I'm worried that my scripts start to slow down as my memberbase grows.
Is there any way to code a bot in PHP, so my database can be updated without the user's intervention?
You should run a PHP file from within a cron job. Most PHP hosts including shared hosting provide cron access.
With cron you can schedule a task to run on an interval basis. This PHP program will then go through and do the updating that you require. So... take the code you do now and move it into a seperate PHP file and then tell cron to run it maybe once an hour or whatever you deem to be the correct interval.
For best performance, you need to update users table when he publish the post, not every time when need to know how many posts he published.
Create a cron to run daily (for instance) with the follow command:
php -q /home/cpaneluser/cron.php
And put a cron.php outside of public_html with all maintenance taks.
Or allow only administrators to do the maintenance tasks with a link in administrative panel.

Categories