在 Mac OSX 计算机和 Linux 计算机之间自动同步目录/文件修改的脚本

发布于 2024-11-03 11:12:56 字数 360 浏览 2 评论 0原文

我的 Mac 上有一些源代码,为了进行测试,我有兴趣将其与包含与生产环境类似的 Web 服务器设置的 VM 同步。因此,我需要能够在每次发生更改时自动将文件复制到虚拟机。

我知道每当脚本运行时我都可以使用 rsync 手动执行此操作,但我需要某种方法让它在每次修改特定目录或其子目录之一中的文件时在后台运行。

我知道 Linux 机器上存在 inotifywait 可以解决这个问题。我还阅读了有关 FSEvents API 和 kqueue 的内容。然而,这些似乎都无法从命令行访问,我真的不想花很长时间做一些事情来做到这一点......

我想我可以使用 cronjob 但一分钟是一个相当长的等待时间查看网站上的更改...

有什么想法吗?

I have some source code on my Mac, and in order to test I'm interested in synchronizing it with a VM containing a similar web server setup to the production environment. Therefore I need to be able to automatically copy files over to the VM every time there are changes.

I know I can use rsync to do this manually whenever a script is run but I need some way of getting it to run in the background every single time a file in a particular directory or one of its sub-directories is modified.

I know inotifywait exists on Linux machines and could solve this problem. I've also read about the FSEvents API and kqueue. However, none of these seem to be accessible from the command line and I really don't want to spend a long time making something to do this...

I guess I could use a cronjob but a minute is a pretty long time to wait to see changes on a website...

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪眸﹌ 2024-11-10 11:12:56

我一直这样做,在 Windows/Linux/Mac 工作站上进行开发,并将更改保存到远程 Linux 服务器,这些更改立即返回到我工作站的浏览器进行测试。您有几个选项:

  1. 您可以在本地安装远程文件(例如通过 sshfs)并直接对其进行更改。即,您的 Mac 认为文件是本地的,因此您可以使用 GUI 编辑器编辑它们,但是当您“文件”->“保存”时,它实际上是远程保存文件。这样做的主要缺点是与服务器断开连接时无法工作。

  2. 远程挂载本地文件。这将允许您在断开连接时在本地工作,但不允许测试站点在断开连接时工作 - 这可能不是什么大问题。如果您在远程服务器上没有正确的工具/访问权限,则此选项可能不可用。

  3. (我的偏好。)使用 NetBeans IDE,它具有非常好的“复制到远程”功能。您在本地维护所有文件的完整副本,并直接编辑它们。当您在文件上单击“文件”->“保存”时,NetBeans 会将其保存在本地以透明方式通过 scp/ftp 到远程服务器。

I do this all the time, developing on a Windows/Linux/Mac workstation, and saving changes to a remote Linux server where they're immediately served back to my workstation's browser for testing. You've got a couple options:

  1. You could mount the remote files locally (like via sshfs) and make changes directly to them. I.e., your Mac thinks the files are local, so you can edit them with your GUI editor, but when you File->Save, it actually saves the file remotely. The main downside to this is that you can't work when disconnected from the server.

  2. Mount the local files remotely. This would allow you to work locally while disconnected but won't allow the test site to work when disconnected -- which may not be a big deal. This option might not be doable if you don't have the right tools/access on the remote server.

  3. (My preference.) Use NetBeans IDE, which has a very nice "copy to remote" feature. You maintain a full copy of all files locally, and edit them directly. When you hit File->Save on a file, NetBeans will save it locally and transparently scp/ftp it to your remote server.

你曾走过我的故事 2024-11-10 11:12:56

如何使用像 gitmercurial 这样的 DVCS,并让本地存储库运行提交后挂钩来运行 rsync,然后运行测试本身?

我有点困惑为什么不能从运行测试的同一个脚本运行 rsync 。如果运行 rsync -e ssh ,您可以在虚拟机和 Mac 之间设置自动公钥身份验证。在这种情况下,不会有任何关于 rsync 的手册。

How about using a DVCS like git or mercurial, and having the local repo run post-commit hooks to run the rsync and then the test itself?

I'm a bit confused about why you can't just run rsync from the same script that runs the test. If you run rsync -e ssh you can set up automatic public key authentication between the VM and the Mac. There won't be anything manual about the rsync in that case.

深海里的那抹蓝 2024-11-10 11:12:56

您也许可以设置一个 launchd 代理来完成您想要的简单设置。请参阅此问题和< a href="http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html" rel="nofollow noreferrer">launchd.plist 的手册页,了解有关 launchd WatchPath 键的更多信息。但看起来 WatchPath 可能不适用于 子目录内的更改

You might be able to set up a launchd agent to do what you want for a simple setup. See this question and the man page for launchd.plist for more information about the launchd WatchPath key. But it looks like WatchPath may not work for changes within sub-directories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文