创建一个shell文件,指定用户,然后要执行 php think xxx
或 php artisan xxx
等命令的时候,使用这个shell来执行
su root
切换到root用户(非必要)cd ~
进入家目录mkdir bin
自定义一个bin目录cd bin
touch tp233
创建一个tp233的文件,不需要后缀vim tp233
编辑这个文件,写入如下内容:#! /bin/bash
su -s /bin/bash -c "$1" www
vim /etc/profile
添加一个环境变量,使我们自定义的命令可以像 php -v
的方式全局调用export TP233=/root
export PATH=$TP233/bin:$PATH
source /etc/profile
刷新环境变量cd /www/wwwroot/my_blog
进入到项目,假设我这个是thinkphp6的项目tp233 ‘php think make:model User‘
使用刚创建的shell执行命令app/model
目录下 ll
打印一下就可以看到,User.php的所有组和创建者都是www用户了su -s /bin/bash -c "$1" www
这一句话,su -s
是指定shell,-c "xxx"
指定执行语句,再后面就是指定用户参考
【centos】【thinkphp】【laravel】指定其他用户身份执行 php think 或 php artisan 等命令
原文:https://www.cnblogs.com/mflnhg/p/14876793.html