강좌게시판

제목 코드이그나이터 크론 사용하기
글쓴이 호랭총각 작성시각 2013/02/15 17:54:22
댓글 : 3 추천 : 0 스크랩 : 1 조회수 : 36433   RSS
안녕하세요 글은 처음 남겨보네요~!
우선 코드이그나이터로 크론 사용을 어떻케 하는지 궁금해서 국내/해외 자료 서칭하던중.  해외사이트에서
제 생각에 제일 정리가 잘되었다 생각된 내용을 퍼왔습니다. 참고하시고, 혹시나 번역이 필요하다고 요청하시면
번역본정리해서 올리겠습니다.

참고로 리눅스 기반에서 크론작업은 매우매우 유용하게 쓰여서, 확실히 짚고 넘어가야 하는 부분이 아닌가 싶네요~^^
아래가 원본 내용입니다~!


외쿡형의 아주 잘 작성된 포스트를 퍼왔습니다. 역시 포스팅은 외쿡인 형들이 잘하나봐여 

 

How to Run Cron Jobs in CodeIgniter

How to Run Cron Jobs in CodeIgniter

Posted on January 4, 2012 by Steve

After a fair bit of trawling Google, I got the impression that setting up a cron job in codeigniter was going to be a bit tricky.


The two potential solutions I discovered were:


execute the “wget” command to replicate a normal browser request, which then exposes my cron functionality to the big wide world and creates some additional unnecessary overheads, or

Create a bespoke front controller a bit like the existing index.php but specifically for cron jobs

Neither of these options sound particularly ideal to me, and thankfully, there is a better way (and it turned out to be a case of RTFM).


Firstly, just create a normal controller. I creatively called mine Cron.



class Cron extends CI_Controller {

 

    function __construct()

    {

        parent::__construct();

 

        // this controller can only be called from the command line

        if (!$this->input->is_cli_request()) show_error('Direct access is not allowed');

    }

 

    function foo($bar = 'bar')

    {

        echo "foo = $bar";

    }

}

Note the call to $this->input->is_cli_request() which ensures that this controller cannot be accessed directly from a url.


Next we setup our cron job as you would any other, except that you provide the path in a special format.


php /path/to/index.php controller_name method_name ["params"]


The first part is either the command “php” or your server path to php.


The second part is the absolute path to your CI front controller, eg. /path/to/index.php.


The third part is the controller name, followed by the method name, followed by optional parameters.


The CI manual gives the example:


php /path/to/index.php cron foo


If that doesn’t work, it probably means you don’t have the package php5-cli installed. On debian / ubuntu you can install this package as follows:


sudo apt-get install php5-cli


If you are not able to install packages, you can specify your path to php. I set mine as follows:


/usr/local/bin/php -f /home/clinic/public_html/index.php cron foo


You can even pass parameters in quotes, which have the same effect as parameters in regular url requests!


/usr/local/bin/php -f /home/clinic/public_html/index.php cron foo “beer”

 다음글 CodeIgniter 동영상 강의 (12)
 이전글 워드프레스와 codeigniter 접목(?)하기 (11)

댓글

kokanee / 2013/02/19 10:38:22 / 추천 0
추가적으로 크론잡을 걸땐 크론잡 유저의 권한과 작업대상(파일 등)의 접근 권한을 확인하는게 중요합니다.
(apache user 혹은 nginx user와 크론잡 유저가 다를 경우)

또한 Oracle DB를 사용 할 경우 Oracle Path, 시스템 환경 변수등이 크론잡 유저에 설정되어 있는지 확인해야 겠지요?

다 귀찮다면.. 문서에 있듯이 wget으로 돌려도 무방.. 단, REMOTE_ADDR등으로 접근을 막아주는 건 기본..
noel / 2014/03/07 16:31:55 / 추천 0
 혹시 클론돌리시거나 cli 로 실행시  아래같은 메세지가 나온다면, 

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index: REMOTE_ADDR</p>
<p>Filename: core/Input.php</p>
<p>Line Number: 351</p>

</div><div>

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - 
headers already sent by (output started at 
/usr/local/www/webdev/corp_intranet/system/core/Exceptions.php:185)</p>
<p>Filename: libraries/Session.php</p>
<p>Line Number: 675</p>

</div> 

다음과 같이 고쳐 주세요.

// system/core/Input.php 라인 351 

//변경 전
$this->ip_address = $_SERVER['REMOTE_ADDR'];

//변경 후
$this->ip_address = $this->server('remote_addr');


다엘 / 2014/07/15 19:03:59 / 추천 0
그리고 크론 작업시 세션이 필요없는 작업이라면 세션 라이브러리 호출을 안하시는게 좋습니다.

CI 크론 돌려놓은거 잊어버리고 세션이 계속 생겨서 삽질했던 기억때문에 댓글남깁니다.

위의 noel님의 팁을 적용하지 않으셨다면 세션의 ip_address값은 0.0.0.0으로 저장됩니다.