PHP——运行shell命令|脚本
内置函数

exec

shell_exec

passthru

system

配置
打开php.ini配置文件,并从disable_function将用到的函数从禁用中删除,然后重新载入或重启服务

脚本
脚本和小程序代码均位于public目录下
#!/bin/bash source /etc/profile time=`date +%Y-%m-%d-%H:%M:%S` cd /data/wwwroot/虚拟域名/public/miniprogram/ if [ -eq $? ] ; then
echo "$time INFO 进入目录成功" >> build_log.log
else
echo "$time ERROR 进入目录失败" >> build_log.log
exit
fi #删除之前编译文件 rm -rvf *.zip dist >> build_log.log if [ -eq $? ] ; then
echo "$time INFO 删除成功" >> build_log.log
else
echo "$time ERROR 删除失败" >> build_log.log
exit
fi #编译 npm run build wx $ $ >> /dev/null if [ -eq $? ] ; then
echo "$time INFO 编译成功" >> build_log.log
else
echo "$time ERROR 编译失败" >> build_log.log
exit
fi #打包 zip -r $-$time.zip dist >> /dev/null if [ -eq $? ] ; then
echo "$time INFO 打包成功" >> build_log.log
else
echo "$time ERROR 打包失败" >> build_log.log
exit
fi
配置权限
通过ps aux | grep nginx 可以知道nginx的用户为www

修改所属组和用户
chown -R www:www miniprogram/
chown -R www:www mini.sh
修改权限
chmod -R miniprogram/
chmod -R mini.sh
赋予权限
usermod -s /bin/bash www
PHP代码
shell脚本执行成功后会返回0
<?php namespace app\common\model; use think\Db; class WeixinShell extends Common
{ /**
* 打包小程序
* @param array $params
* @return array
*/
public function build($params = [])
{
$result = [
'status' => true,
'msg' => '编译成功',
'data' => [],
]; $version = $params['mini_version'];
$area = $params['mini_area'];
system("sh mini.sh {$version} {$area} ", $status);
$mini = glob("./miniprogram/*.zip");
$file = $mini['0']; $url = $_SERVER['HTTP_HOST'].'/';
if(isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))){
$http = 'https://';
}elseif(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) {
$http = 'https://';
}else {
$http = 'http://';
} if(file_exists($file)){
$result['data']['url'] = $http.$url.$file;
}else{
header("HTTP/1.1 404 Not Found");
} if($status){
$result['msg'] = '编译失败';
return $result;
}else{
return $result;
} } }
编译成功后返回URL地址,然后前端直接用window.location.href 可实现自动下载
成功示例

PHP——运行shell命令|脚本的更多相关文章
- 由Java代码运行shell命令/脚本
JDK有两种方法自带通Runtime.getRuntime().exec()和ProcessBuilder课上做, 后者是JDK1.5引进后,,政府还提出要放弃使用Runtime顺便做.现的时候就是採 ...
- 在R中运行Shell命令脚本(Call shell commands from R)
aaa.R Args <- commandArgs()cat("Args[1]=",Args[1],"\n")cat("Args[2]=&quo ...
- 4.Vim编辑器与Shell命令脚本
第4章 Vim编辑器与Shell命令脚本 章节简述: 本章首先讲解如何使用Vim编辑器来编写.修改文档,然后通过逐个配置主机名称.系统网卡以及Yum软件仓库参数文件等实验,帮助读者加深Vim编辑器中诸 ...
- 《Linux就该这么学》培训笔记_ch04_Vim编辑器与Shell命令脚本
<Linux就该这么学>培训笔记_ch04_Vim编辑器与Shell命令脚本 文章最后会post上书本的笔记照片. 文章主要内容: Vim编辑器 Shell脚本 流程控制语句 if语句 f ...
- 让你提前认识软件开发(23):怎样在C语言中运行shell命令?
第1部分 又一次认识C语言 怎样在C语言中运行shell命令? [文章摘要] Linux操作系统具备开源等诸多优秀特性,因此在很多通信类软件(主流开发语言为C语言)中,开发平台都迁移到了Linux上, ...
- PHP 反引号运行Shell命令,C程序
/********************************************************************* * PHP 反引号运行Shell命令,C程序 * 说明: ...
- java运行shell命令,chmod 777 xxx,改变权限无效的解决的方法。
在java程序中运行shell命令,改变文件的权限.能够在命令行中运行 chmod 777 <span style="font-family: Arial, Helvetica, sa ...
- Vim编辑器与Shell命令脚本
章节简述: 本章节将教给您如何使用Vim编辑器来编写文档.配置主机名称.网卡参数以及yum仓库 ,熟练使用各个模式和命令快捷键. 我们可以通过Vim编辑器将Linux命令放入合适的逻辑测试语句(if. ...
- 以root用户身份在jenkins中运行shell命令
以下过程是CentOS 1.打开此脚本(使用VIM或其他编辑器): vim /etc/sysconfig/jenkins 2.找到$JENKINS_USER并更改为“root”: $JENKINS_U ...
随机推荐
- 深入理解[Master-Worker模式]原理与技术
Master-Worker模式是常用的并行模式之一.它的核心思想是,系统由两类进程协作工作:Master进程和Worker进程.Master进程负责接收和分配任务,Worker进程负责处理子任务.当各 ...
- Python_内置函数2_44
字符串类型代码执行: exec('print(123)') eval('print(123)') print(eval('1*2+3+4')) # 有返回值 print(exec('1+2+3+4') ...
- 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题
Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...
- tortoisegit密钥与git密钥配置
在客户端生成密钥并将公钥上传到服务器可以避免每次连接git服务器都要登录的尴尬. 但git的私钥是不能直接用在tortoisegit上的,需要用tortoisegit的puttygen转换一下,详细过 ...
- java异常Exception
学习笔记: 一.程序的异常:Throwable 严重问题:Error ,我们不处理.这种问题一般很严重,不如内存溢出 问题:Exception 编译问题:不是RuntimeException异常.必须 ...
- mac下的快捷键
功能 快捷键 通用 打开新窗口 command + n 打开新标签 command + t 关闭标签 command + w 缩小 command - 放大 command + 全屏.取消全屏 com ...
- 墨者学院——密码学加解密实训(Base64转义)
地址:https://www.mozhe.cn/bug/detail/SW5ObnVFa05vSHlmTi9pcWhRSjRqZz09bW96aGUmozhe 在靶场中找到内容 解密 访问直接得key
- .net 报错汇总——持续更新
1.未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPla PM> Install-Package Microsoft ...
- JQuery/JS select标签动态设置选中值、设置禁止选择 button按钮禁止点击 select获取选中值
//**1.设置选中值:(根据索引确定选中值)**// var osel=document.getElementById("selID"); //得到select的ID var o ...
- Arrays.copyOf() 和 System.arrayCopy()分析
java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...