Laravel and Shell planning tasks

Source: Internet
Author: User
Tags closure laravel forge

PHP itself does not have a timer function,PHP can not be multithreaded. PHP's scheduled task function must be combined with other tools to achieve

Linux the script implementation

How to use :

crontab Filecrontab [-u user ] [-u user ] {-l |-r |-e}

crontab is used to allow the user to execute the program at a fixed time or at a fixed interval.

Use crontab to write shell scripts, and then let php call the shell, this is the use of Linux features, should not be considered the characteristics of PHP's own language

XX * * * * lynx-dump https://www.yourdomain. com/script.php

The 1 column represents minutes 1~59 per minute or */1 ,/nmeans every n minutes, for example */8 is every 8 minutes, and the following analogy

column 2 indicates hours 1~(0 means 0 points)

column 3 represents the date 1~

Column 4 shows the month 1~

Column 5 identification number week 0~6 (0 = Sunday)

This means that the URL is accessed through Lynx . We use lynx,curl,wget for remote access to URLs, and if you want to improve efficiency, it is best to use PHP directly to execute local PHP files, for example:

XX */2 * * */usr/local/bin/php/home/www/script.php

This statement can be executed every 2 hours in 0 minutes, through the Linux internal PHP environment script.php, note that this is not through the URL access, through the server environment to execute Oh, but directly execute, Because the server environment is bypassed, the efficiency is certainly much higher.

PHP Implement

Ignore_user_abort(True);

when accessing this php via a URL , even if the user turns the browser off (disconnected)

1190000002955509

Laravel of Scheduled Tasks

In the past, developers need to write a Cron entry for every task that needs to be scheduled , which is a very frustrating thing to do. Your task Scheduler is not in source control, you must use ssh to log on to the server and add these cron entries.

1 Start

Task scheduling is defined in the schedule method of the app/console/kernel.php file , and an example is already included in the method. You are free to add the dispatch task you need to the schedule object.

You can define all scheduling tasks in the schedule method of the App\console\kernel class.

1 In this example, we will dispatch a called closure at midnight every day. In this closure we will execute a database query to clear the table:

protected function Schedule(schedule $schedule)

{

$schedule->call(function () {

Db::table(' recent_users ')->delete();

})->daily();

}

2 In addition to dispatching closure calls, you can also dispatch Artisan commands and operating system commands. For example, you can use the command method to dispatch a artisan command by using the name or class:

$schedule->command(' emails:send--force ')->daily();

$schedule->command(Emailscommand::class, ['--force '])->daily();

The 3exec command can be used to send commands to the operating system:

$schedule->exec(' node/home/forge/script.js ')->daily();

2 Calls

Turn on the scheduler

Here's the only cron entry you'll need to add to the server, If you don't know how to add a cron entry to the server, consider using a service such as Laravel Forge to manage Cron entries:

* * * * * * Php/path/to/artisan schedule:run >>/dev/null 2>&1

The Cron will call the Laravel command Scheduler once every minute, and thenLaravel Evaluate your dispatch task and run the task that expires.

3 Details

Scheduling common options

Of course, you can assign multiple dispatches to tasks:

Method Description

->cron (' * * * * * *'); to run a task on a custom cron schedule

->everyminute(); run a task every minute

->everyfiveminutes(); run a task every five minutes

->everytenminutes(); run a task every 10 minutes

->everythirtyminutes(); run a task every 30 minutes

->hourly (); Run a task once per hour

->daily (); Run a task 0 o'clock in the morning every day

->dailyat(' 13:00 '); 13:00 running Tasks every day

->twicedaily(1); Daily & 13:00 running tasks

->weekly (); Run a task once a week

->monthly (); Run a task once a month

->monthlyon(4, ' 15:00 '); 4 15:00 per month run one task

->quarterly(); Run once per quarter

->yearly(); Run once a year

->timezone(' america/new_york '); Setting the time zone

$schedule->call(function () {

13:00 Run once a week in Monday ...

})->weekly()->mondays()->at (' 13:00 ');

Run hourly from 8 AM-5 PM on weekdays ...

$schedule->command(' foo ')

->weekdays()

->hourly()

->timezone(' America/chicago ')

->between(' 8:00am ', ' 17:00 ');

->weekdays (); Run tasks only on weekdays

->sundays (); Run a task every Sunday

->mondays (); Run a task every week

->tuesdays (); Run tasks every Tuesday

->wednesdays(); run tasks every Wednesday

->thursdays(); run tasks every Thursday

->fridays (); Run tasks every Friday

->saturdays(); run tasks every Saturday

->between($start, $end); run a task based on a specific time period

->when(Closure); run a task based on a specific test

Constraints of the Truth test

The When method is used to restrict the execution of a task based on the result of a given truth test. In other words, if a given closure returns true, the task executes as long as no other constraints prevent the task from running:

$schedule->command (' emails:send ')->daily()->when(function () {

return true;

});

Skip method and when in contrast, if the Skip method returns True, the dispatch task will not execute:

$schedule->command (' emails:send ')->daily()->skip(function () {

return true;

});

4 Avoiding overlapping tasks

By default, even if the previous task is still running scheduled tasks, you can avoid this by using the withoutoverlapping Method:

$schedule->command(' emails:send ')->withoutoverlapping();

In this example, theArtisan command emails:send runs every minute if the command is not running. The Withoutoverlapping method is useful if your task often changes drastically during execution, and you don't have to predict how long a given task will take.

5 Results

Task output

The Laravel Scheduler provides several convenient methods for processing scheduling task output. First, using the Sendoutputto method, you can send output to a file for later inspection:

$schedule->command(' emails:send ')

->daily()

->sendoutputto($filePath);

If you want to append output to a given file, you can use the Appendoutputto method:

$schedule->command(' emails:send ')

->daily()

->appendoutputto($filePath);

Using the Emailoutputto method, you can send the output to an e-mail message, noting that the output must first be sent to the file through the Sendoutputto method. Also, before sending the task output using e-mail, you should configure the Laravel e-mail Service :

$schedule->command(' foo ')

->daily()

->sendoutputto($filePath)

->emailoutputto(' [email protected] ');

Note: the Emailoutputto and Sendoutputto methods are only valid for the command method and do not support the call method.

6 Task Hooks

1 using the before and after methods, you can specify the code to execute before and after the dispatch task finishes:

$schedule->command(' emails:send ')

->daily()

->before(function () {

Task is on to start ...

})

->after(function () {

Task is complete ...

});

2ping URL

Using the pingbefore and thenping methods, the scheduler can automatically ping a given URL before and after the task is completed . This method is useful when notifying an external service, such as Laravel Envoyer, when a scheduled task starts or finishes:

$schedule->command(' emails:send ')

->daily()

->pingbefore($url)

->thenping($url);

Use pingbefore($url) or thenping($url) feature to install HTTP Library guzzle, you can use the Composer Package Manager to install guzzle to the project:

Laravel and Shell planning tasks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.