i use ghostscript to save pdf files as tif. I've written a little function in the shell script i use to delete the uncomplete output folder when i abort the process.

function lock-remove {
 rm -r output-folder
trap lock-remove EXIT

The function is running when i close the terminal window or when i ctrg+c. The probem with ghostscript is that after the process is completed ghostscript start something like a n interactive mode. I don't know why. i can prevent this with "-dBATCH" or "-c quit" at the end of the command. This both are interpreted as ctrg+c or closing the terminal windows and the function is started and the output folder is deleted.

How can i finish the ghostscript process without deleting the output. At the moment the function is commented out.

  • 1
    My advice: Try pdftocairo for pdf -> image conversions. gs can really be a pain...
    – Freddy
    Jul 20 '19 at 22:37

Your trap on EXIT is not specifically executed when you close the terminal window or press Ctrl+C, it is executed when then shell exits.

Closing the terminal will kill the shell running in the terminal, and pressing Ctrl+C in GhostScript will terminate GhostScript, and depending on the commands that follow will eventually also terminate the shell.

But a regular exit of GhostScript with -c quit will also eventually terminate the shell and then execute the trap.

If you want to keep the files, cancel the trap after GhostScript exits successfully.

Regarding the interactive mode of GhostScript, the manual says:

The interpreter exits gracefully when it encounters the "quit" command (either in a file or from the keyboard), at end-of-file, or at an interrupt signal (such as Control-C at the keyboard).

The option -c quit will execute the quit command and therefor terminate GhostScript. You can execute arbitrary commands with -c. So you can just append quit to the commands you pass for the conversion, Or you can feed the commands on stdin and GhostScript will terminate at end-of-file.


The syntax for the trap command is

trap [-lp] [[arg] signal_spec ...]

If arg is absent (and there is a single sigspec) or -, each specified signal is reset to its original disposition

You can always specify another trap action, or no action. This example will first set up command echo original trap, then echo modified trap andfinally cancel the trap so that no commands are executed when the shell exits.

trap 'echo original trap' exit
# run some commands
trap 'echo modified trap' exit
# run some commands
trap - exit
# run some commands

Edit Here an example:

Typing quit, the trap is not executed.

$ bash -c 'trap "echo;echo exit" exit; gsnd -q ; trap - exit'

Typing Ctrl+C, the trap is executed.

$ bash -c 'trap "echo;echo exit" exit; gsnd -q ; trap - exit'
  • what do you mean with "cancle the trap after ghostscript exits secessfully"? how can i do this?
    – fmeier
    Jul 25 '19 at 11:16
  • I added an example how to change and cancel a trap.
    – RalfFriedl
    Jul 25 '19 at 16:57
  • I dont understand that. with ctrg+c or closing the terminal window i run the trap. Also when i run -c quit at the end of the command. as i understand the problem i have to finf a way to differ betwen ctrg+c and -c quit. Your example does the same. It runs the same trap when i ctrg+c also when i run -c quit.
    – fmeier
    Jul 26 '19 at 5:46
  • Look at the example I added.
    – RalfFriedl
    Jul 26 '19 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.