"PHP 배치 작업"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 55개는 보이지 않습니다)
1번째 줄: 1번째 줄:
<source lang='bash'>
;PHP execution in bash shell, PHP batch process, PHP cron job
php -f 파일명.php
;PHP 배치 작업 돌리기
</source>
;PHP 스크립트 작업
;리눅스 셸에서 PHP 파일 실행하기
;PHP 파일 쉘에서 실행할 때 파라미터 전달하기


==설명==
==개요==
<source lang='bash'>
* 리눅스(또는 윈도우) 쉘에서 php 파일을 실행할 수 있다.
[root@cdckb ~]# php --help
* [[crontab]]에 등록하여 주기적 수행되도록 할 수 있다.
Usage: php [options] [-f] <file> [--] [args...]
* $_SERVER 변수를 사용할 수 없다. 예를 들어 다음과 같은 코드는 제대로 실행되지 않을 것이다.
      php [options] -r <code> [--] [args...]
<syntaxhighlight lang='php'>
      php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
include $_SERVER['DOCUMENT_ROOT'].'/my_lib.php';
      php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
</syntaxhighlight>
      php [options] -- [args...]
      php [options] -a


  -a              Run as interactive shell
==방법==
  -c <path>|<file> Look for php.ini file in this directory
<syntaxhighlight lang='bash'>
  -n              No php.ini file will be used
php 파일명.php
  -d foo[=bar]    Define INI entry foo with value 'bar'
</syntaxhighlight>
  -e              Generate extended information for debugger/profiler
:→ 옵션을 명시하여 <code>php -f 파일명.php</code> 라고 써도 되지만 굳이 그럴 필요는 없다.
  -f <file>       Parse and execute <file>.
 
  -h              This help
==테스트==
  -i              PHP information
===파일 쓰기===
  -l              Syntax check only (lint)
test1.php를 편집기로 열고,
  -m              Show compiled in modules
<syntaxhighlight lang='bash'>
  -r <code>       Run PHP <code> without using script tags <?..?>
vi test1.php
  -B <begin_code> Run PHP <begin_code> before processing input lines
</syntaxhighlight>
  -R <code>       Run PHP <code> for every input line
아래 내용을 넣은 후
  -F <file>       Parse and execute <file> for every input line
<syntaxhighlight lang='php'>
  -E <end_code>   Run PHP <end_code> after processing all input lines
<?php
  -H              Hide any passed arguments from external tools.
file_put_contents("test1.txt", "John Smith\n");
  -s              Output HTML syntax highlighted source.
</syntaxhighlight>
</source>
실행시켜 보자.
 
;실행 예시
<syntaxhighlight lang='console'>
[root@zetawiki ~]# php test1.php
[root@zetawiki ~]# cat test1.txt
John Smith
</syntaxhighlight>
 
===인수 받기 1===
{{참고|PHP 배치 인수 받기}}
test2.php를 편집기로 열고,
<syntaxhighlight lang='bash'>
vi test2.php
</syntaxhighlight>
아래 내용을 넣은 후
<syntaxhighlight lang='php'>
<?php
print_r($argv);
?>
</syntaxhighlight>
실행시켜 보자.
 
;실행 예시
<syntaxhighlight lang='console'>
[root@zetawiki ~]# php test2.php hello world 2012
Array
(
    [0] => test2.php
    [1] => hello
    [2] => world
    [3] => 2012
)</syntaxhighlight>
 
===인수 받기 2===
test3.php를 편집기로 열고,
<syntaxhighlight lang='bash'>
vi test3.php
</syntaxhighlight>
아래 내용을 넣은 후
<syntaxhighlight lang='php'>
<?php
$k=0;
if(isset($argv[1])&&$argv[1]>0)$k=$argv[1];
for($i=0;$i<$k;$i++) {
print "$i ";
}
print "\n";
</syntaxhighlight>
실행시켜 보자.
 
