You are not logged in.

#1 2016-10-27 22:00:32

Malikos
Member
Registered: 2015-12-12
Posts: 46

[SOLVED] How to run command as root after X starts

Hello,

I have a following question.

I need to run this command (disabling discrete GPU):

echo '\_SB.PCI0.P0P2.DGPU._OFF' > /proc/acpi/call

I've read that I can do this via another command through /etc/tmpfiles.d/ but then my system hangs with black screen.
I think that my laptop has problems when I disable my GPU before X starts, because when i run the above command through terminal it works perfectly.

So, can you provide me some simple way to execute this command either just after user login or simply after X starts?

[edit]
And yes, I know about bumblebee, bbswitch etc. they just don't work for my case. This is an old laptop, pre 2011 and I think it doesn't support optimus at all.
I just want to execute this command as root after my X starts.

Last edited by Malikos (2016-10-29 14:51:35)

Offline

#2 2016-10-27 22:50:50

onslow77
Member
Registered: 2014-09-21
Posts: 283

Re: [SOLVED] How to run command as root after X starts

https://wiki.archlinux.org/index.php/Sudo

Last edited by onslow77 (2016-10-27 22:51:28)

Offline

#3 2016-10-27 23:41:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] How to run command as root after X starts

You could write a --user service file: https://wiki.archlinux.org/index.php/Systemd/User


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#4 2016-10-28 04:33:01

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

jasonwryan wrote:

You could write a --user service file: https://wiki.archlinux.org/index.php/Systemd/User

Okay, thanks, this seems like a good option.
But the question is, can I run command writing an acpi call using a user service?

Because firstly it needs root privileges and secondly when I manually run it it doesn't work with simple sudo. It wants a root shell, so I need to either do sudo su or start bash as superuser.

So is this even possible for a user service?

Edit:
Okay, maybe if I make a user service which launches a script with my command, and then I add to sudoers file that my user can run this script as root without password? Is this a good procedure?

Last edited by Malikos (2016-10-28 04:47:58)

Offline

#5 2016-10-28 04:50:12

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] How to run command as root after X starts

If it doesn't work with sudo, then the --user approach will fail.

How do you start X, with a DM or manually? You could try running a system service that waits for your respective target, but that has been (in my experience) pretty flaky in the past.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2016-10-28 04:53:15

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

jasonwryan wrote:

If it doesn't work with sudo, then the --user approach will fail.

How do you start X, with a DM or manually? You could try running a system service that waits for your respective target, but that has been (in my experience) pretty flaky in the past.

I start X with a SDDM.

So do you think that there's there any chance of automating this command?
Damn, I'm fighting with this GPU almost a week hmm

Offline

#7 2016-10-28 04:56:34

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] How to run command as root after X starts

DMs use graphical target; so have the service wait for that.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2016-10-28 05:10:44

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

Okay, thank you very much.
I'll try it in a few hours when I come back home and then report here.

With sudo there's a weird thing that the template script from acpi call which disables GPU has "sh" extension and works with simple sudo. Although, when I run my command with sudo it says permission denied, and I need to do sudo su.

I'll look into that and maybe that first approach which we talked about will work.

Last edited by Malikos (2016-10-28 05:11:47)

Offline

#9 2016-10-28 07:54:23

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 68,317

Re: [SOLVED] How to run command as root after X starts

echo '\_SB.PCI0.P0P2.DGPU._OFF' | sudo tee /proc/acpi/call

The problem is that you'll be executing "echo" as root (yeah! - but useless) while the redirection (">") which would require root privs still runs as your ordinary user.

So you either wrap that entire call into a script and sudo the script or you ensure the writing process (tee is a T-pipe) has su privs.

Offline

#10 2016-10-28 07:58:08

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

seth wrote:
echo '\_SB.PCI0.P0P2.DGPU._OFF' | sudo tee /proc/acpi/call

The problem is that you'll be executing "echo" as root (yeah! - but useless) while the redirection (">") which would require root privs still runs as your ordinary user.

