원문 : http://www.ischo.net -- 조인상 // 시스템 엔지니어

Writer : http://www.ischo.net -- ischo // System Engineer in Replubic Of Korea

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

원문 : http://www.ischo.net -- 조인상 //시스템 엔지니어

+++++++++++++++++++++++++++++++++++++++++++++++++++++++



shell script를 cron으로 동작시킬때 .bashrc .bash_profile의 변수가 적용되지 않는다



예1) 오라클 sqlplus 를 동작시키는 shell script를 작성하여 테스트 해봤을때 정상동작하는 것을 확인했는데,

        crontab에 등록하여 실행하면 sqlplus가 실행되지 않는다. 


예2)  foobar 유저의 .bash_profile 에  FOOBAR="foobar" 라는 변수를 설정하고

         shell script 작성하여 직접 실행시 FOOBAR 변수의 내용이 잘 보이는데, crontab에 등록하면 FOOBAR 변수값이 미지정으로 나온다.




원인

: .bashrc  .bash_profile 등의 초기화 파일은 interactive mode에서만 실행되기 때문이다.



man bash 내용에서 발췌

       /bin/bash
              The bash executable
       /etc/profile
              The systemwide initialization file, executed for login shells
       ~/.bash_profile
              The personal initialization file, executed for login shells
       ~/.bashrc
              The individual per-interactive-shell startup file
       ~/.bash_logout
              The individual login shell cleanup file, executed when a login shell exits
       ~/.inputrc
              Individual readline initialization file

    


위와 같이 /etc/profile,  ~/.bash_profile, ~/.bashrc  초기화 파일은 login shell 혹은 interactive-shell 에서만 수행되기 때문에

crontab 으로 수행시 초기화파일에 정의한 환경변수는 적용되지 않는다.


shell의 수행환경

     - interactive shell : shell login을 수행하는 환경 (terminal 혹은 ssh remote login)

     - non-interactive shell : shell login을 수행하지 않는 환경 (crontab 실행)




해결방법


1. shell script 안에 초기화파일 수행하는 라인을 추가한다.

#!/bin/bash

 . ~/.bash_profile



2. script의 첫 라인에 bash -login 옵션을 사용한다.  --login 옵션을 사용하면 초기화파일이 모두 수행된다.

#!/bin/bash --login



man bash 내용에서 발췌

       -l        Make bash act as if it had been invoked as a login shell (see INVOCATION below).

       --login
              Equivalent to -l.



3. root 유저를 이용한다.

root 유저의 cron에 등록하여 su 스위치를 -c 옵션과 같이 사용하면 초기화파일이 수행된다.

예)   0 0 * * * su - foobar -c /home/foobar/foobar.sh




번호 제목 글쓴이 날짜 조회 수
공지 [공지] 게시자료 열람자유. 불펌금지입니다. 조인상 2010.12.07 30768
64 kill 명령어로 줄수 있는 interrupt list ischo 2012.08.17 12530
63 apache log rotate 시키기 ischo 2012.08.14 17151
62 DNS 기초에서 보안까지 조인상 2012.07.26 11423
61 아파치. 톰캣 연동 (apache+tomcat by mod_jk) ischo 2012.04.18 17336
60 SELINUX 비활성화하기 ischo 2012.04.06 15867
59 CentOS 6 에서 XDMCP 활성화 방법 ischo 2012.02.25 15235
58 리눅스 커널이 64bit 인지 확인방법 ischo 2012.02.24 34988
57 복수서버들의 서비스포트 응답여부를 확인하여 메일로 보내주는 스크립트 file ischo 2012.01.17 14656
56 리눅스에서 디스크 덤프하기 ischo 2011.12.22 21901
55 telnet 으로 메일 보내기 ischo 2011.12.07 14394
54 웹서버 포트 무응답시 메일 보내주는 스크립트 file ischo 2011.11.30 11018
53 Shell 스크립트의 내장변수 + exit code ischo 2011.11.16 16834
52 프로세스 생사여부 검사후 메일보내는 쉘스크립트 file ischo 2011.11.10 13552
51 TCP/IP Flow file ischo 2011.10.19 11584
50 ssh 접속시 로그인 속도가 느릴경우 속도 향상법 ischo 2011.08.30 23008
49 백그라운드로 프로세스 실행시키기 ischo 2011.08.25 14814
48 TCP TIME_WAIT이 많이 발생하여 문제가 되는 경우 ischo 2011.08.18 28349
47 리눅스 mdadm 을 이용한 S/W RAID 구성 [1] ischo 2011.07.20 25449
46 리눅스 Services 설명 ischo 2011.07.08 23645
45 아파치 SSL 적용된 경우 startup 시에 SSL 패스워드 자동입력시키기 ischo 2011.06.02 24256
서버에 요청 중입니다. 잠시만 기다려 주십시오...