;실행 예시
<syntaxhighlight lang='console'>
[root@zetawiki ~]# php test3.php 3
0 1 2
[root@zetawiki ~]# php test3.php 10
0 1 2 3 4 5 6 7 8 9
[root@zetawiki ~]# php test3.php
 
[root@zetawiki ~]# php test3.php jmnote
 
[root@zetawiki ~]# php test3.php -1
 
[root@zetawiki ~]#
</syntaxhighlight>
 
==예약작업==
{{참조|crontab}}
 
;cron 예약작업 예시
<syntaxhighlight lang='console'>
[root@zetawiki ~]# crontab -l
00 02 * * * php /root/script/backup.php
</syntaxhighlight>
:→ 매일 02:00에 /root/script/backup.php를 수행한다.
 
==같이 보기==
*[[PHP 스크립트 직접 실행하기]]
*[[PHP 배치 전달인자 받기]]
*[[crontab]]
*[[Can't connect to local MySQL server through socket]]
*[[PHP 배치 백그라운드 실행]]
*[[PHP 짧은 태그 사용]]
*[[PHP CLI인지 확인]]
*[[PHP]]
*[[리눅스 쉘에서 한줄 코드 실행]]


[[분류:리눅스]]
[[분류:리눅스]]
[[분류:PHP]]
[[분류:PHP]]
[[분류:Bash]]

2020년 11월 2일 (월) 02:35 기준 최신판

PHP execution in bash shell, PHP batch process, PHP cron job
PHP 배치 작업 돌리기
PHP 스크립트 작업
리눅스 셸에서 PHP 파일 실행하기
PHP 파일 쉘에서 실행할 때 파라미터 전달하기

1 개요[ | ]

  • 리눅스(또는 윈도우) 쉘에서 php 파일을 실행할 수 있다.
  • crontab에 등록하여 주기적 수행되도록 할 수 있다.
  • $_SERVER 변수를 사용할 수 없다. 예를 들어 다음과 같은 코드는 제대로 실행되지 않을 것이다.
include $_SERVER['DOCUMENT_ROOT'].'/my_lib.php';

2 방법[ | ]

php 파일명.php
→ 옵션을 명시하여 php -f 파일명.php 라고 써도 되지만 굳이 그럴 필요는 없다.

3 테스트[ | ]

3.1 파일 쓰기[ | ]

test1.php를 편집기로 열고,

vi test1.php

아래 내용을 넣은 후

<?php
file_put_contents("test1.txt", "John Smith\n");

실행시켜 보자.

실행 예시
[root@zetawiki ~]# php test1.php
[root@zetawiki ~]# cat test1.txt
John Smith

3.2 인수 받기 1[ | ]

test2.php를 편집기로 열고,

vi test2.php

아래 내용을 넣은 후

<?php
print_r($argv);
?>

실행시켜 보자.

실행 예시
[root@zetawiki ~]# php test2.php hello world 2012
Array
(
    [0] => test2.php
    [1] => hello
    [2] => world
    [3] => 2012
)

3.3 인수 받기 2[ | ]

test3.php를 편집기로 열고,

vi test3.php

아래 내용을 넣은 후

<?php
$k=0;
if(isset($argv[1])&&$argv[1]>0)$k=$argv[1];
for($i=0;$i<$k;$i++) {
	print "$i ";
}
print "\n";

실행시켜 보자.

실행 예시
[root@zetawiki ~]# php test3.php 3
0 1 2 
[root@zetawiki ~]# php test3.php 10
0 1 2 3 4 5 6 7 8 9 
[root@zetawiki ~]# php test3.php

[root@zetawiki ~]# php test3.php jmnote

[root@zetawiki ~]# php test3.php -1

[root@zetawiki ~]#

4 예약작업[ | ]

이 부분에 대해 더 많은 내용을 읽으려면 crontab 문서를 참조해 주세요.

cron 예약작업 예시
[root@zetawiki ~]# crontab -l
00 02 * * * php /root/script/backup.php
→ 매일 02:00에 /root/script/backup.php를 수행한다.

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}