Operating System - HP-UX
1855644 Members
7834 Online
104113 Solutions
New Discussion

Re: Using Cron to Print Mail automatically

 
Rick Knisely
Occasional Advisor

Using Cron to Print Mail automatically

I need to setup up cron job that will check a users mail box every 15 minutes and if it finds new mail, it will print the mail to laserprinter I would identify in cron.

Thank you.
Don't Sweat the Small Stuff!
6 REPLIES 6
Evert Jan van Ramselaar
Valued Contributor

Re: Using Cron to Print Mail automatically

Not very neat, but this might work:

0,15,30,45 * * * * lp -d /var/mail/ ; cat /var/mail/ >> /var/mail/.printed
Contrary to popular belief, Unix is userfriendly. It just happens to be selective about who it makes friends with.
Evert Jan van Ramselaar
Valued Contributor

Re: Using Cron to Print Mail automatically

Not very neat, but this might work:

0,15,30,45 * * * * lp -d /var/mail/ ; cat /var/mail/ >> /var/mail/.printed ; echo "" > /var/mail/
Contrary to popular belief, Unix is userfriendly. It just happens to be selective about who it makes friends with.
Kent Ostby
Honored Contributor

Re: Using Cron to Print Mail automatically

To find if new mail is available, you can do the following :


if [ -s $mailfile ]
then
INSERT COMMANDS IN HERE
fi

As far as reading and printing, you can use readmail(1) to read through a list of mails and quit when you get to the end of the list.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Rick Knisely
Occasional Advisor

Re: Using Cron to Print Mail automatically

Evert your suggestion is very close to answer. I am getting the mail printed on the quarter hour, only problem is it printing old and new mail. Anyway to suppress printing jobs it printed in previous quarter hour? Also, anyway to suppress header information?

Rick
Don't Sweat the Small Stuff!
Kevin Wright
Honored Contributor

Re: Using Cron to Print Mail automatically

if [[ -s $MAILBOX ]]
lp $MAILBOX
mv $MAILBOX $MAILBOX.old
fi
Gregory Fruth
Esteemed Contributor

Re: Using Cron to Print Mail automatically

If a message is received while your cron job is
accessing the mailbox you'll see a partial
message. If a message is deleted while your
cron job is accessing the mailbox you might
corrupt the mailbox! Mail clients like elm
typically use file locking to prevent mailbox
corruption and to make sure you only see
complete messages.

You're probably better off doing this sort
of thing from sendmail itself, and not from
cron. Either use an alias or a .forward file
(see aliases(5)). Example alias:

some_user: \some_user, "| lp -dsome_printer"

This will print each message as it's
received. It will also save the message
to the mailbox as usual. If you don't want
the message to be saved to the mailbox, use:

some_user: "| lp -dsome_printer"

If you want to process the message to
strip off the headers & whatnot, use a
script in the alias:

some_user: "| my_script | lp -dsome_printer"

If you really want to batch the print jobs
up, you can use an alias to save the messages
to another file, and have a cron job print
that file every 15 minutes. You'll still
have to deal with some sort of locking,
though.

HTH