diff --git a/scripts/gitaly-test-spawn b/scripts/gitaly-test-spawn
index 2e0e51124afe47107b756d210fc3fe302c82c153..3bf2de7676085a8eac169aa7cbda05d64e0b1c49 100755
--- a/scripts/gitaly-test-spawn
+++ b/scripts/gitaly-test-spawn
@@ -9,16 +9,8 @@ class GitalyTestSpawn
   include GitalySetup
 
   def run
-    install_gitaly_gems if ENV['CI']
-    check_gitaly_config!
-
-    # # Uncomment line below to see all gitaly logs merged into CI trace
-    # spawn('sleep 1; tail -f log/gitaly-test.log')
-
-    # In local development this pid file is used by rspec.
-    IO.write(File.expand_path('../tmp/tests/gitaly.pid', __dir__), start_gitaly)
-    IO.write(File.expand_path('../tmp/tests/gitaly2.pid', __dir__), start_gitaly2)
-    IO.write(File.expand_path('../tmp/tests/praefect.pid', __dir__), start_praefect)
+    install_gitaly_gems
+    spawn_gitaly
   end
 end
 
diff --git a/spec/support/helpers/gitaly_setup.rb b/spec/support/helpers/gitaly_setup.rb
index 3902d2b18472b008f959831ae8e223ce09d441e0..6284a1bcef8863de212c52b9d57482bb6dcde371 100644
--- a/spec/support/helpers/gitaly_setup.rb
+++ b/spec/support/helpers/gitaly_setup.rb
@@ -285,25 +285,27 @@ def stop(pid)
   end
 
   def spawn_gitaly
-    spawn_script = Rails.root.join('scripts/gitaly-test-spawn').to_s
-    Bundler.with_original_env do
-      unless system(spawn_script)
-        message = 'gitaly spawn failed'
-        message += " (try `rm -rf #{gitaly_dir}` ?)" unless ENV['CI']
-        raise message
-      end
-    end
+    check_gitaly_config!
 
-    gitaly_pid = Integer(File.read(TMP_TEST_PATH.join('gitaly.pid')))
-    gitaly2_pid = Integer(File.read(TMP_TEST_PATH.join('gitaly2.pid')))
-    praefect_pid = Integer(File.read(TMP_TEST_PATH.join('praefect.pid')))
+    gitaly_pid = start_gitaly
+    gitaly2_pid = start_gitaly2
+    praefect_pid = start_praefect
 
     Kernel.at_exit do
+      # In CI this function is called by scripts/gitaly-test-spawn, triggered a
+      # before_script. Gitaly needs to remain running until the container is
+      # stopped.
+      next if ENV['CI']
+
       pids = [gitaly_pid, gitaly2_pid, praefect_pid]
       pids.each { |pid| stop(pid) }
     end
 
     wait('gitaly')
     wait('praefect')
+  rescue StandardError
+    message = 'gitaly spawn failed'
+    message += " (try `rm -rf #{gitaly_dir}` ?)" unless ENV['CI']
+    raise message
   end
 end