2011-01-06 11:24:35 +0000 2011-01-06 11:24:35 +0000
56
56
Advertisement

起動/ログイン時にコマンドを実行する(Mac OS X

Advertisement

起動時に実行されるように、このbashコマンドをどのファイルに入れればいいのか悩んでいました。

# Start the MongoDB server
/Applications/MongoDB/bin/mongod --dbpath /usr/local/mongo/data --fork --logpath /usr/local/mongo/log

ネットで調べてみたところ、~/.bashrc, ~/profile, /etc/bashrc, /etc/profile, ~/.bash_profile, 0x6&, 0x6&, 0x6&, 0x6& の間だと思います。これらを試してみましたが、terminal startupではなくMac startupで実行されているようです。私はファイルを見落としているのでしょうか?

Advertisement
Advertisement

回答 (8)

60
60
60
2011-01-06 12:50:04 +0000

OS Xで起動時にコマンドを実行するには、launchdを使う必要があります。

リンゴン を使用したくない場合は、launchd プロパティリストを作成する必要があります。これはXMLファイルなので、お気に入りのテキストエディタで作成するか、Mac OS X Dev ToolsにインストールされているProperty List Editorを使用することができます。以下のように作成します。

<?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>some.meaningful.name</string> <!-- org.mongodb.mongodb perhaps? -->

    <key>OnDemand</key>
    <false/>

    <key>UserName</key>
    <string>anAppropriateUser</string>

    <key>GroupName</key>
    <string>anAppropriateGroup</string>

    <key>ProgramArguments</key>
    <array>
            <string>/Applications/MongoDB/bin/mongod</string>
            <string>--dbpath</string>
            <string>/usr/local/mongo/data</string>
            <string>--fork</string>
            <string>--logpath</string>
            <string>/usr/local/mongo/log</string>
    </array>
</dict>
</plist>

これを/Library/LaunchAgents/some.meaningful.name.plistに保存し(管理者アカウントまたはsudoが必要です)、ターミナルを開いて実行します。

sudo launchctl load /Library/LaunchAgents/some.meaningful.name.plist

これで launchd がアイテムをロードし、起動時に MongoDB が起動するようになります。さらに、launchd が監視して、何らかの理由で終了した場合は再起動します。アイテムを削除するには、上のコマンドの load を unload に置き換えてください。

51
51
51
2012-08-23 18:09:02 +0000

Stack Overflowからのもう一つの簡単な解決策. 以下のことができます。

  • Automator.appを起動;
  • “Application "を選択;
  • ツールバーの "Show library "をクリック(非表示の場合);
  • "Run shell script "を追加(Actions/Utilitiesから);
  • スクリプトをウィンドウにコピー&ペースト;
  • テスト;
  • どこかに保存: your_name.appというファイルが作成されます);
  • お使いのMacOSXのバージョンによります:
  • Oldversion. システム環境設定→アカウント→ログイン項目、または
  • _新しいバージョン。
  • Newバージョン: システム環境設定→ユーザーとグループ→ログイン項目(右上)へ;
  • 新しく作成したアプリを追加;

ログオフして、再度ログインすれば完了です。)

35
Advertisement
35
35
2011-01-06 11:37:23 +0000
Advertisement

公式にはどれもありません。Appleが提案している方法は、 launchd を使うことです。これを設定するには、 lingon Launch Control

あなたが言及しているファイルに関しては、ホームディレクトリ ~/.bashrc, ~/profile, ~/.bash_profile は、ターミナルからログインした時だけ起動します。etcにあるものは、シェルが起動して、ホームディレクトリにあるものよりも先に全ユーザに対して起動しますが、ログインしたときだけ起動します。 bash manual

Unixの起動スクリプトは/etc/rc*を使っていたけど、OSXの場合はlaunchdのものを使えばいいのね。

30
30
30
2015-11-03 17:53:47 +0000

ログイン時にコマンドを起動するには、以下のようにします。

  • コマンドを含むテキストファイルを作成する(bashスクリプト):

  • このファイルを~/Library/Startup.cmd

  • Finderでファイルをダブルクリックしてテストすることができます

  • 実行可能にする。chmod +x ~/Library/Startup.cmd

  • Add this file in System Preferences 〾 Accounts 〾 Login items

3
Advertisement
3
3
2016-03-24 19:58:03 +0000
Advertisement

LinuxとMac OS Xで動作するようにしたい場合は、cronタスクで十分です(crontab -eを実行してcronタスクを編集してください):

@reboot /path/to/script

(クレジット. https://unix.stackexchange.com/questions/49207/how-do-i-set-a-script-that-it-will-run-on-start-up-in-freebsd )

3
3
3
2011-01-06 11:32:48 +0000

launchdlaunchctlがMacOS上でどのように動作するかを調べる必要があります。まず、両方のコマンドのマニュアルページを読むことから始めましょう。次に、/Library/LaunchAgents//Library/LaunchDaemons/ の中を見て、launchctl インターフェイスを使って異なる時間に読み込まれるアプリケーションを設定する方法の例を見つけることができます。 ここに例があります Stack Overflow で見つけました。

0
Advertisement
0
0
2018-05-22 16:37:28 +0000
Advertisement

私はこの問題に対する非常に簡単なunixの答えに興味があり、別のサイトでそれを見つけました。以下、解決策の概要です。

ログインシェルの標準は、名前に “profile "が入っているbashの設定ファイルを常にこの順番で探すことになっています。etc/profile, ~/.bash_profile, ~//.bashlogin, 最後に ~/.profile の順に探します。ログインシェルが終了すると、~/.bashlogoutを読む。

私の場合は ~/.bash_profile を作成して、Mac Terminal アプリの環境設定を開いて、"Shell opens with” オプションをデフォルトから /bin/bash に変更した。それだけです。クリーンでシンプル。

-3
-3
-3
2017-03-11 07:25:05 +0000

ターミナルを開き、

nano ~/.bash_profile

と入力し、このテキストをファイルに追加します。

/Applications/MongoDB/bin/mongod --dbpath /usr/local/mongo/data --fork logpath /usr/local/mongo/log
``` 0x1&
Advertisement

関連する質問

12
5
13
8
8
Advertisement