Cron job runs 2 times and then stops - php

I m trying to configure a cron job via cpanel. What i want to do is execute a php script that updates a value in a mysql database table every minute. i tryed 2 cron commands :
wget -O – -q "URL_HERE"
and
usr/local/bin/php -q /home/user/public_html/filename.php
note that its a very small script with tiny execution time, so it should be ok.
for testing purposes I configured it to run every minute to see if it works.
Here is the problem:
the cron job runs ok for the first two times , but then it stops. for example if the time is 12:00, the script will run on 12:01, then on 12:02, but NOT on 12:03 .. 12:04 and so on. What could be the problem here?
thanks in advance

Make sure your crontab entry is correct...
For running a cron every minute, one needs to add entry something like
* * * * * "username" "task to execute"

Related

Best ways in which to conduct background tasks

Using PHP, I want to have scheduled tasks based upon the time the server is currently running.
Say at 7pm on Sunday I want a database query to be ran.
The way in which I've considered doing this is to put the task in the script that is ran on each page load in the session init.
Any ideas?
One method to automatically run a PHP script at specified time intervals is to use Crontab. This can be particularly beneficial for scripts that need to automatically update information without user interaction such as a script that gathers website statistics so that they can be emailed to you or a script that regularly retrieves content from another website.
See: PHP CLI and Cron
You can just use cron to trigger your script for you with something like this:
If you have ssh access you can add a crontab entry like this:
crontab -e
and enter something like this:
0 19 * * 0 php -f /path/to/script/file.php
where 19 is the hour (7pm), 0th day of the week (sunday), and 0 is the minute.
this will run at 7pm on sunday.
This is what your cron tab is for. You can specify whatever tasks, scripts, or other programs you need to run at whatever time you want to run them. Cron is run by your operating system and goes by your operating system's clock.
For example if I wanted to run a PHP script every minute, I would put something like the following into my cron tab.
* * * * * php /path/to/script.php
How you actually create cron entries depends on what your server setup is like. If you have some sort of shell access on a Unix/Linux system, you can edit your cron tab easily by running the following command.
crontab -e
That will bring up the crontab in the default text editor.
If you are using something like cPanel, you will have to consult the manual for information on how to edit cron entries.
The actual syntax for the cron can be complicated at first. There are entry generators online that you can use if you need help.
0 * * * * php /some/script.php # This will execute at the 0 minute of every hour.
0 1 * * * php /some/script.php # This will execute at the 0 minute of the first hour of every day. In other words, every day at 1AM.
You should use your systems Cron deamon to schedule and run a PHP file.
First read up on Cron:
https://help.ubuntu.com/community/CronHowto
Then implement this:
Open crontab:
vim crontab -e
Add an entry to your cron table:
0 19 * * 0 /path/to/php /path/to/script.php
Your script.php will contain the code/logic to query the database.

Cron job using codeigniter on an ubuntu server

I'm having trouble setting up a cron job.
I have a cron job created at a url like so:
myurl.com/cron
When that link is accessed, it is run.
How do I set it so that it runs automatically, every 10 minutes?
I can't figure out how to create a cron job. I know that that's what I need to do, but I can't figure out how to do it for a ubuntu server.
Login via SSH and in the shell, type the following:
crontab -e
Then, place the following line:
*/10 * * * * /path/to/command
Save the file and that command will be called every 10 minutes.

Send cron jobs using algorithm?

I'm working on a site in php. Is it possible to send cron jobs at times defined by an algorithm? Could I add such a functionally in the console or send it from my php script? I haven't used cron yet.
The first thing that comes to the mind - run a cron job every minute with php script which generates random number in order to check whether to run the rest of the job.
This line in crontab will run cron job every minute (replace paths with yours)
*/1 * * * * /usr/bin/php /path/to/job.php
Or if you can't directly call php, you may use curl or wget like that
*/1 * * * * /usr/bin/curl --silent --compressed http://yoursite.com/job.php
And in your php file do whatever you want to check if it's time to run the rest of the job, like that:
if(rand(1, 60)==1)
{
include 'the_actual_job.php';
}
Set your PHP script to be executed every hour, for example. Then add this code at the top of your script
sleep(rand(1, 60));
Now your script will be executed every hour + some seconds. It is random :)
If you want to schedule a job at a particular time you can use "cron"s friend "at" which will schedule a script a the time requested.
You simply fire off a command like:
$reqtime = '17.00'
system('at -f yourscript.sh ' . $reqtime);
And yourscript.sh will magically run at 17.00 today

set url to be excuted on paticular time event of a day cron job

I have Linux, PHP installed.
I want to set the cron job which will be executed 11:00 AM.
We can add cron job using crontab command
time of execution /usr/bin/php /var/www/html/somefile.php
I have experience about adding cron job like above using file path but now I need to add file url in cron job with some query parameter.
something like
time of execution /usr/bin/php www.example.com/somefile.php?do=sometask&with=me
I have tried above method with url also but it is not executing.
Please suggest
But
Many options. You could use curl.
/usr/bin/curl www.example.com/somefile.php?do=sometask&with=me &>/dev/null

How can I set up cron job in my site using PHP?

I want to mail after 6 hours automatically to my user who hasn't fully completed form on my website.
Help Me
Use crontab -e to edit the cron table for your account.
In the crontab, put an entry something like...
0,10,20,30,40,50 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php
or the equivalent
*/10 * * * * /usr/bin/wget -O - -q http://path.to/cron.handler.php
...which will run the cron handler php file every 10 minutes using wget (there are other options as well, and you may need to edit the command appropriately). (Note: you don't want to just run it every 6 hours, because then if someone happened to fill out the form right after it ran, it wouldn't have been 6 hours since they filled it out next time it runs, so you'd end up with 10-11 hour gaps.)
Then in your PHP file, find users who BOTH (a) haven't fully completed the form for at least 6 hours and (b) haven't been emailed yet. Send them an email, and mark them as having been emailed.
You will need to create the php script that does the checking and mailing, and then set the cron job like the following
/path/to/php -q /home/username/public_html/mycheckingscript.php
Obviously you will need to adjust the first path to point to your php binary, and the second path to point to the full location of your checking & mailing script.
I don't think you want to set the cron up using php. Instead write a php script and then have cron execute that script every hour or so. This would be something that is going to be dependent on your operating system.
For linux, here is the manpage for using crontab.
There is no way you can change/add a schedule on the cron job on the fly. according to my experience. because until now i did not find..

Categories