创建一个 mac 的后台进程(daemon)
2024-10-18 21:19:17
Mac中创建守护进程(Daemon)
创建一个可以执行的脚本 hello.sh
touch /Users/oslivan/test/hello.sh
chmod 755 /Users/oslivan/test/hello.sh
## hello.sh start
for((;;))
do
echo "hello."
sleep 3
done
## hello.sh end
创建一个 plist, 并通过 launchctl 加载
touch /Users/oslivan/Library/LaunchAgents/com.oslivan.test.hello.plist
## com.oslivan.test.hello.plist start
<?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.oslivan.test.hello</string>
<key>ProgramArguments</key>
<array>
<string>/Users/oslivan/test/hello.sh</string>
</array>
<key>StandardOutPath</key>
<string>/Users/oslivan/test/logfile.log</string>
<key>StandardErrorPath</key>
<string>/Users/oslivan/test/logfile_error.log</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
## com.oslivan.test.hello.plist end
launchctl load /Users/oslivan/Library/LaunchAgents/com.oslivan.test.hello.plist
测试是否启动成功
ps -ef | grep hello
cd /Users/oslivan/test/ && tail logfile.log
参考
Sample Guide
launchd.plist 语法
launchd 教程
最新文章
- git的一些指令
- Entity Framework Core 实现读写分离
- 关于 Visual Studio 调试 Global 的一点总结
- mjrefresh源码分析
- php中json_decode()和json_encode()
- casperjs配合phantomjs实现自动登录百度,模拟点击等等操作 - 怕虎在线www.ipahoo.com图文教程 - 怕虎在线
- select标签操作大全
- WebX框架学习笔记之二----框架搭建及请求的发起和处理
- BZOJ 2222: [Cqoi2006]猜数游戏【神奇的做法,傻逼题,猜结论】
- springboot~基于单元测试的mongodb
- 解决Warning: mysql_connect(): Headers and client library minor version mismatch. 警告
- 4-1 requests库的安装
- 记录tiny6410 jlink 命令行调试linux-2.6.38内核
- jsp 假分页的实现
- .NET MVC中的防CSRF攻击
- OpenCV——图像的深度与通道数讲解
- Linq let Concat
- java 面试题总结(一)
- js搜索算法——二分搜索
- 按钮CSS样式