1. vi test.sh 에러발생할수있는 쉘스크립트작성
1 | #!/bin/bash |
2 | ssssssssss |
2. 실행시 에러가 발생함
-------------------------------------------
[root@test]$ /bin/bash test.sh
test.sh: line 3: ssssssssss: command not found
-------------------------------------------
3. PHP에서 exec 실행시 에러 감시 불가
1 | $outmsg = exec ( '/bin/bash test.sh' ); |
2 | echo $outmsg ; |
3 | exit ; |
--------------------------------------------
2>&1의 의미는 표준 출력의 전달되는 곳으로 표준에러를 전달하라는 의미
2>&1의 의미는 표준 출력의 전달되는 곳으로 표준에러를 전달하라는 의미
0, 1, 2는 각각 표준입력, 표준출력, 표준에러를 의미
--------------------------------------------
1 | $outmsg = exec ( "/bin/bash test.sh 2>&1" , $out , $err ); |
2 | var_dump( $out ); |
3 | var_dump( $err ); |
4 | exit ; |
--결과물------------------------------------
array(2) {
[0]=> string(3) "111"
[1]=> string(46) "test.sh: line 3: ssssssssss: command not found"
}
int(127)
--------------------------------------------
5. proc_open 사용
01 | $sDesc = array ( |
02 | 0 => array ( "pipe" , "r" ), // stdin |
03 | 1 => array ( "pipe" , "w" ), // stdout |
04 | 2 => array ( "pipe" , "w" ) //stderr |
05 | ); |
06 | $oProcess = proc_open( '/bin/bash test.sh' , $sDesc , $aPipes , dirname( __FILE__ ), null); |
07 | if ( is_resource ( $oProcess ) ) { |
08 | $sStdout = stream_get_contents( $aPipes [1]); |
09 | $sStderr = stream_get_contents( $aPipes [2]); |
10 | fclose( $aPipes [1]); |
11 | fclose( $aPipes [2]); |
12 | echo 'stdout : ' . $sStdout ; |
13 | echo 'stderr : ' . $sStderr ; |
14 | proc_close( $oProcess ); |
15 | } else { |
16 | exit ( 'ERROR' ); |
17 | } |
--결과물------------------------------------
stdout :
stderr : /bin/bash: test.sh: No such file or directory
--------------------------------------------
참고사이트
http://stackoverflow.com/questions/2320608/php-stderr-after-exec