Firefox browser window appears despite using headless configuration - fails when executed via cron

I’m trying to create an automated script that runs through cron, but I’m having issues with browser automation. Even though I’m using a headless setup, Firefox still opens a visible window when the script runs.

The main problem is that when I execute this manually, everything works fine. But when cron tries to run it, the script fails because it can’t open the display.

Here’s my current approach:

#!/usr/bin/env ruby
require 'watir-webdriver'
require 'headless'

# Start headless session
headless_session = Headless.new
web_driver = Watir::Browser.start 'http://dashboard.example.com/login'

# Navigate to login page
web_driver.link(:xpath => '//div[@class="login-container"]/a').when_present.click
web_driver.text_field(:id => 'username').when_present.set '[email protected]'
web_driver.button(:id => 'continue').click
web_driver.text_field(:id => 'password').when_present.set 'mypassword'
web_driver.button(:id => 'login').click

# Go to main dashboard
web_driver.goto 'https://dashboard.example.com/main'

# Extract required data
data_points = []
# ... data extraction logic here ...

# Cleanup
web_driver.close
headless_session.destroy

# Output results
timestamp = Time.now
puts "Data extracted at #{timestamp}"
data_points.each_slice(2) { |pair| puts pair.join(" - ") }

My shell script looks like this:

#!/bin/bash
today=$(date +"%Y_%m_%d")
ruby data_scraper.rb > ~/reports/daily_$today.txt

I thought headless mode was supposed to run without opening any GUI windows. Am I configuring something wrong? The script completes but produces empty output when run via cron.

Note: I also tried using a different headless browser but ran into timeout issues with form fields, especially on authentication pages.

u forgot to call .start on headless_session b4 launching the browser. cron runs in a diff env without display vars - try adding export DISPLAY=:99 in your bash script or use Headless.ly do |headless| for auto start/stop!

Your headless session and browser aren’t syncing up properly. Cron doesn’t have the X11 display context that you get when running manually from your desktop. Your Headless instance gets created but never actually starts before Watir tries to launch Firefox. I’ve hit this same issue before - calling headless_session.start right after you create it fixes the display connection problems. Also, cron runs with a bare-bones PATH and environment, so Firefox can’t find the libraries it needs. Try adding ENV['DISPLAY'] = ':99' or similar virtual display setup in your Ruby script. The empty output means your script’s dying during browser launch, not making it to the data extraction part. Add some error handling around browser initialization so you can see what’s actually breaking when cron runs it.

You’re not actually starting the headless session. You create the Headless instance but never call headless_session.start before launching the browser - Firefox just tries to open normally. Cron runs in a bare-bones environment without the display variables your local session has. Even with headless setup, you should set the DISPLAY variable explicitly or use headless_session.start in a block. I ran into the same thing with automated scrapers under cron. Fixed it by restructuring how I initialized headless and making sure cleanup happened if the script got interrupted. Wrap your browser stuff in a begin/rescue block so the headless session gets destroyed even when errors happen.