So you either wrap that entire call into a script and sudo the script or you ensure the writing process (tee is a T-pipe) has su privs.

Heh, of course wink thank you. I'll report what worked for me in the end.

Last edited by Malikos (2016-10-28 07:58:34)

Offline

#11 2016-10-28 12:59:25

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

Okay, now I have a problem.

I have an executable script:

 /usr/local/sbin/disable-dGPU 

which I added to sudoers. All is good, it works without password.

The problem is "sudo" which must be inside my disable-dGPU script needs CLI even if I don't need to provide a password, so:

- systemd --user fails due to sudo: no tty present and no askpass program specified
- main systemd works but crashes sddm and later the whole system (so gpu must be disabled later than graphical.target)
- placing symlinks or scripts in plasma autostart also fails, but I don't know why (tested with touch dummy and it works, disable-dGPU doesn't), probably the same problem as with systemd --user

So, is there anything left? It's so frustrating.

Last edited by Malikos (2016-10-28 13:19:39)

Offline

#12 2016-10-28 13:49:03

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 68,317

Re: [SOLVED] How to run command as root after X starts

"sudo -S" will likely avoid the tty issue, but it sounds as if you actually want a systemd service which "Wants" the graphical target (resp. is scheduled to run "After" it?)

Offline

#13 2016-10-28 13:54:44

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

seth wrote:

"sudo -S" will likely avoid the tty issue, but it sounds as if you actually want a systemd service which "Wants" the graphical target (resp. is scheduled to run "After" it?)

No no, don't be concerned with the tty, I just want systemd to run after graphical target, user service will acomplish this.

So I added -S and now it's authentication failure, don't know why because the script is in sudoers and calling it from terminal does not ask for password...

systemctl --user status disable-dGPU says:
authentication failure and then it asks for password with [sudo] password for.... Sorry, try again.

But basically yes, root systemd service should also work but only when it runs after graphical target. The best would be to run it after login (exiting sddm and entering plasma).

Last edited by Malikos (2016-10-28 14:43:35)

Offline

#14 2016-10-28 14:47:48

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 24,759

Re: [SOLVED] How to run command as root after X starts

From

man sudo wrote:

-n, --non-interactive
    Avoid prompting the user for input of any kind. If a password is required for the command to run, sudo will display an error message and exit.

...

-s, --shell
    Run the shell specified by the SHELL environment variable if it is set or the shell specified by the invoking user's password database entry. If a command is specified, it is passed to the shell for execution via the shell's -c option. If no command is specified, an interactive shell is executed.

I'd figure one of these should help

Offline

#15 2016-10-28 14:51:18

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 68,317

Re: [SOLVED] How to run command as root after X starts

Alternatively, put the script into ~/.config/plasma-workspace/env/

Offline

#16 2016-10-28 15:25:25

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

Unfortunately:

- "-s" still shows "no tty preset...." error
- "-n" shows  "a password is required"
- placing in plasma-workspace/env doesn't work. Other scripts work, but not this.

Offline

#17 2016-10-28 16:14:32

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: [SOLVED] How to run command as root after X starts

Can you share the sudoers entry and the exact error you see?


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#18 2016-10-28 16:21:22

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

sudoers:

konrad ALL=(ALL) NOPASSWD: /usr/local/sbin/disable-dGPU, /usr/local/sbin/enable-dGPU

sudo -n error:

Oct 28 18:16:55 konrad-vaio disable-dGPU[2114]: sudo: a password is required
Oct 28 18:16:55 konrad-vaio systemd[563]: disable-dGPU.service: Main process exited, code=exited, status=1/FAILURE
Oct 28 18:16:55 konrad-vaio systemd[563]: disable-dGPU.service: Unit entered failed state.
Oct 28 18:16:55 konrad-vaio systemd[563]: disable-dGPU.service: Failed with result 'exit-code'.

sudo -s error:

