I am trying to create a system to monitor a Minecraft server using PHP. It will then periodically(every 10 minutes) check the status of the server. If the server is unreachable, it will time for how long it went down and then store that in a database.
I have created the code to check the server status but am struggling with timing for how long the server went off. What ways could I time the downtime ?
You cant calculate the downtime, I suggest that when a downtime is occured, you check it again in every one minute until the uptime, and get the difference of first down time and next uptime, this gives you a downtime with accuracy of one minute
you can update to the database the last up-time and calculate downtime with it and overwrite the downtime every time you calculate it
For this type of tasks, crontab is a great tool: it's a file that will execute defined commands at the time you set them.
you can use your isp's cpanel tool named cron jobs. from there it's pretty easy to create a cron job. just enter the command portion of the three lines below.
if you can't use this tool or prefer to write directly into your crontab, those lines are the resultant command (from http://www.pantz.org/software/cron/croninfo.html):
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0,10,20,30,40,50 * * * * php /home/yourdomain/public_html/test.php
my test.php:
mail('mail#internets.com','time();',date('H:i:s',time()));
this mails the time each ten minutes. from there on you patch your status checker code in test.php (or whatever name you want) and insert this data in database. I don't have enough information to provide code to calculate time. What is your data structure? How do you interact with your database?
Related
I have a database of scheduled tasks that'll be run based on the normal crontab-like syntax eg. min hour dom month dow (in separate table fields).
I'm not all that good at explaining myself, however what I would like to do is just set up one actual system crontab every minute, then that PHP file will then parse my database to look for all the scheduled tasks with crontab syntax, then when it hits the right time/day/etc, execute a function..... So, for example, if I set */10 for the minutes, then * for the rest, the function should be executed every 10 minutes, checked from the every minute running system cron.
To be honest, I am not sure how I go about actually parsing the database crontab-like syntax via PHP - like, take the example I just give, every 10 minutes. I know I'd need to record the last timestamp of the said task was run and the next timestamp it will be run - however, it's just initially working out when this next timestamp will be based on the crontab syntax I'll have issues with.
Hope I have explained this clearly, and thanks in advance.
I'm trying to create a YouTube-like service where the content creator is paid for their content however I'm a bit stuck.
The website needs to calculate the amount of views a content creator gets a month to calculate how much they need to be paid and then through PayPal API pay them by the end of the month. The problem is that this needs to be done even when the page is closed. I assume this is done on a server but I not sure if that's the case or even how to do it.
Thanks
Just googling real quick on cron, I found this website: https://www.pantz.org/software/cron/croninfo.html
I have used cron a lot before and the information here seems adequate to answer your question; it at least will arm you with the knowledge needed to do what is required.
An example for a cron job could be the following:
50 * * * * /myTask.php
What this says is "run this script 'myTask.php' every 50 minutes, every hour, every day of the month, every month, every day of the week" - simply put, run every 50 minutes.
The aforementioned task can be added to the crontabs file by using the following command from the terminal crontab -e
I found the solution. It turns out that the service hosting my website has an option to run cron jobs internally so I can make a script to loop through all the users, calculate their view earnings and pay them and run it at the beginning or each month.
I have a PHP-based site with bookings/appointments stored in a MySQL database.
I want to set an e-mail notification to be sent to each person who made a booking exactly 24 hours before their booking. Bookings are added through a regular PHP form.
I know I could add a script to, for instance, the index page that checks the database for any bookings that are in the final 24 hours, but that's unreliable since I won't be getting much traffic at first. So the index page could go hours without visits, and the notification would be hours late.
The other solution that came to mind is to set a cron job that runs every minute, calls a PHP script which checks whether any e-mails should be sent and sends them. But I'm not sure if this is overkill in a way; does anyone have a better solution than having something run in the background every minute?
To sum it up - is there a way to do this without cron?
Triggering the job from a web page is a very bad idea, for two reason: (1) if you don't get traffic to your site, the job doesn't run; (2) if you get a lot of notifications, the job will be slowing down the response to the web requests (assuming you invoke the job synchronously).
I would strongly discourage you from running a job every minute either - it definitely will be an overkill. Instead, think whether you really need "exactly 24 hours" as the interval or would "between 22 and 26 hours" be ok.
We have a similar requirements - and went about it by setting a job that runs every 4 hours and checks what notifications need to be sent for events starting between 22 and 26 hours form the time the script runs. This way, the script is only execute 6 times in a day and everything gets sent correctly.
If 4 hours approximation is not good enough, then think to the largest interval that's appropriate. I'm sure 1 hour should be sufficient. Have a script run once an hour (from cron) and check for events starting between 23 and 24 hours from the time of the run.
Remember that once your email is sent, it doesn't end up in the recipient's inbox immediately: sometimes it takes a few seconds, but sometimes it may take an hour or even more - so an extra hour difference in your script won't be a problem.
You don't need to use cron as an interval'd timer. You can set a very specific date and time when you want your job done.
Here's an article on the subject
For instance:
0 0 18 5 * <php command here>
Will run every May 18th at midnight. That's more than enough time to clear it before the next iteration (next year).
there is no way other than setting cron or sending request to server through periodic calls...below is post similar to your question, you may get idea.
Live redirect based on periodic server calls with JSON or AJAX
Thanks
a cron job every minute has no sense ! but you can do a cron job every hour because i think it doesn't meter a hour difference or 2 hours . without cron it isn't any other way . it will take about 2 second (at max) to complete so it is worth
I'm new to PHP, so I need some guidance as to which would be the simplest and/or elegant solution to the following problem:
I'm working on a project which has a table with as many as 500,000 records, at user specified periods, a background task must be started which will invoke a command line application on the server that does the magic, the problem is, at each 1 minute or so, I need to check on all 500,000 records(and counting) if something needs to be done.
As the title says, it is time-critical, this means that a maximum of 1 minute delay can be allowed between the time expected by the user and the time that the task is executed, of course the less delay, the better.
Thus far, I can only think of a very dirty option, have a simple utility app that runs on the server, that at each minute, will make multiple requests to the server, example:
check records between 1 and 100,000;
check records between 100,000 and 200,000;
etc. you get the point;
and the server basically starts a task for each bulk of 100,000 records or less, but it seems to me that there must be a faster approach, something similar to facebook's notification.
Additional info:
server is Windows 2008
using apache + php
EDIT 1
users have an average of 3 tasks per day at about 6-8 hours interval
more than half of the tasks can be at least 1 time per day executed at the same time[!]
Any suggestion is highly appreciated!
The easiest approach would be using a persistent task that runs the whole time and receives notification about records that need to be processed. Then it could process them immediately or, in case it needs to be processed at a certain time, it could sleep until either that time is reached or another notification arrives.
I think I gave this question more than enough time, I will stick to a utility application(that sits on the server) that will make requests to a URL accessible only from the server's IP which will spawn a new thread for each task if multiple tasks needs to be executed at the same time, it's not really scalable but it will have to do for now.
I've got a PHP script on a shared webhost that selects from ~300 'feeds' the 40 that haven't been updated in the last half hour, makes a cURL request and then delivers it to the user.
SELECT * FROM table WHERE latest_scan < NOW() - INTERVAL 30 MINUTE ORDER BY latest_scan ASC LIMIT 0, 40;
// Make cURL request and process it
I want to be able to deliver updates as fast as possible, but don't want to bog down my server or the servers I'm fetching from (it's only a handful).
How often should I run the cron job, and should I limit the number of fetches per run? To how many?
It would be a good thing to "rate" how often each feed actually changes so if something has an average time of 24 hours per change, then you just fetch is every 12 hours.
Just store #changes and #try's and pick the ones you need to check... you can run the script every minute and let some statistics do the rest!
On a shared host you might also run into script run time issues. For instance, if your script runs longer than 30 seconds the server may terminate. If this is the case for your host, you might want to do some tests/logging of how long it takes to process each feed and take that into consideration when you figure out how many feeds you should process at the same time.
Another thing I had to do to help fix this was mark the "last scan" as updated before I processed each individual request so that a problem feed would not continue to fail and be picked up for each cron run. If desired, you can update the entry again on failure and specify a reason (if known) why the failure occurred.