Skip to main content
Question

Cannot set default printer using lpoptions in a script

  • March 19, 2024
  • 1 reply
  • 17 views

Spillou
Forum|alt.badge.img+5

Hello !

I have a script that helps me to reconfigure printers and sets them by default depending of their location, department, etc...

The problem is that I can't set the ~/.cups/lpoptions by using lpoptions -d default_printer. To run the command, I use a function that I found and that executes it as the logged user.

runAsUser() {
     if [[ "${loggedInUser}" != "loginwindow" ]]; then
          launchctl asuser "$userID" sudo -u "${loggedInUser}" "$@"
     else
          echo "No user logged in"
          exit 1
     fi
}

If I execute the command as test:
launchctl asuser 502 sudo -u myuser /usr/bin/lpoptions -d default_printer, it does nothing. The lpoptions file is not populated. Even if I run the command line locally, it doesn't work. It only works if I run the script or the command line as the specific local user.

Any help is welcome !

1 reply

Spillou
Forum|alt.badge.img+5
  • Author
  • Contributor
  • 30 replies
  • March 19, 2024

Well, I found how to manage this with "sudo -iu". I have used this command line in the past, but I got problems resolved with the "launchctl asuser" command, that doesn't work for me with the lpoptions command.