Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos Mac OSX启动时启动守护程序,使用SSH编写Shell脚本_Macos_Shell_Launchd_Launch Daemon - Fatal编程技术网

Macos Mac OSX启动时启动守护程序,使用SSH编写Shell脚本

Macos Mac OSX启动时启动守护程序,使用SSH编写Shell脚本,macos,shell,launchd,launch-daemon,Macos,Shell,Launchd,Launch Daemon,我希望有人能帮我。我一整天都在做这件事 我想要一个LaunchDaemon在启动时执行shell脚本。这是我的plist文件,位于/Library/LaunchDaemons/com.mhi.backup.plist: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList

我希望有人能帮我。我一整天都在做这件事

我想要一个LaunchDaemon在启动时执行shell脚本。这是我的plist文件,位于/Library/LaunchDaemons/com.mhi.backup.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.mhi.backup</string>
  <key>UserName</key>
  <string>Joel</string>
  <key>GroupName</key>
  <string>Admin</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/mhi_websites_backup.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

标签
com.mhi.backup
用户名
乔尔
组名
管理
程序参数
/usr/local/bin/mhi_网站_backup.sh
运行负荷
当我从终端(launchctl load/Library/LaunchDaemons/com.mhi.backup.plist)加载它时,它会正确执行,但不会在启动时执行

以下是我的脚本,供参考:

#!/bin/bash
sleep 15 #delay script to ensure time for network connection
ssh user@hostname << HERE
  mysqldump -u <user_name> -pPASSWORD --all-databases | lzma > alldatabases.sql.lzma 
  tar cfa backup-`date '+M%mD%dY%y'`.tar.lzma webapps alldatabases.sql.lzma 
  exit
HERE
scp user@hostname:backup-`date '+M%mD%dY%y'`.tar.lzma /Users/Joel/Desktop
#/bin/bash
sleep 15#延迟脚本以确保网络连接时间
sshuser@hostnamealldatabases.sql.lzma
tar cfa备份-`date'+M%mD%dY%y'`.tar.lzma webapps alldatabases.sql.lzma
出口
在这里
scpuser@hostname:backup-`date'+M%mD%dY%y'`.tar.lzma/Users/Joel/Desktop
有人能帮忙吗

非常感谢


JG

您看到了哪些错误?我希望您可能在这里遇到
路径问题。哪里是mysqldump
?如果它位于
/usr/local/bin
中,那么您可能希望将其显式化,或者在中设置默认路径。

plist是否归root所有?如果
/Library/Launch{Agents,Daemons}/
中的plist不归root所有,则可以使用
launchctl
加载它,而不使用
sudo
,但在登录时不会加载它

您还可以尝试将plist移动到
/Library/LaunchAgents/
并添加
LimitLoadToSessionType
键:

<key>LimitLoadToSessionType</key>
<array>
  <string>LoginWindow</string>
  <string>Aqua</string>
</array>
LimitLoadToSessionType

技术说明。

Excecute
{set;export;}| sort>cmdLineEnv
{set;export;}| sort>startupEnv
每个环境,然后
diff*Env
?祝你好运。看起来和我的类似,但我把它放在了LaunchAgents中,而不是LaunchDaemon中。。不确定区别是什么。@Sheller唯一的区别是管道状态。在startupEnv中,PIPESTATUS=([0]=“0”[1]=“0”);在cmdLineEnv中,PIPESTATUS=([0]=“0”)。我甚至不知道这意味着什么…@JustinMeiners当我把我的plist放入LaunchAgents时,脚本运行了。但是,它只在登录时运行,而不是在启动时运行。我希望脚本在启动时执行,无论用户是否登录。如果不看到实际错误,很难进行调试。指定
StandardErrorPath
StandardOutPath
。我相信解决方案就在那里。mysql转储正在远程服务器上执行。如果环境是root,我怀疑问题可能与我的用户的ssh密钥没有被使用有关,但是我不确定测试这个假设的确切语法。我明白你的意思。启动守护进程运行在一种有点奇怪的状态,没有绑定到GUI,但它们是
root
,应该能够读取
~root/.ssh
控制台中有任何错误吗?