Oct 28 18:18:28 konrad-vaio disable-dGPU[2804]: sudo: no tty present and no askpass program specified                                                                                                                              
Oct 28 18:18:28 konrad-vaio systemd[563]: disable-dGPU.service: Main process exited, code=exited, status=1/FAILURE                                                                                                                 
Oct 28 18:18:28 konrad-vaio systemd[563]: disable-dGPU.service: Unit entered failed state.
Oct 28 18:18:28 konrad-vaio systemd[563]: disable-dGPU.service: Failed with result 'exit-code'.

service:

[Unit]
Description= test

[Service]
ExecStart=/usr/local/sbin/disable-dGPU

[Install]
WantedBy=default.target

script:

#!/bin/bash
echo '\_SB.PCI0.P0P2.DGPU._OFF' | sudo -n tee /proc/acpi/call

Offline

#19 2016-10-28 16:30:14

loqs
Member
Registered: 2014-03-06
Posts: 18,603

Re: [SOLVED] How to run command as root after X starts

The sudo entry is for

/usr/local/sbin/disable-dGPU
/usr/local/sbin/enable-dGPU

The service file itself does not invoke sudo when calling the script
The script calls sudo for the binary tee

Offline

#20 2016-10-28 16:47:01

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED] How to run command as root after X starts

afaik unless you workaround it, all systemd units run as root, so you can take out the sudo call from your script, and it should work properly

Offline

#21 2016-10-28 16:51:25

loqs
Member
Registered: 2014-03-06
Posts: 18,603

Re: [SOLVED] How to run command as root after X starts

Malikos wrote:

I just want systemd to run after graphical target, user service will acomplish this.

Offline

#22 2016-10-28 18:27:23

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 68,317

Re: [SOLVED] How to run command as root after X starts

@Malikos, adding "tee" to the NOPASSWD commands is the by *far* wrong answer ;-P
(Seriously, major security break)

Offline

#23 2016-10-29 15:07:24

Malikos
Member
Registered: 2015-12-12
Posts: 46

Re: [SOLVED] How to run command as root after X starts

seth wrote:

@Malikos, adding "tee" to the NOPASSWD commands is the by *far* wrong answer ;-P
(Seriously, major security break)

I don't think that I gave NOPASSWD sudo to "tee", I just gave it to this one particular script.

Nevertheless, I'm marking this thread as solved.
Solution is not to do --user service but do a standard system-wide service as root, and set WantedBy=graphical.target or default.target.
Thanks to everybody for help.


However, this does not work in my case, due to my hardware. In order to work I had to "hack" the BIOS. Explanation below:

This case is for Sony Vaio VPCZ1 (vpc-z12v9e).

My computer is trying to access GPU when it's off and system hangs. This is caused by rather unpredictible behavior of "Dynamic Hybrid Graphics System", which is pre-optimus, but is also NOT a standard "change switch to speed and reboot to have nVidia enabled". It works dynamically thanks to windows 7 specific software. On linux, the second GPU is always on, but not always used (it is used radomnly from my experience).

Long story short: tried everthing, didn't work:
- bbswitch won't work (unexpected behavior, system hangs, "segmentation fault" in /proc/acpi/bbswitch, so bumblebee is also not an option
- acpi calls work, but only after successful login, however system even then can hang radomnly during usage which was really driving me crazy
- systemd acpi calls won't work due to pre-boot hangs

Solution:
THIS: http://forum.notebookreview.com/threads … os.473226/
Update bios to the "hacked" one, then you can set in BIOS setting "vga switching policy to STATIC", then simply switch to Speed (nvidia) mode and reboot for nvidia, or switch to Stamina (intel) and reboot for intel. It works, does not hang and I think that even HDMI will work without problems, but I have yet to check that.

If somebody needs instructions, files for bios flashing etc. you can write to me, I have a backup on disk because they are not accessible from the links provided on the site above.

Last edited by Malikos (2016-10-29 15:10:36)

Offline

Board footer

Powered by FluxBB