当前位置:   article > 正文

创建一个 mac 的后台进程(daemon)

mac 模拟一个进程

Mac中创建守护进程(Daemon)

创建一个可以执行的脚本 hello.sh
  1. touch /Users/oslivan/test/hello.sh
  2. chmod 755 /Users/oslivan/test/hello.sh
  3. ## hello.sh start
  4. for((;;))
  5. do
  6. echo "hello."
  7. sleep 3
  8. done
  9. ## hello.sh end
创建一个 plist, 并通过 launchctl 加载
  1. touch /Users/oslivan/Library/LaunchAgents/com.oslivan.test.hello.plist
  2. ## com.oslivan.test.hello.plist start
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  5. <plist version="1.0">
  6. <dict>
  7. <key>Label</key>
  8. <string>com.oslivan.test.hello</string>
  9. <key>ProgramArguments</key>
  10. <array>
  11. <string>/Users/oslivan/test/hello.sh</string>
  12. </array>
  13. <key>StandardOutPath</key>
  14. <string>/Users/oslivan/test/logfile.log</string>
  15. <key>StandardErrorPath</key>
  16. <string>/Users/oslivan/test/logfile_error.log</string>
  17. <key>RunAtLoad</key>
  18. <true/>
  19. <key>KeepAlive</key>
  20. <true/>
  21. </dict>
  22. </plist>
  23. ## com.oslivan.test.hello.plist end
  24. launchctl load /Users/oslivan/Library/LaunchAgents/com.oslivan.test.hello.plist
测试是否启动成功
  1. ps -ef | grep hello
  2. cd /Users/oslivan/test/ && tail logfile.log
参考

Sample Guide
launchd.plist 语法
launchd 教程

转载于:https://www.cnblogs.com/zhangyanpei/p/7136409.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/836128
推荐阅读
相关标签
  

闽ICP备14008679号