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之间。虽然我试过这些,它们似乎是在终端启动 不是Mac启动上运行的。我是否遗漏了一个文件?

Advertisement
Advertisement

答案 (8)

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

要在OS X上启动时运行一个命令,你需要使用launchd

如果你不想使用 Lingon ,你需要创建一个 launchd 属性列表。这是一个 XML 文件,所以你可以用你最喜欢的文本编辑器来做,或者你可以使用 Mac OS X 开发工具中安装的属性列表编辑器。创建以下内容。

<?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;
  • 选择 “应用程序";
  • 点击工具栏中的 "显示库"(如果隐藏的话);
  • 添加 "运行 shell 脚本"(从 Actions/Utilities);
  • 将你的脚本复制并粘贴到窗口中;
  • 测试它;
  • 将它保存在某个地方:将创建一个名为 your_name.app 的文件);
  • 根据你的 MacOSX 版本:
  • _旧版本。转到系统偏好设置 → 账户 → 登录项目,或
  • _新版本。转到系统偏好设置 → 用户和组 → 登录项目 (右上角);
  • 添加这个新创建的应用程序;

注销,重新登录,就可以了。)

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

官方没有这些。苹果建议的方法是使用 launchd 。设置方法包括 lingon Launch Control

至于你提到的主目录下的文件 ~/.bashrc, ~/profile, ~/.bash/_profile 是在你通过终端登录时才启动的。在/etc中的文件是由shell为所有用户启动的,而不是在主目录下的文件,但只有在用户登录时才会启动。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

  • 在系统偏好设置中添加此文件 >账户 >登录项目。

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

如果你想要一个能在Linux和Mac OS X上工作的方法,一个cron任务应该就足够了(编辑你的cron任务,执行crontab -e):

@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上是如何工作的。你可以先阅读这两个命令的man页。然后你可以在/Library/LaunchAgents//Library/LaunchDaemons/里面寻找如何通过launchctl接口设置应用程序在不同时间加载的例子。 这是我在Stack Overflow上找到的一个例子 ,可能会进一步帮助你。

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

我对这个问题的一个非常简单的unix答案很感兴趣,并在另一个网站找到了它。这里是解决方案的总结。

登录shell的标准是总是寻找名称中带有 “profile "的bash配置文件,顺序如下。/etc/profile, ~/.bash/profile, 然后是 ~/.bash/login, 最后是 ~/.profile. 当登录壳退出时,它们读取~/.bash/_logout。

在我的例子中,我只是创建了~/.bash/_profile,然后我打开Mac终端应用程序的偏好设置,并将 "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
Advertisement

相关问题

12
5
13
8
1
Advertisement
Advertisement