9

Being lazy, I would like to chain the following two commands so that it will work with one simple bash alias.

jekyll -w serve

starts up the development server for my jekyll instance.

open "http://localhost:4000"

launches my jekyll application in my default browser.

How can I chain them together so that I can type one simple alias in my command line to serve and launch?

NOTES

  • Note using either && or ; to chain jekyll -w serve and open "http://localhost:4000" will not work because the jekyll -w serve launches the persistent webrick process in stdout. Which means that the 2nd command open "http://localhost:4000" will not be executed because the first process (webrick) never did "complete".

  • When webrick starts, we can see a typical output as follows:-

Configuration file: /Users/calvin/work/calviny/_config.yml
            Source: /Users/calvin/work/calviny
       Destination: /Users/calvin/work/calviny/_site
      Generating... done.
 Auto-regeneration: enabled
[2013-09-08 18:43:58] INFO  WEBrick 1.3.1
[2013-09-08 18:43:58] INFO  ruby 1.9.3 (2013-06-27) [x86_64-darwin11.4.2]
[2013-09-08 18:43:58] INFO  WEBrick::HTTPServer#start: pid=6183 port=4000
4

6 回答 6

12

v3.70 开始,不再需要这些 hack。现在jekyll serve具有 LiveReload 功能,并会自动生成带有该-o, --open-url选项的默认浏览器。该-l, --livereload选项将在更改时自动重建和刷新浏览器。

bundle exec jekyll serve -l -o
于 2018-06-25T00:03:14.620 回答
1
function jek {
    jekyll -w serve & open "http://localhost:4000" 
}

解决问题。一个&.

于 2013-09-08T11:37:27.607 回答
0

我不是 100% 使用终端命令,但这似乎已经为我解决了。测试了几次没有问题。(如果对您有任何影响,我正在运行 osx 10.8)

function jek {
    jekyll -w serve & sleep 5 && open "http://localhost:4000"
}
于 2014-01-08T11:02:11.890 回答
0

正如 hek2mgl 建议的那样,您可以这样做expect

set timeout 5
spawn jekyll serve
expect {
    -re {Server address:\s+(.+)$} {
        set address $expect_out(1,string)
    }
}
expect "Server running..."
catch {
    exec xdg-open $address
}
interact
于 2014-12-21T16:38:47.077 回答
0

jekyll serve > /dev/null 2>&1 & sleep 5 && open "http://localhost:4000"

于 2018-03-26T00:28:13.880 回答
-1

目前我看到以下选项:

  • 用于expect在浏览器上启动 jekyll 和稍后,这取决于某行输出jekyll表明现在启动浏览器是安全的。

  • 编写一个按顺序尝试连接到端口的小程序4000。如果可以在给定时间内建立连接,则可以启动浏览器。当然,您也可以使用 wget 或 curl 并依次尝试加载 index.html。

  • jekyll您可能会根据某些文件的存在来识别它已准备就绪。创建此类文件,您可以编写一些代码inotifywait来启动浏览器。(但我不知道,我不确定这是否真的是一个选择)jekyll

于 2013-09-08T10:26:52.137 回答