LoginSignup
8
10

More than 5 years have passed since last update.

service コマンド風の launchctl ラッパースクリプト「osx-service-cmd」をつくってみた

Posted at

OS X の launchctl を使いやすくする service コマンド風のラッパースクリプトをつくってみました。

Homebrew 等でインストールしたサービスの起動・停止・再起動などに使えます。

動作環境

  • launchctl コマンドが搭載されている Mac OS X
    • Mac OS X 10.4 (Tiger) 以上
  • OS X 10.10.4 (Yosemite) で動作確認済み

インストール方法

git clone https://github.com/nobutakaoshiro/osx-service-cmd.git
cd osx-service-cmd

./install.sh

/usr/local/bin に service コマンドがコピーされます。
パスが通ってない方は、以下のページを参考にしてパスの設定を行ってください。

使い方

サービス一覧表示

service list 

LaunchAgents/LaunchDaemons の plist 一覧を表示します

サービス起動

もし ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist という plist が存在している場合

service mongodb start
# => launchctl load /Users/nobutaka/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

# 以下でも可(部分一致)
service homebrew.mxcl.mongodb start
service homebrew.mxcl.mongodb.plist start

サービス停止

service mongodb stop
# => launchctl unload /Users/nobutaka/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

サービス再起動

service mongodb restart
# => launchctl unload /Users/nobutaka/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
# => launchctl load /Users/nobutaka/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

サービス自動起動ON

service mongodb enable
# => launchctl load -w /Users/nobutaka/Library/LaunchAgents/homebrew.mxcl.mongodb.plis

サービス自動起動OFF

service mongodb disable
# => launchctl unload -w /Users/nobutaka/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

解説

内部的には ~/Library/LaunchAgents, /Library/LaunchAgents, /Library/LaunchDaemons にある plist の中から、第1引数に指定した文字列に部分一致する plist を探して、その plist に対して launchctl load/unload を行っています。
rootの権限が必要なところは sudo をつけて service コマンドを実行してください。

アンインストール方法

rm /usr/local/bin/service

アンインストールはスクリプトを削除するだけです。お手軽簡単です。

注意事項

OS X 環境専用です。OS X 以外の環境では動きません。

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10