Skip to content

Commit 0c9c93c

Browse files
committed
PHP 和 Shell 如何很好的搭配
1 parent 0bc4c33 commit 0c9c93c

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

PHP/php-shell_run.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
## PHP 和 Shell 如何很好的搭配
2+
3+
## shell 脚本
4+
```bash
5+
#!/bin/bash
6+
#######################################################
7+
# $Name: check_oss_cut.sh
8+
# $Author: ShaoBo Wan (Tinywan)
9+
# $organization: https://github.com/Tinywan
10+
#######################################################
11+
12+
if [ $# -ne 3 ] ; then
13+
echo 1 ; // 注意这个值才是php 执行函数接受到的返回值,而不是 exit
14+
exit 1;
15+
fi
16+
17+
if [ $# -ne 6 ] ; then
18+
echo 2 ;
19+
exit 2;
20+
fi
21+
22+
exit 0;
23+
```
24+
25+
## PHP 脚本
26+
```php
27+
/**
28+
* 脚本没有执行权限 Permission denied
29+
*/
30+
const PERMISSION = 126;
31+
32+
/**
33+
* 需要使用dos2unix命令将文件转换为unix格式
34+
* eg:dos2unix file
35+
*/
36+
const DOS2UNIX = 127;
37+
38+
/**
39+
* 脚本内部命令执行错误
40+
*/
41+
const INTERNAL_ERROR = 247;
42+
43+
const OUTPUT_MSG = [
44+
0 => 'success',
45+
1 => 'API Sign Error , Please task_id',
46+
2 => 'Oss File Download Fail',
47+
3 => 'FFmpeg cut/concat Video Fail',
48+
4 => 'Rename file error, Disk is full',
49+
5 => '截取缩略图失败,请检查视频开始、结束、视频截图时间',
50+
];
51+
52+
/**
53+
* 在退出时使用不同的错误码
54+
*/
55+
public function phpRunShellScript()
56+
{
57+
// 脚本路径
58+
$scriptPath = $_SERVER['DOCUMENT_ROOT'] . "/shell/php-test.sh";
59+
$scriptParam = '1 2 8';
60+
$cmdStr = "{$scriptPath} {$scriptParam}";
61+
echo $cmdStr;
62+
// 执行
63+
exec("{$cmdStr}", $output_result, $return_status);
64+
65+
// 返回码判断
66+
if($return_status == self::PERMISSION ){
67+
echo "chmod u+x ".$scriptPath."<br/>";
68+
}elseif ($return_status == self::DOS2UNIX){
69+
echo "需要使用dos2unix命令将文件转换为unix格式 <br/>";
70+
}else{
71+
// 这时候要根据脚本返回的第二个返回值判断脚本具体哪里出错误了
72+
echo "脚本执行异常 MSg :" . $return_status . "<br/>";
73+
if ( isset($output_result[1]) && $output_result[1] == 1) {
74+
echo " MSg1 : ".self::OUTPUT_MSG[$output_result[1]]."<br/>";
75+
} elseif (isset($output_result[1]) && $output_result[1] == 2){
76+
echo " MSg2 : ".self::OUTPUT_MSG[$output_result[1]]."<br/>";
77+
} elseif (isset($output_result[1]) && $output_result[1] == 3){
78+
echo " MSg3 : ".self::OUTPUT_MSG[$output_result[1]]."<br/>";
79+
}
80+
}
81+
echo 'success';
82+
}
83+
```
84+
85+

README.md

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

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

0 commit comments

Comments
 (0)