Create a cron job with user define dtime - php

I want to create a cron job which will run with the user define time in database.
For eg.User can set start time and end time in the database .When the end time is reached I want cron to trigger one script to send mail.First of all Is it possible in cron or I have to go with some different approach ? and this all things will be done on AWS EBS.
Below is what I tried on my local machine to just send a simple mail which is too basic
*/1 * * * * /usr/bin/php -q/var/www/html/cronTry/cron.php

You dont want to run the CRON every minute instead create a specific job for your user and run it once. Something like this should work (untested - on mobile)
$your_users_date;
$cmd = "sudo crontab -l | { cat; echo ". date("i H d m",strtotime($your_users_date))." * php /path_to_your_script.php arg1 arg2; } | crontab -";
shell_exec($cmd);
This will append a job to the crontab at the time your user has defined

Get the dynamic details from the database, create the command string and
You can easily run your cron by passing the command in the shell_exec()

Related

Can I modify the schedule of a cron job from a php script that it is being executed by another cron job?

For example I have a cron running on the 1st of every month, this cron executes a PHP script that returns some dates.
I want to use these dates and modify the schedule of another cron job?
I know I can add a new cronjob doing something like this but unsure of how to update 1 specific entry when there could be multiple.
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
Make sure you do not break something you better monitor your cronjobs after you deploy this 😉 But what you want to do is possible.
You can create, modify and delete Cronjobs via PHP (Use PHP to create, edit and delete crontab jobs?)
This is also true if this PHP is executed via another cronjob (as long as you give the correct permissions)
⇒ It is possible (q.e.d.)
Also see this question: https://askubuntu.com/questions/408611/how-to-remove-or-delete-single-cron-job-using-linux-command
This is how you remove a single cronjob (by example) - just create a new one for the different dates:
crontab -u mobman -l | grep -v 'perl /home/mobman/test.pl' | crontab -u mobman -

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

How do I use cron to run a database cleaning script?

I have a database with a bunch of links that I want to keep updated. Basically if a link returns a 404 error code I want to remove it from the database. I have a script that I am using however I need to run it manually. How can I make this work using CRON?
in your shell as cron user (or root):
crontab -e
This will bring up your crontab file in your editor. Add a new line something like this:
* */12 * * * /path/to/script
Save/exit the file.
Now for a quick lesson on cronjobs:
-The first 5 arguments in the line tell how often, or when the cron daemon will execute the 6th argument.
-From left-right, arguments represent: minutes, hours, days, weeks, months
-An asterix (*) tells the cron to run on all values of it's associated time measurement (example * * * * * means to run every minute, of every hour, of every week, and of every month!)
In my example, * */12 * * * means to run every 12 hours.
Check out: http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
To run a PHP script with cron you can use the PHP executable and the path to the script.
On most linux systems you want to edit your cron file (the crontab) with the command crontab -e. This will open up a command line based editor and you can just append your new job to the bottom of the file using this format.
<minute> <hour> <day_of_month> <month> <day_of_week> php /path/to/script
If the commands dont work for you let me know what distribution you are using and I can modify the instructions.
/usr/bin/php -q /home/user/public_html/script.php

Categories