Home / Alt manpages / gsnd(1)

  • gsnd(1)
  • User command
  • linux

Run Ghostscript Headless with gsnd

Use gsnd when you need Ghostscript to crunch PostScript without ever trying to open a display. It suits small calculations, scripts that run without X, and scheduled jobs where a page preview would just get in the way. First run takes about ten minutes if Ghostscript is already installed.

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. The underlying Ghostscript manual documents the spelling -dNODISPLAY; the wrapper accepts the equivalent -NODISPLAY form.

1. Check that gsnd is installed

Confirm the executable and its version first. Neither command changes anything or needs elevated privileges.

$ command -v gsnd
/usr/bin/gsnd
$ gsnd --version
10.02.1

If the command is missing, install the ghostscript package with your distribution's normal package manager. Do not reach for sudo just to run a calculation: ordinary evaluation should run as your own user.

2. Evaluate a short PostScript expression

Pass PostScript with -c. == prints a readable value, and quit ends the interpreter explicitly. -q suppresses startup messages, and -dBATCH tells Ghostscript to exit once it has processed its input.

$ gsnd -q -dBATCH -c '1 2 add == quit'
3

That output is the calculated value, not a graphical preview. Because the display device is disabled, this runs fine in a text-only shell, an SSH session or a cron job.

3. Feed a PostScript file on standard input

A lone hyphen tells Ghostscript that standard input is a file or pipe. Handy for generated input, and it avoids a temporary script file.

$ 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 never waits on 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 file you trust and can read. A zero exit status means Ghostscript completed without a fatal error; it does not prove the script produced the result you wanted.

4. Keep diagnostic output separate

Ghostscript can write values to standard output from PostScript. Useful in a pipeline, but mixing diagnostics into it can corrupt whatever the next program in the chain expects. Use == deliberately, and redirect 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.

Warning

Removing that file with rm -- /tmp/gsnd-result.txt is irreversible, so check the path before you run it. If the output is valuable, 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, restricting file operations that PostScript can perform. That is not a substitute for running untrusted input in a separate account or sandbox. Treat any downloaded or supplied PostScript and PDF as active input, not as harmless documents.

Security boundary

Do not add -dNOSAFER just because a script fails to open a file. The local manual says it loosens normal protection and recommends it only for debugging or tightly controlled workflows. If a controlled script genuinely needs access, work out exactly which paths it must read or write, then use the documented permission options for your Ghostscript release. Never open up a whole directory just to silence an error.

There is normally no need for root. Running gsnd with sudo hands the PostScript program that account's authority and turns a small mistake or a malicious file into a bigger one.

6. Diagnose a command that appears to hang

The usual culprit is an accidental interactive session. Add -dBATCH, supply a file or -c program, and end short programs with quit. If you are unsure what was passed, check your shell history and rerun with -q removed so startup messages stay 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 returns control without touching the input file.

Done means

  • Version confirmed. gsnd --version identifies the installed Ghostscript release.
  • Calculation runs clean. A short calculation with -q -dBATCH -c returns the expected value.
  • Input ends properly. File or pipe input ends cleanly instead of opening an interactive prompt.
  • Output checked before overwrite. Any output file is written to a deliberate path and checked before replacement.
  • Untrusted input stays contained. It runs under the default SAFER mode, never as root.