How to add Multiple cron job using php? - php

What i try to do >
1.User visit my PHP base Page
2.My page collect its data
3.Add cron job using PHP (Q1.How to add Cron job using PHP? )
(this type of cron job command currently i am using ""/usr/bin/wget myweb.org/some_directory/file.php?uid=2738 >/dev/null""
4.Cron job runs a above URL and do a task with user id which takes almost 1 hour + time (Q2. Is it possible to run job at the same time or after one minute if possible how)
5.If another user visit the page on the same time above procedure is also happen for them too (Q3.So this is possible)

Sounds to me that what you need is not a cron job but a background worker process. Cron jobs are jobs that are run at a specific automated time (such as 2:30 PM every day), not every time a user visits your page which could be whenever.
In order to instantiate background worker processes, I strongly recommend using Pheanstalk. Check out the readme to see how to instantiate a worker process, it's pretty easy.

Related

Run laravel jobs one time at specific time only

I came across a situation i want to trigger some code at specific time, i.e when user does booking, the freelancer must accept/reject the booking request, if he doesnt, after x duration (15* mins lets say) it would be rejected and user would get push notification. All code is done but currently im running a cronjob after each 1 minute which checks for any unresponded bookings and checks when their time (15mins, dynamic) passed so then I execute my code for afterward, it is not good i guess as its running db queuries over and over each minute.
I'm aware with laravel queue jobs as well but didnt see anything for me to run that job for a specific time only (i.e execute this job after 15mins, if it isnt responded, reject it)
have you looked at Queue delay?
https://laravel.com/docs/9.x/queues#delayed-dispatching
This sounds like what you are looking for, I would just trigger the queue and delay when they make a booking so it executes 15 minutes after.
Use scheduled tasks.
use App\Console\Commands\SendEmailsCommand;
$schedule->command('emails:send Taylor --force')->daily();
$schedule->command(SendEmailsCommand::class, ['Taylor', '--force'])->daily();
https://laravel.com/docs/9.x/scheduling#scheduling-artisan-commands

How to add dynamic cron job for php

I want to run php script at the particular time and then update the time to next interval. Is it possible to set cron job time duration from php script ?
For Example:
1 - set cron to run at the next sunday,
2 - then update the cron time to another particular time from php,
3 - so on..
You can set your cron job to run at a constant interval (every minute, every hour, etc.), and within the job, check if the action needs to be performed. Laravel framework, for example, does this with their scheduler. There is a single entry into the crontab, and within the job itself, it determines what it actually needs to run.
To keep track of the job internally, you can use a database to store the next run time. If the current time is greater than the next run time, you perform the action and update the database based on your rules for determining the next interval.

Is is possible to trigger an event in mongoDB on a particular timestamp?

I need to have a central countdown timer for my website which will be common for all users.
This countdown timer will be reloaded after cycle of 7days.
Which means,the database will have a collection that stores-
StartDate: ,StartTime: ,EndDate: , EndTime:
And when the endtime is reached one cycle ends and the next cycle of 7days begins for which the same fields are updated. How to trigger command from shell and not from the php code. Also, is it possible to manipulate it using php?
There is no thing such as a SQL trigger in MongoDB. Although there are some tricks for creating triggers. See the following question and answer explaining how to create one:
How to listen for changes to a MongoDB collection?
However the correct way to do this is creating a cron job. A cron job is a script that is run in Unix and can be triggered periodically (e.g. every Friday night).
You can find more info about cron in the following link explaining what a cron job is:
http://www.unixgeeks.org/security/newbie/unix/cron-1.html
And if you want to create one, follow the following guide:
http://www.thesitewizard.com/general/set-cron-job.shtml

Run PHP script at a certain ( dynamic ) time ? Alternatives to cronjob

I'm working on a service where the user schedules his tweets so for example i want to post a tweet tomorrow at 12:30 PM. How can that be done ? is cron jobs the best way to do so ? like running a cronjob every 5 minutes and see if there are tweets to post in this interval ? Are there any alternatives ?
Running a cron job is definitely the easiest solution, however there are other approaches available, one such approach would be to use a queue like Amazons SQS
This lets you simply throw things onto a queue to be processed later, by default they are immediately added to the queue in a state ready to be processed immediately, however you can add items to the queue with a timestamp they should remain dormant until. This would be your users predefined tweet time.
You could then have a script running that is constantly listening to the queue for any new tweets that need to be sent, as soon as a queue item becomes available, it will be processed.
The downside to this is of course that it's more effort, the upsides though are that you can scale easier since you can have multiple machines processing tweets and they wont ever send out the same tweet twice (whereas if two machines are running the same cron, there's the chance they'll both send out the tweet)

PHP start task for certain time

Im having a small problem i searched a while about it but didnt found any good aswer for my problem , so here it is:
I have a PHP script with user database and i want users to have a small button that will make a certain task execute for a certain time ( imagine 2 hours ) , and this task will keep running till the job is done. The user can stop the task and hit continue , and the task will stop executing after 2 hours!
Any ideia how i can do it?
My actual hosting have crontab.
Well you haven't explained your problem in enough details , but you should this generally :
when users post tasks , write these tasks in a database table, with other information such as start time and end time (and you can update these due to user new input), then write a script which reads these tasks from database and compares current time to start time and if it is time to run, runs the task.
now put this single script in a cron job
This is actually a better idea than creating a cron job for each user defined task

Categories