Skip to content

Commit

Permalink
PHP 和 Shell 如何很好的搭配
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinywan committed Jun 18, 2018
1 parent 0bc4c33 commit 0c9c93c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
85 changes: 85 additions & 0 deletions PHP/php-shell_run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## PHP 和 Shell 如何很好的搭配

## shell 脚本
```bash
#!/bin/bash
#######################################################
# $Name: check_oss_cut.sh
# $Author: ShaoBo Wan (Tinywan)
# $organization: https://github.com/Tinywan
#######################################################

if [ $# -ne 3 ] ; then
echo 1 ; // 注意这个值才是php 执行函数接受到的返回值,而不是 exit
exit 1;
fi

if [ $# -ne 6 ] ; then
echo 2 ;
exit 2;
fi

exit 0;
```

## PHP 脚本
```php
/**
* 脚本没有执行权限 Permission denied
*/
const PERMISSION = 126;

/**
* 需要使用dos2unix命令将文件转换为unix格式
* eg:dos2unix file
*/
const DOS2UNIX = 127;

/**
* 脚本内部命令执行错误
*/
const INTERNAL_ERROR = 247;

const OUTPUT_MSG = [
0 => 'success',
1 => 'API Sign Error , Please task_id',
2 => 'Oss File Download Fail',
3 => 'FFmpeg cut/concat Video Fail',
4 => 'Rename file error, Disk is full',
5 => '截取缩略图失败,请检查视频开始、结束、视频截图时间',
];

/**
* 在退出时使用不同的错误码
*/
public function phpRunShellScript()
{
// 脚本路径
$scriptPath = $_SERVER['DOCUMENT_ROOT'] . "/shell/php-test.sh";
$scriptParam = '1 2 8';
$cmdStr = "{$scriptPath} {$scriptParam}";
echo $cmdStr;
// 执行
exec("{$cmdStr}", $output_result, $return_status);

// 返回码判断
if($return_status == self::PERMISSION ){
echo "chmod u+x ".$scriptPath."<br/>";
}elseif ($return_status == self::DOS2UNIX){
echo "需要使用dos2unix命令将文件转换为unix格式 <br/>";
}else{
// 这时候要根据脚本返回的第二个返回值判断脚本具体哪里出错误了
echo "脚本执行异常 MSg :" . $return_status . "<br/>";
if ( isset($output_result[1]) && $output_result[1] == 1) {
echo " MSg1 : ".self::OUTPUT_MSG[$output_result[1]]."<br/>";
} elseif (isset($output_result[1]) && $output_result[1] == 2){
echo " MSg2 : ".self::OUTPUT_MSG[$output_result[1]]."<br/>";
} elseif (isset($output_result[1]) && $output_result[1] == 3){
echo " MSg3 : ".self::OUTPUT_MSG[$output_result[1]]."<br/>";
}
}
echo 'success';
}
```


4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* [PHP脚本运行Redis](#PHP_Run_Redis)
* [PHP7中php.ini/php-fpm/www.conf的配置,Nginx和PHP-FPM的开机自动启动脚本](/PHP/PHP-FPM/config.md)
* [PHP 脚本执行一个Redis 订阅功能,用于监听键过期事件,返回一个回调,API接受改事件](/Redis-PHP/Php-Run-Redis-psubscribe/nohupRedisNotify.php)
* [PHP和Shell 脚本如何很好的搭配](/PHP/php-shell_run.md)

#### Linux 教程
* [Linux 基础知识](/Linux/linux-basic.md)
Expand All @@ -64,7 +65,8 @@
* [实战篇](http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html)
* [定时器教程](http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html)
#### Shell 教程
* [编写快速安全Bash脚本的建议](/Shell/write-shell-suggestions.md)
* [编写快速安全Bash脚本的建议](https://www.oschina.net/translate/bash-scripting-quirks-safety-tips)
* [写好shell脚本的13个技巧](https://mp.weixin.qq.com/s/f3xDHZ7dCQr7sHJ9KDvuyQ)
* [shell脚本实现分日志级别记录日志](/Nginx-Rtmp/Shell_Log.sh)
* [Nginx日志定时备份和删除](/Nginx-Rtmp/Shell_Nginx_Log_cut.sh)
* [SHELL脚本小技巧](/Nginx-Rtmp/Shell_script.md)
Expand Down

0 comments on commit 0c9c93c

Please sign in to comment.