레이블이 launchctl인 게시물을 표시합니다. 모든 게시물 표시
레이블이 launchctl인 게시물을 표시합니다. 모든 게시물 표시

2009년 12월 4일 금요일

맥에서 tftp 사용하기 (Using tftp on the Mac)

일반적으로 많이 쓰이지는 않아도 라우터등의 장비 펌웨어 업그레이드라던지 임베디드 보드 개발할 때 유용한 도구중에 하나가 tftp(trivial ftp)이다.
Mac OS X에도 역시 기본으로 포함되어 있지만 다른 daemon들과는 다르게 자동으로 실행하게 되어있지 않다. 그리고 실행하려면 직접 하면 안되고 꼭 launchd와 /System/Library/LaunchDaemon/tftp.plist를 사용해서 실행해야 한다.



실행하는 방법은 다음과 같다.

$ sudo launchctl
Password:
launchd% load -F /System/Library/LaunchDaemons/tftp.plist
launchd%

이제 아무 문제가 없으면 launchd가 tftp 포트를 listen하고 있다가 connection이 들어오면 tftp를 실행해주게 된다.

그리고 linux/unix의 경우 일반적으로 tftp 디렉토리는 /tftpboot 를 사용하지만 맥에서는 tftp.plist 파일에서 지정하게 되어 있고 디폴트로는 /private/tftpboot 디렉토리이다.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<true/>
<key>Label</key>
<string>com.apple.tftpd</string>
<key>ProgramArguments</key>
<array>
<string>/usr/libexec/tftpd</string>
<string>-i</string>
<string>-s</string>
<string>/private/tftpboot</string>
</array>
<key>Sockets</key>
<dict>
<key>Listeners</key>
<dict>
<key>SockServiceName</key>
<string>tftp</string>
<key>SockType</key>
<string>dgram</string>
</dict>
</dict>
<key>inetdCompatibility</key>
<dict>
<key>Wait</key>
<true/>
</dict>
</dict>
</plist>

 
텍스트 에디터로 tftp.plist파일을 열어보면 내용은 위와 같다. 그 중 bold체로 된 부분을 변경해 주면 tftp 디렉토리를 원하는대로 바꿔줄 수 있다.

사용이 끝나 실행을 중단시키고 싶으면 실행할 때와 마찬가지로 launchctl에서 명령을 내려주면 된다.

launchd% unload /System/Library/LaunchDaemons/tftp.plist

만일 tftp를 부팅할 때 부터 계속 실행하도록 해 주고 싶으면 launchctl에서 다음의 명령을 사용한다.

launchd% load -w  /System/Library/LaunchDaemons/tftp.plist

또한 반대로 부팅할 때 부터 계속 실행되는걸 멈추고 싶으면 아래 명령을 사용하면 된다.

launchd% unload -w /System/Library/LaunchDaemons/tftp.plist



2008년 5월 20일 화요일

Mac에서 정해진 시간에 자동으로 프로그램 실행하는 법

애플은 Mac OS X v10.4 Tiger에서부터 launchd라는 프로그램을 사용하기 시작했다. launchd는 애플이 개발하여 공개한 open source service management framework으로 전통적인 유닉스에서 사용되는 /etc/rd, cron, xinetd, init.d의 기능을 통합해서 제공해준다.
좀 더 자세한 소개는 애플 사이트의 Getting started with launchd 를 참조하면 된다.

여기서는 launchd의 다양한 기능들 중에 cron을 대치하는 기능을 설정하는 법을 설명하겠다.

launchd를 제어하기 위해서는 launchctl을 이용하면 된다.
xml로 된 configuration파일을 작성해서 .plist 확장자를 붙인 다음 해당 디렉토리에 저장해준다.
관리자가 아닌 경우 자신의 홈 디렉토리(~) 아래의 Library/LaunchAgents 에 저장해주면 된다.

* 각각의 경우에 따른 .plist 파일 저장 위치
     ~/Library/LaunchAgents         Per-user agents provided by the user.
/Library/LaunchAgents Per-user agents provided by the administrator.
/Library/LaunchDaemons System wide daemons provided by the administrator.
/System/Library/LaunchAgents Mac OS X Per-user agents.
/System/Library/LaunchDaemons Mac OS X System wide daemons.

.plist 파일의 상세한 내용은 애플 사이트의 man pages 를 참조하면 된다.

여기서는 일단 간단한 예로 설명하기로 하겠다.

$ /usr/bin/python /Users/abcd/bin/checkmail.py

위의 명령을 1분(60초)마다 한번씩 실행해서 메일이 왔는지 확인하고 싶은 경우

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>check_mail</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python</string>
<string>/Users/abcd/bin/checkmail.py</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>

---
실행할 명령에 argument가 없으면

<key>ProgramArguments</key>
<array>
<string>프로그램 이름</string>
</array>

같이 해 주면 되지만 argument가 있는 경우 각각의 argument를 다음과 같이 별개의 element로 구분해서 넣어줘야 한다.

<key>ProgramArguments</key>
<array>
<string>프로그램 이름</string>
<string>argument 1</string>
<string>argument 2</string>
....
</array>

<key>StartInterval</key>
<integer>시간간격(초)</integer>

빨간색 부분은 Tiger(v10.4)에서만 사용할 수 있고 Leopard(v10.5)에서는 저 부분을 아래와 같이 바꿔줘야 한다.

<key>KeepAlive</key>
<false/>
---

위의 내용을 ~/Library/LaunchAgents 에 check_mail.plist 라는 이름으로 저장한 경우 터미널을 열고

$ launchctl load ~/Library/LaunchAgents/check_mail.plist

를 쳐 주거나 맥을 재부팅 시키면 바뀐 내용이 적용된다.

지금까지 설명한 방식은 전통적인 유닉스 환경에서 vi를 사용하는 방식이고 사용자 편의성을 최고로 생각하는 맥OS답게 이미 사용자가 쉽게 launchd configuration을 만들거나 수정할 수 있는 어플리케이션도 나와 있다.

사용자 삽입 이미지

Lingon은 launchd의 configuration파일을 에디팅하거나 만들어주는 소프트웨어로 xml이나 plist의 문법을 몰라도 손 쉽게 launchd를 설정할 수 있게 해 준다. 물론 오픈소스 소프트웨어로 여기에서 다운받을 수 있다.
위에 보이는 화면은 베이직 모드로 일반적인 설정을 손 쉽게 할 수 있게 해 주는 모드고 전문가 모드에서는 텍스트 입력 화면에서 각 xml element를 직접 쳐 넣을 수 있다.