一,代码:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class IndexAllCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:index-all-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
echo "开始执行命令:\n";
// 获取当前执行用户的用户ID
$uid = posix_geteuid();
// 获取用户信息
$user_info = posix_getpwuid($uid);
// 打印用户名
echo "当前用户:".$user_info['name']."\n";
//得到当前脚本的所有者即owner
$scriptOwner = get_current_user();
echo 'Current script owner: ' .$scriptOwner."\n" ;
}
二,执行结果:
用root用户执行时:
root@lhdpc:/data/api# php artisan app:index-all-command
开始执行命令:
当前用户:root
Current script owner: liuhongdi
用runuser换一个用户执行:
root@lhdpc:/data/api# runuser -u www-data php artisan app:index-all-command
开始执行命令:
当前用户:www-data
Current script owner: liuhongdi
可以看到执行用户和脚本所有者不一定是一个人,要区分开来
标签:laravel,脚本,www,root,app,用户,linux,执行,data From: https://www.cnblogs.com/architectforest/p/18344629