Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for some guidance on creating a script that will check a specific folder, print all documents (if any) and then delete those documents. I would also like to run this script as a cron job.

The reason I am looking to do this is because I currently have a PHP webapp that creates pdf invoices on the fly and saves them in a certain folder, but I have no way of automatically printing these documents from the web (I imagine for security/spam issues).

I am no language preference but would ideally like the script to be able to control how many copies are printed, which tray to use in the printer etc.

share|improve this question
How would you like to control the # of copies? Also from the cron entry, or only when invoked from the commandline. Make sure you have some locking mechanism, or you will end up with double printouts if cron and your manual invocation coincide. – Anthon Apr 17 '13 at 14:00
What have you tried? What is your specific question? Also, this sounds a bit like an XY problem. At any rate, you are defeating whatever rule prevents you from invoking lp directly from your PHP script. That may by fine, but it's a bit ironic. Makes me wonder what your sysadmin would think. – depquid Apr 17 '13 at 15:32
Thanks @depquid for your very kind and constructive comment. What exactly do you mean, "invoking lp directly from your PHP script?". – user37353 Apr 17 '13 at 15:58
@user37353 E.g. in PHP: exec("lp generated_invoice.pdf", $output, $status); – depquid Apr 17 '13 at 16:03
Sure you want to delete those invoices? In many regions around the world you are bound to keep archived for a year, two or more. Either electronically, on paper or both. – Runium Apr 17 '13 at 16:58

I haven't tested this, but this should work:

cd some-directory

for file in *.pdf ; do
    lp -n2                        # number of copies
    -o media=a4\                  # media
    -o number-up=2\               # number of pages per sheet
    -o fit-to-page\               # scale to page
    -o sides=two-sided-long-edge\ # duplex (can be 'one-sided')
    -o tray1\                     # tray
    -d SomePrinter\               # device
    rm "$file"

You will need to install and configure cups before you can use lp. See the lp manpage (man lp) for more information on common options.

You might want to archive the files instead and delete them once you know they have been printed.

To do this replace rm "$file" with:

mkdir archived
mv "$file" archived

As mentioned in Anthon's comment, you may want to implement some sort of file locking to make sure your cron job and applications don't interfere with each other. For this, you have a few options. flock is probably your best bet; it has excellent PHP support.

You can throw this script right into a cronjob. Just make sure to set your path to include /usr/bin and /bin.

Don't run the script as a cronjob or with your actual files until you know it works.

share|improve this answer
+1 for not deleting until you know it printed properly. I would include some lock, to make sure a manual invocation (for multiple copies) does not interfere with the one from cron. – Anthon Apr 17 '13 at 14:02
This looks very interesting, would it be possible to integrate something like CUPS to set which printer to print to? For example sending #1 copy to a black and white printer, then sending 2 copies to a colour printer? – user37353 Apr 17 '13 at 14:10
@user37353: lp is part of cups. To select a printer, set the PRINTER environmental variable (adding to answer now). – Evan Teitelman Apr 17 '13 at 16:32
To use two printers, you should make a function that accepts the file you are printing, the number of copies you want to print, and the device you wish to print to and call that function appropriately. – Evan Teitelman Apr 17 '13 at 17:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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