The following command will create a pdf file under ~/PDF/:

enscript -B -P PDF  bloom.c

I want to move the output pdf file here

mv "`ls -dtr1 ~/PDF/* | tail -1`" .

But I can't run the mv command immedately follow the enscript command, because enscript sends the input file to the printer PDF, and returns immediately before the printer finishes creating a pdf file.

A very bad solution is to insert the following command between the two commands

sleep 5

It is bad because I don't know how long I shall wait.

Note that I know there is other better way to do the same thing without having to run the mv command after enscript, but I just wonder if there is any way to make enscript block till the printer finishes its job? I remember having similar thoughts for other commands besidesenscript. I am assuming that enscript doesn't provide such an option, so would be interested in whether bash or other program can make that happen. Signal handling might be a solution, but I don't know which signal to catch.

  • Hi @Tim, i would suggest that you try wait instead of sleep stackoverflow.com/questions/13296863/… – user88036 Sep 20 '18 at 20:45
  • Does lpstat -p PDF show something different if you run it while the printer is generating the output file vs. after it finishes generating the file? If so, it may be possible to write a shell script to do what you need. – Mark Plotnick Sep 20 '18 at 20:56
  • @Goro enscript -B -P PDF code/bloom.c & wait $! will not work, because the job of enscript returns immediately. – Tim Sep 20 '18 at 22:36
  • @Tim. I would suggest that you change your approach. Instead of sleep let the script sense when the PDF file is really exist in the path then move it. Use if then statement – user88036 Sep 20 '18 at 23:00
  • enscript just runs the lpr command, along with any options that you pass to it. I don't know offhand how to make cups not spool, that is, how to make the lpr command only exit after the printer driver has run to completion. If you don't get an answer here, you might consider asking the more general question about the cups lpr command. – Mark Plotnick Sep 24 '18 at 15:49

As this is too large to put into a comment, have you tried checking whether any files in the ~/PDF directory are still open while they're begin spooled by using lsof (LiSt Open Files)?

Add this code snippet after your enscript -B -P PDF bloom.c:

# Go into endless loop and break when all files in */PDF/* are closed.
while :
    if ! [[ `lsof | grep /PDF/` ]]
    sleep 1

and before the

mv "`ls -dtr1 ~/PDF/* | tail -1`"

This way you lose only 1 second...

For more info:

  • lsof will list all currently open files
  • the obligatory man lsof will give you even more details.


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.