PHP- How to run cron jobs one after another like job queue - php

How to run cron jobs one after another, like job queue ?
I am working on project which has near about 20+ cron jobs, most of them starts every hour and are very DB intensive, so i would like to have something like job queue which will start one cron job after another ,so not all 20+ job start at the start of new hour and make my server unavailable.
Is there per-built system for something like this ?
or any other easy way to add job queue functionality to the linux cron. ?

Write a bash script to run the commands in sequence, and add that to the crontab.
Eg create a file called something like phpjobs.sh with the commands to run the jobs in order:-
#!/bin/bash
/usr/bin/php -q <yourfile1.php>
/usr/bin/someothercommand
/usr/bin/php -q <yourfile2.php>
/usr/bin/php -q <yourfile3.php>
then make it executable
chmod 755 phpjobs.sh
Then add it to the crontab to run every hour
0 * * * * root /path/to/phpjobs.sh

Related

How to process php artisan queue:listen

I am facing a serious problem in Laravel Queue system please help me to fix this issue.
Once I queue my mail by using
$mailer = Mail::to($email_to)->queue(new ContactGeneral($data));
it stores into the database and runs this command from terminal php artisan queue:listen it works fine once I close my terminal it does not listen to my queue.
For that, I set up a scheduled in kernem.php file like that which run in every minute
protected function schedule(Schedule $schedule){
$schedule->command('queue:listen')->everyMinute();
}
set this line in a cronjob and work fine
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Problem is as runs every minute run every minute it not kill the previous process and run another process in next minute it slowdown my server
Please can you let me know what is the best way to implement this
Thanks in advance
Best way is to use supervisor. Though if you are running the application in a shared hosting environment, you can process the queues once and then exit the process thus freeing up the memory by using the following command:
php artisan queue:work --once
Depending on how many queues you'll have, set the queue to run once every 1, 2 or 3 minutes to make sure the previous process has time to consume the queues and they won't interfere often. I think you can use the following command:
* * * * * cd /path-to-your-project && php artisan queue:work --once
No, you don't need to schedule this process
as long as queue:work process is running he will look at your "jobs" table and run task by task
what you need is something to make sure that the process doesn't end when you close the console, as user8555937 and Webinion said you need supervisor and its configuration file, once you run it it will run in the background and you can forget about it

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 -

How to run cron job in php

I have php function that I wnat to run every 15 minutes. I found some questions like this:
How can I make a cron to execute a php script?
and in my case I should use this:
*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p
But should I run this command in terminal or put it in my file? And once, it is executed, will it run all the time or will have time limit?
Thanks!
PHP doesn't run cron jobs, your server (or operating system) is doing this. There are two ways to put your cron job to work:
#1
Using the shell command crontab. The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). crontab -e will open an editor window where you can put in a you cron job as a new line. Save, and your cron job is now running. crontab -l again and you will see it listet. crontab -r to remove all the cont jobs.
You can also start a cron job from a file. Simply type crontab filename (eg. crontab textfile.txt)
Alternatively you can also start it from within PHP. Just put your cron job into a file and start it via exec() like so:
file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );
#2
If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob) and put your cron job there. In this case you probably want to run it as a user (not as admin, that would be rather insecure). This is quite easy to do, just add the user name like so:
*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p
(In this case the cron job will not be listet under crontab -l)
Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes.
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script. More on this later.
Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

Create a cron job with user define dtime

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()

Execute PHP script in cron job

In our centos6 server. I would like to execute a php script in cron job as apache user but unfortunately it does not work.
Here is the edition of crontab (crontab -uapache -e)
24 17 * * * php /opt/test.php
and here is the source code of "test.php" file which works fine with "apache" user as owner.
<?php exec( 'touch /opt/test/test.txt');?>
I try to replace php with full path of php (/usr/local/php/bin/php) but also it doesn't work.
Automated Tasks: Cron
Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.
You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.
Configuring Cron Tasks
In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:
*/10 * * * * /usr/bin/php /opt/test.php
In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.
* is a wildcard, meaning "every time".
Start with finding out your PHP binary by typing in command line:
whereis php
The output should be something like:
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
Specify correctly the full path in your command.
Type the following command to enter cronjob:
crontab -e
To see what you got in crontab.
EDIT 1:
To exit from vim editor without saving just click:
Shift+:
And then type q!
I had the same problem... I had to run it as a user.
00 * * * * root /usr/bin/php /var/virtual/hostname.nz/public_html/cronjob.php
You may need to run the cron job as a user with permissions to execute the PHP script. Try executing the cron job as root, using the command runuser (man runuser). Or create a system crontable and run the PHP script as an authorized user, as #Philip described.
I provide a detailed answer how to use cron in this stackoverflow post.
How to write a cron that will run a script every day at midnight?
I tried all combinations with PATHs, but don't work. Probably they are needed.
In my case, with Centos 7, a reboot or server worked.

Categories