我有一个 API 路由可以更新我的数据库,
这是我想通过 cronjob 执行的路由结构,
@extends ('layout')
@section ('content')
<script>
/*--------------------------------------*/
/* Start Get Employees Data and Update */
/*------------------------------------*/
getDataFs();
function getDataFs()
{
var _token = $("input[name='_token']").val();
$.ajax({
url: 'https:/api/', /* API Url of JSON Data from Hosts */
type: 'GET',
dataType: 'json',
xhrFields: {
withCredentials: true
},
success: function(response){
console.log(response);
for(i=0; i < response.length; i++)
{
$.ajax({
url: '/api/update_list',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: 'POST',
dataType: 'json',
data: { 'employee_data': response[i], '_token': _token },
success: function(response){
console.log(response);
}
})
}
}
});
}
/*--------------------------------------*/
/* End Get Employees Data and Update */
/*------------------------------------*/
</script>
@endsection
在我的 API 路由中,我有一个从另一个 API 获取数据的 ajax 请求,
我知道可以使用 sh 脚本或 perl 脚本运行 php,并在创建的 cronjob 上添加 .sh
或 .pl
脚本,
我想知道的是,是否可以在 Linux 上的 shell 脚本/perl 脚本
中运行 laravel 路由?
/usr/bin/php myscript.php 到/usr/bin/php laravel/route
如果是这样,你能给我一个工作示例脚本,其中包含我的 laravel 路由的路径吗,
最佳答案
经过一个小时的研究,我一点一点地研究解决了我的问题,
这是我做的,
对于 Laravel 5.4 - 在终端中执行
php artisan make:command CustomCommand --command=command:name
转到
app/Console/Kernel.php
定位调度函数 并将其添加到
$schedule->command('command:name')->daily();
中
转到
app/Console/Commands/CustomCommand.php
添加“用户数据库;”并设置 $signature
protected $signature = 'command:name';
set $description = 'This will update employees';
设置句柄
公共(public)函数句柄()
{
DB::table('users')->where('id','25')->update(['email'=>'sample@gmail.com']);
$this->info('数据更新成功!');
去终端
转到项目目录
运行'php artisan command:name'
crontab -e
设置分、时、日、年、月 php/path/to/project/artisan update:employee
例如* * * * * php/var/html/laravelproject/artisan 命令:名称
关于php - 我可以在 Linux Cron Job/Crontab 上运行 Laravel Route 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47689001/