원문 : 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 30726
104 Rocky 9+nginx1.20+php8+MariaDB 10.5 환경에서 wordpress 설치하기 ischo 2024.01.10 319
103 파일시스템 양방향 동기화하기 - rsync, lsyncd file ischo 2024.01.03 384
102 LVM 타입 swap 영역 용량 축소하기 ischo 2023.11.28 386
101 named 에서 DNS root 서버 리스트 갱신하기 ischo 2023.06.13 517
100 vi 에디터에서 복사,붙여넣기 하면 # 마크가 자동으로 앞에 붙을때 ischo 2023.04.19 470
99 구버전 Linux에서 Rocky Linux 9.1 버전으로 ssh 접속시 "no hostkey alg" 표시후 접속불가 ischo 2023.04.19 1169
98 파일시스템 사용량 추이를 http 전송으로 DB에 누적하기 file ischo 2022.08.29 752
97 [Shell] 시작한지 1시간 이상 경과한 프로세스 kill 하기 ischo 2022.06.16 244
96 apache log4j 취약점 제거조치 CVE-2021-44228 CVE-2021-45046 CVE-2021-41045 CVE-2021-4104 ischo 2021.12.23 1050
95 Multi Putty Manager file ischo 2020.10.08 897
94 CentOS 7에 Apache2, php7.3, MariaDB 설치하기 ischo 2019.09.02 1926
93 리눅스에서 Windows Filesystem(NTFS) 마운트하기 ischo 2018.12.20 3470
92 SFTP로 자동 업로드/다운로드 스크립트 ischo 2018.04.04 7355
91 CIDR Conversion Table ischo 2018.03.08 2965
90 (스크립트) 오라클 DB서버에서 매일 원격지 FTP로 export 파일 업로드 하기 ischo 2017.11.04 4333
» shell script를 cron으로 동작시킬때 .bashrc .bash_profile의 변수가 적용되지 않는다 ischo 2017.09.08 14745
88 재부팅 없이 SCSI device를 rescan하기 ischo 2017.03.16 16527
87 apache 404 에러에 OS 또는 apache버전을 표시하는 취약점 없애기 ischo 2016.10.25 7682
86 RHEL/CentOS 7 환경에서 자동업데이트 disable 시키기 ischo 2016.07.02 10193
85 NTP 서버 설정 ischo 2016.03.03 10774
서버에 요청 중입니다. 잠시만 기다려 주십시오...