# Can Julia detect that it is running in headless machine?

**URL:** <https://discourse.julialang.org/t/can-julia-detect-that-it-is-running-in-headless-machine/119696>\
**Category:** General Usage\
**Tags:** question, operating-system\
**Created:** [September 22, 2024, 2:44pm UTC](https://discourse.julialang.org/t/can-julia-detect-that-it-is-running-in-headless-machine/119696 "2024-09-22T14:44:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HMegh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmegh/32/216684_2.png) [@HMegh](https://discourse.julialang.org/u/HMegh)\
**Post date:** [September 22, 2024, 2:44pm UTC](https://discourse.julialang.org/t/can-julia-detect-that-it-is-running-in-headless-machine/119696/1 "2024-09-22T14:44:18Z")

</div>

Hi,

I have a small project that I am running on my computer, as well as on a remote server. On my laptop, I typically use `Plots.jl` or `Makie.jl` for visualization, but on the server I am typically interested in saving my results as a `txt` or `jld2` file.

Now, I am wondering if there is an `is_headless()` function somewhere that would tell you whether you are in headless machine/environment or not. This will save me some time since I do not have to plot anything on the server, and I do not have to rewrite my code.

Here’s a simple example of what I am imagining `is_headless()` would do

```julia
f(x)=sin(x) #example 
x=range(0,10,1000)
y=f.(x)

if is_headless() #no gui 
    using DelimitedFiles
    writedlm("y.txt",y)

else #there is a gui
    using Plots
    plt=plot(x,y)
    savefig(plt,"plot.png") #or display(plt)
end

```

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [September 22, 2024, 3:46pm UTC](https://discourse.julialang.org/t/can-julia-detect-that-it-is-running-in-headless-machine/119696/2 "2024-09-22T15:46:32Z")

</div>

For Linux, try this:

```julia
is_headless() = !(ENV["XDG_SESSION_TYPE"] in ("wayland", "x11"))

```

Explanation: the function returns false if you’re running either Wayland or X11, the two common graphical systems on Linux.

---

<div class="post-metadata">

**Author:** ![HMegh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmegh/32/216684_2.png) [@HMegh](https://discourse.julialang.org/u/HMegh)\
**Post date:** [September 22, 2024, 4:16pm UTC](https://discourse.julialang.org/t/can-julia-detect-that-it-is-running-in-headless-machine/119696/3 "2024-09-22T16:16:19Z")

</div>

Neat! It worked on my Arch and Debian 12 servers. However, I got `ERROR: KeyError: key "XDG_SESSION_TYPE" not found` when running it on Ubuntu 22.04 and RHEL 8.10 (all using the latest v1.10.5)

For now, it seems like this works fine

`is_headless() = !(haskey(ENV,"XDG_SESSION_TYPE") && (ENV["XDG_SESSION_TYPE"] in ("wayland", "x11")))`

Having said that, I am curious as to why `XDG_SESSION_TYPE` is not an environment variable on some systems.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [September 22, 2024, 4:30pm UTC](https://discourse.julialang.org/t/can-julia-detect-that-it-is-running-in-headless-machine/119696/4 "2024-09-22T16:30:25Z")

</div>

How does this work?

```julia
is_headless() = !Sys.iswindows() && isempty(get(ENV, "DISPLAY", ""))

```

---

<div class="post-metadata">

**Author:** ![HMegh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hmegh/32/216684_2.png) [@HMegh](https://discourse.julialang.org/u/HMegh)\
**Post date:** [September 22, 2024, 4:53pm UTC](https://discourse.julialang.org/t/can-julia-detect-that-it-is-running-in-headless-machine/119696/6 "2024-09-22T16:53:00Z")

</div>

Thanks! This worked flawlessly.
