MacOS – Print from launchd task

launchdmacosprinting

I have a script that needs to be running in the background periodically (I've put it there with Lingon X).
That script (PHP based) is
1. polling a server,
2. retrieving a PDF file from the server
3. Saving it to disk
4. Printing it using exec("lpr myFile.pdf");
5. Playing a sound using exec("afplay ding.mp3");

When I run the script manually (php myscript.php), everything works perfectly.

However, when the script is running in the background, it does 1, 2, 3 and 5 normally, but does not print at all.

I've tried using lp instead of lpr, I've also compiled and run PDF-Printer (https://github.com/robwithhair/PDF-Printer) and still no paper is getting out of my printer. I've also tried another printer (you know…just in case). Still no luck.

I've tried to get the output from the lpr command to see if it was returning any kind of an error message, yet, nothing. There's also nothing in the printer queue.

I've also tried to run this script as root and as a regular user and still nothing makes the printer…print.

Anyone has any idea of how to accomplish this?


Here's the output of lpc status:

Brother_HL_2240_series:
printer is on device 'usb' speed -1
queuing is enabled
printing is enabled
no entries
daemon present

Best Answer

Try to redirect the stderr so you can see what is happening.

exec("lpr myFile.pdf 2> /tmp/lpr_error");