0

I want to write a little python script to automate Jekyll blog creation, but popen() seems to block and not call asynchronously.

The expected behavior would be:

  1. Start jekyll serve --livereload asynchronously
  2. Start firefox-esr http://127.0.0.1:4000 async and wait for it(, or synchronously, this is not relevant in my use case)
  3. After termination of firefox, terminate Jekyll too.
jekyll = subprocess.Popen(['jekyll', 'serve', '--livereload'])
print('This never gets displayed')
time.sleep(3)
firefox = subprocess.Popen(['firefox-esr', 'http://127.0.0.1:4000'])
firefox.wait()
jekyll.terminate()

But this only starts Jekyll and outputs its stdout to the terminal.

This problem only appears with Jekyll. ping or any other command/program i tried work fine.

Any ideas on what I did wrong?

baifaine
  • 5
  • 2
  • Does it show any errors in the terminal? – Linux Geek Apr 21 '21 at 17:40
  • No. it shows the jekyll stdout. When killing the python script via ctrl+c the jekyll process is not terminated if that helps. Im quite confused, as i did a completely similar thing with another piece of software, an it worked like a charm. – baifaine Apr 21 '21 at 17:57
  • Can you try any other command instead of `Jekyll` which doesn't constantly output something in the terminal, and check the terminal for output/error. And are you sure that you have `firefox-esr`. – Linux Geek Apr 21 '21 at 18:00
  • when reversing the commands, firefox gets started, then jekyll starts blockingly. With other commands like `ping` i have no issues. Are you aware of an hotfix? Maybe writing a bash script that only calles jekyll that i could call asynchronously and then terminate. It clearly seems to be a problem with Jekyll. – baifaine Apr 21 '21 at 18:09
  • I am not aware of a hotfix, but bash script could help, or even threading could help but not sure though. I will try doing it on my local machine. – Linux Geek Apr 21 '21 at 18:18
  • Thank you very much for your help! – baifaine Apr 21 '21 at 18:19

1 Answers1

0

If you are on Linux, you can create a simple bash script for it.

#!/bin/bash

jekyll serve --livereload & 
sleep 5
firefox-esr http://127.0.0.1:4000 &>/dev/null


pid=$(pgrep firefox-esr)

while :
do
    sleep 5
    if [ -z  "$pid" ]
    then
        pkill ruby 2>/dev/null
        echo "Killed jekyll"
        break
    fi
done

Give this file execution permissions with

chmod +x filename.sh

Then run this bash script with

./filename.sh &

This will make your script run in the background.

baifaine
  • 5
  • 2
Linux Geek
  • 957
  • 1
  • 11
  • 19
  • Thank you very much! But unfortunately, this does not work: It starts Jekyll and firefox fine (but a little typo in the address) but it also fails to kill jekyll for some reason. – baifaine Apr 22 '21 at 07:41
  • 1
    I took a look at htop and modified your answer. htop says the command is ruby2.5 . Killing ruby does the trick for me but is arguably brutal. I can live with the consequences as i use ruby only for Jekyll, but others may not. Thank you very much, i am thinking of accepting this answer for me. – baifaine Apr 22 '21 at 07:47