Run Ghostscript Without a Display Using gsnd
You will use gsnd to run Ghostscript without opening or initialising a normal display device. That is useful for small PostScript calculations, scripts that must run without X, and jobs where you want Ghostscript to consume input without presenting a page. Allow about ten minutes for a first run if Ghostscript is already installed.
The route
Jump straight to the step you need, or tick off Done means at the end.
This guide describes the installed Ghostscript 10.02.1 package on this machine. The local gsnd(1) manual is dated 1 November 2023 and says that gsnd invokes gs with -NODISPLAY, then forwards the remaining command-line arguments. In the underlying Ghostscript manual, the documented spelling is -dNODISPLAY; the wrapper accepts the equivalent -NODISPLAY form.
1. Check that gsnd is installed
Start by checking the executable and its version. This does not change any files or require elevated privileges.
$ command -v gsnd
/usr/bin/gsnd
$ gsnd --version
10.02.1
If the command is missing, install the ghostscript package using your distribution's normal package manager. Do not use sudo just to run a calculation; ordinary evaluation should run as your own user.
2. Evaluate a short PostScript expression
Pass PostScript with -c. The == operator prints a readable value, and quit ends the interpreter explicitly. -q suppresses normal startup messages, while -dBATCH tells Ghostscript to exit after processing its input.
$ gsnd -q -dBATCH -c '1 2 add == quit'
3
The output is the value calculated by Ghostscript, not a graphical preview. The wrapper has disabled the normal display device, so this command can run in a text-only shell, an SSH session or a scheduled job.
3. Feed a PostScript file on standard input
Use a lone hyphen to tell Ghostscript that standard input is a file or pipe. This is convenient for generated input and avoids creating a temporary script.
$ printf '%s\n' '10 4 sub ==' 'quit' | gsnd -q -dBATCH -
6
The shell's printf is only producing input; gsnd interprets the two PostScript lines. The process exits at end of input after quit, so it does not wait for an interactive prompt.
For a saved script, pass its path after the options:
$ gsnd -q -dBATCH /path/to/check.ps
$ printf 'exit status: %s\n' "$?"
exit status: 0
Replace the path with a readable file that you trust. A zero exit status says that Ghostscript completed without reporting a fatal error; it does not prove that the script produced the result you wanted.
4. Keep diagnostic output separate
Ghostscript can write values to standard output from PostScript. That is useful in a pipeline, but it can corrupt another program's expected data stream if you mix diagnostics with it. Use == deliberately, and redirect the shell's standard output only when you know which stream the next command expects.
$ gsnd -q -dBATCH -c '20 5 div == quit' > /tmp/gsnd-result.txt
$ test "$(cat /tmp/gsnd-result.txt)" = 4
$ echo "result check passed"
result check passed
The temporary file is only an example. Remove it when it is no longer needed with rm -- /tmp/gsnd-result.txt. That deletion is irreversible, so check the path before running it. If you are processing valuable output, write to a new name and inspect it before replacing an existing file.
5. Understand the safety boundary
Ghostscript 10.02.1 enables SAFER mode by default. It restricts file operations performed by PostScript, but it is not a substitute for running untrusted input in a separate account or sandbox. Treat downloaded or supplied PostScript and PDF files as active input, not as harmless documents.
Do not add -dNOSAFER merely because a script fails to open a file. The local manual says that it loosens the normal protection and recommends it only for debugging or very controlled workflows. If a controlled script genuinely needs access, first establish exactly which paths it must read or write, then prefer the documented permission options for your Ghostscript release. Never grant broad access to a directory just to silence an error.
There is normally no need for root privileges. Running gsnd with sudo gives the PostScript program the authority of that account and increases the impact of a mistake or malicious file.
6. Diagnose a command that appears to hang
The most common distraction is accidentally starting an interactive session. Add -dBATCH, provide a file or -c program, and finish short programs with quit. If you are unsure what was passed, inspect the command in your shell history and rerun it with -q removed so startup messages remain visible.
$ gsnd -h 2>&1 | sed -n '1,12p'
GPL Ghostscript 10.02.1 (2023-11-01)
Copyright (C) 2023 Artifex Software, Inc. All rights reserved.
Usage: gs [switches] [file1.ps file2.ps ...]
Most frequently used switches: (you can use # in place of =)
-dNOPAUSE no pause after page | -q `quiet', fewer messages
-g<width>x<height> page size in pixels | -r<res> pixels/inch resolution
If an input file reports an access error, check its path and permissions first. Do not jump straight to -dNOSAFER. If the process is genuinely stuck and you do not need its output, press Ctrl-C; the shell will return control without changing the input file.
Done means
gsnd --versionidentifies the installed Ghostscript release.- A short calculation runs with
-q -dBATCH -cand returns the expected value. - File or pipe input ends cleanly instead of opening an interactive prompt.
- Any output file is written to a deliberate path and checked before replacement.
- Untrusted input stays under the default SAFER mode and is not run as root.