当前位置:网站首页>php执行shell脚本
php执行shell脚本
2022-07-26 09:23:00 【为天空着色】
原文:https://blog.csdn.net/zhangsheng_1992/article/details/52805760
php作为一门服务器端脚本语言,也是可以调用系统命令的,这其中就包括了执行shell脚本
编写test.sh脚本如下
#! /bin/sh
mkdir /www/a
很简单的一个shell脚本 创建一个文件夹
php脚本如下
system('/usr/bin/test.sh')
在运行前 首先需要验证的
1.test.sh脚本是否具有可执行权限
2.apache nginx phttp等web服务器是否有test.sh的权限
3.由于是同步文件命令 所以需要确保用户对要下载的目录有写入权限
4.由于我使用php版本高于5.4,已经删除了安全模式一说,说一也不存在运行在安全模式下
5.执行的脚本请使用绝对路径 这点很重要
但是 实际运行后 我发现输出了faild 说明脚本并没有执行成功 查了下原因 是因为apache用户不具备某些目录的执行权限(没个用户有没个用户的可执行目录,你的脚本需要在该目录下) 切换目录属主容易造成安全问题 在linux下 root用户是不受此限制的 所以 临时让用户可以切换到root权限 既可以简单的规避此类问题 如何操作呢?
首先 查看你的apache用户或者nginx php-fpm用户
可以使用 ps -ef | grep httpd 命令来查看 其他同理
经查我的apache用户为_www用户
然后 visudo 或者 vim /etc/sudoers 找到
## Allow root to run any commands anywhere
root ALL=(ALL) ALL这一行 在下边追加
_www ALL=(root) NOPASSWD:ALL
第一列是用户 第二个是可以切换到的用户 我这里写的是root 如果你想让随意切换可以在这里写All 第三个是可以执行的脚本路径 ALL 表示所有
最后 重启apache让用户重新获得权限
修改php脚本
system('sudo /usr/bin/test.sh')
运行 执行成功~~~
边栏推荐
- Qtcreator reports an error: you need to set an executable in the custom run configuration
- Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
- Where are the laravel framework log files stored? How to use it?
- Byte buffer stream & character stream explanation
- (2006,Mysql Server has gone away)问题处理
- 围棋智能机器人阿法狗,阿尔法狗机器人围棋
- NTT (fast number theory transformation) polynomial inverse 1500 word analysis
- Local cache
- Sending and receiving of C serialport
- VS2019配置opencv
猜你喜欢

Go intelligent robot alpha dog, alpha dog robot go

2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation

神经网络与深度学习-6- 支持向量机1 -PyTorch

【Mysql】一条SQL语句是怎么执行的(二)

李沐d2l(五)---多层感知机

STM32+MFRC522完成IC卡号读取、密码修改、数据读写

ZXing简化版,转载

Use of off heap memory

【线上问题】Timeout waiting for connection from pool 问题排查

arcgis的基本使用1
随机推荐
Original root and NTT 5000 word explanation
What is asynchronous operation
el-table实现增加/删除行,某参数跟着变
[use of final keyword]
OFDM 十六讲- OFDM
Windows backs up the database locally by command
Thread Join 和Object wait 的区别
Unity topdown character movement control
2B和2C
CF1481C Fence Painting
redis原理和使用-基本特性
什么是异步操作
Simple message mechanism of unity
839. Simulation reactor
“could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32” 问题处理
JS output diamond on the console
Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
QT | about how to use EventFilter
Server memory failure prediction can actually do this!
Codeworks DP collection