bash 登录mac时运行脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6442364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:41:24  来源:igfitidea点击:

Running script upon login mac

bashshellloginterminalosx-snow-leopard

提问by MZimmerman6

I am wondering if anyone is able to help me out with getting a .sh file to run when I log in to my account on my computer. I am running Mac OS X 10.6.7.

我想知道当我在我的计算机上登录我的帐户时,是否有人能够帮助我运行 .sh 文件。我正在运行 Mac OS X 10.6.7。

I have a file "Example.sh" that I want to run when I log onto my computer. I do not have a problem running it when I am already logged in, but I want this to run automatically.

我有一个文件“Example.sh”,我想在登录到我的计算机时运行它。我已经登录后运行它没有问题,但我希望它自动运行。

People are saying to add it to the login shell file, but I do not know where that is. Some help please.

人们说要将它添加到登录 shell 文件中,但我不知道它在哪里。请一些帮助。

回答by trisweb

tl;dr: use OSX's native process launcher and manager, launchd.

tl;dr:使用 OSX 的本机进程启动器和管理器,launchd.

To do so, make a launchctldaemon. You'll have full control over all aspects of the script. You can run once or keep alive as a daemon. In most cases, this is the way to go.

为此,请创建一个launchctl守护进程。您将完全控制脚本的所有方面。您可以运行一次或作为守护进程保持活动状态。在大多数情况下,这是要走的路。

  1. Create a .plistfile according to the instructions in the Apple Dev docs hereor more detail below.
  2. Place in ~/Library/LaunchAgents
  3. Log in (or run manually via launchctl load [filename.plist])
  1. .plist根据此处 Apple Dev 文档中说明或下面的更多详细信息创建文件。
  2. 放入 ~/Library/LaunchAgents
  3. 登录(或通过手动运行launchctl load [filename.plist]

For more on launchd, the wikipedia articleis quite good and describes the system and its advantages over other older systems.

有关更多信息launchd维基百科文章非常好,描述了该系统及其相对于其他旧系统的优势。



Here's the specific plist file to run a script at login.

这是在 login 时运行脚本的特定 plist 文件。

Updated 2017/09/25 for OSX El Capitan and newer (credit to José Messias Jr):

2017 年 9 月 25 日更新了 OSX El Capitan 和更新版本(归功于José Messias Jr):

<?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>com.user.loginscript</string>
   <key>ProgramArguments</key>
   <array><string>/path/to/executable/script.sh</string></array>
   <key>RunAtLoad</key>
   <true/>
</dict>
</plist>

Replace the <string>after the Program key with your desired command (note that any script referenced by that command must be executable: chmod a+x /path/to/executable/script.shto ensure it is for all users).

<string>用您想要的命令替换Program 键之后的(请注意,该命令引用的任何脚本都必须是可执行的:chmod a+x /path/to/executable/script.sh以确保它适用于所有用户)。

Save as ~/Library/LaunchAgents/com.user.loginscript.plist

另存为 ~/Library/LaunchAgents/com.user.loginscript.plist

Run launchctl load ~/Library/LaunchAgents/com.user.loginscript.plistand log out/in to test (or to test directly, run launchctl start com.user.loginscript)

运行launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist并注销/登录以进行测试(或直接测试,运行launchctl start com.user.loginscript

Tail /var/log/system.logfor error messages.

/var/log/system.log错误消息的尾部。

The key is that this is a User-specific launchd entry, so it will be run on login for the given user. System-specific launch daemons (placed in /Library/LaunchDaemons) are run on boot.

关键是这是一个特定于用户的启动条目,因此它将在给定用户登录时运行。特定于系统的启动守护程序(放置在 中/Library/LaunchDaemons)在启动时运行。

If you want a script to run on login for all users, I believe LoginHook is your only option, and that's probably the reason it exists.

如果您希望脚本在所有用户登录时运行,我相信 LoginHook 是您唯一的选择,这可能就是它存在的原因。

回答by jm666

Follow this:

按照这个:

  • start Automator.app
  • select Application
  • click Show libraryin the toolbar (if hidden)
  • add Run shell script(from the Actions/Utilities)
  • copy & paste your script into the window
  • test it
  • save somewhere (for example you can make an Applicationsfolder in your HOME, you will get an your_name.app)

  • go to System Preferences-> Accounts-> Login items

  • add this app
  • test & done ;)
  • 开始 Automator.app
  • 选择 Application
  • 单击Show library工具栏中的(如果隐藏)
  • 添加Run shell script(来自Actions/Utilities
  • 将您的脚本复制并粘贴到窗口中
  • 测试一下
  • 保存在某个地方(例如,您可以Applications在 HOME 中创建一个文件夹,您将获得一个your_name.app

  • System Preferences-> Accounts->Login items

  • 添加此应用
  • 测试 & 完成;)

EDIT:

编辑:

I've recently earned a "Good answer" badge for this answer. While my solution is simple and working, the cleanest way to run any program or shell script at login time is described in @trisweb's answer, unless, you want interactivity.

我最近为这个答案赢得了“好答案”徽章。虽然我的解决方案简单且有效,但@trisweb 的回答中描述了在登录时运行任何程序或 shell 脚本的最简洁方法除非您需要交互性

With automator solution you can do things like next: automator screenshot login application

使用自动化解决方案,您可以执行以下操作: automator 截图登录应用

so, asking to run a script or quit the app, asking passwords, running other automator workflows at login time, conditionally run applications at login time and so on...

因此,要求运行脚本或退出应用程序、询问密码、在登录时运行其他自动化工作流、在登录时有条件地运行应用程序等等......

回答by anubhava

  1. Create your shell script as login.shin your $HOME folder.

  2. Paste the following one-line script into Script Editor:

    do shell script "$HOME/login.sh"

  3. Then save it as an application.

  4. Finally add the application to your login items.

  1. login.sh在 $HOME 文件夹中创建 shell 脚本。

  2. 将以下单行脚本粘贴到脚本编辑器中:

    做shell脚本“$HOME/login.sh”

  3. 然后将其另存为应用程序。

  4. 最后将应用程序添加到您的登录项。

If you want to make the script output visual, you can swap step 2 for this:

如果要使脚本输出可视化,可以为此交换步骤 2:

tell application "Terminal"
  activate
  do script "$HOME/login.sh"
end tell

If multiple commands are needed something like this can be used:

如果需要多个命令,可以使用这样的东西:

tell application "Terminal"
  activate
  do script "cd $HOME"
  do script "./login.sh" in window 1
end tell