Find the answer to your Linux question:
Results 1 to 4 of 4
Hey, I have a question about xdg-open. I basically need it to open a URL in a browser after the installer for that product has finished. To run the installer ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    4

    running xdg-open as a non root user


    Hey, I have a question about xdg-open. I basically need it to open a URL in a browser after the installer for that product has finished. To run the installer you must be root, this is where the problem occurs. I checked xdg-open and you can't run it as root, well you can but in the manual page it suggests you don't. I ran it as root, my browser didn't fire it up, I ran it as a non root user and it worked fine.

    From this I thought that if the user who installs the software is root (because they are made to be) then I will just run xdg-open as nobody.

    su -c "xdg-open $URL" nobody

    I them got a message saying owner of /tmp/orbit-admin is not the current user.

    Is there any way I can run xdg-open as root? Is there work around or alternative? Bare in mind they can't be to complex due to the nature of the project.

    Any thoughts and feelings are welcome.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    You should run xdg-open as the user that is running the desktop session (i.e., not root but whatever account the person used to log in to Gnome/KDE, etc.).

    Also, try runuser, instead of su.

    Now if you are running the script as root, then obviously you can't do things like use the $USER variable (it will be root) or the output of `id -ru` (will be 0). but if you are using bash, and you know that the person will have used su to elevate to root privileges (instead of using sudo, e.g.), then you can get the correct user id like so:

    Code:
    #!/bin/bash
    
    # get the pid of the shell owning this script
    gpid=$(ps --no-heading -o ppid -p $PPID)
    [ -z "$gpid" ] && echo "failed to get gpid" && exit 1
    #echo grandparent pid is $gpid
    
    # get the pid of the su command that spawned the shell
    supid=$(ps --no-heading -o ppid -p $gpid)
    [ -z "$supid" ] && echo "failed to get su pid" && exit 1
    #echo su pid is $supid
    
    # get the user id of the user that executed su
    user=$(ps --no-heading -o user -p $supid)
    [ -z "$user" ] && echo "failed to get userid" && exit 1
    #echo user is $user
    
    # run the xdg-open command
    runuser $user /usr/bin/xdg-open http://www.example.com
    If that doesn't work, hopefully you can figure out how to modify the ps output. Post back if you can't get it to work.

  3. #3
    Just Joined!
    Join Date
    Sep 2011
    Posts
    4
    Everything is fine except I don't have the runuser command. Just trying to look for workarounds. Any thoughts?

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    I didn't realize that SUSE does not have runuser...you can still use su, e.g.:
    Code:
    su $user -c '/usr/bin/xdg-open http://www.example.com'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •