How to automatically execute sudo command after logged in?

Hello everyone,
As I mentioned in the title, how can I automatically run the code below?
echo PTXH | sudo tee /proc/acpi/wakeup

Click for details:

What is your issue?
What is it that you want to achieve?

… running the command …
which isn’t well defined
(syntax - you tried to write something to /proc/acpi/wakeup)

isn’t going to achieve anything

this is what /proc/acpi/wakeup looks like on my system

cat /proc/acpi/wakeup 
Device	S-state	  Status   Sysfs node
P0P1	  S4	*disabled
USB1	  S3	*disabled
USB2	  S3	*disabled
USB3	  S3	*disabled
USB4	  S3	*disabled
USB5	  S3	*disabled
USB6	  S3	*disabled
USB7	  S3	*disabled
RP01	  S4	*disabled  pci:0000:00:1c.0
PXSX	  S3	*disabled  pci:0000:02:00.0
RP02	  S4	*disabled  pci:0000:00:1c.1
PXSX	  S3	*disabled  pci:0000:03:00.0
RP03	  S4	*disabled  pci:0000:00:1c.2
PXSX	  S3	*disabled  pci:0000:04:00.0
PEG0	  S3	*disabled  pci:0000:00:01.0
PEGP	  S3	*disabled  pci:0000:01:00.0
PEGA	  S4	*disabled
GLAN	  S3	*disabled
EHC1	  S3	*enabled   pci:0000:00:1d.0
EHC2	  S3	*enabled   pci:0000:00:1a.0
XHC	  S3	*enabled   pci:0000:00:14.0
HDEF	  S3	*disabled  pci:0000:00:1b.0
PWRB	  S4	*enabled   platform:PNP0C0C:00

What are you trying to achieve by writing PTXH to it?

a usual and nice way is to implement this as system-service. this is the way to activate something that needs sudo-rights and cannot run as autostart-script

1 Like

Sorry and thanks for the warning.

Let me explain in detail.

I recently purchased the Logitech MX Keys Mini Keyboard and Signature M650L Mouse.
They are connected to my computer via Logitech USB Bolt Receiver.

I have a problem that I partially solved in the previous issue;
When I put my computer to sleep, it wakes up immediately.

I’ve added a file named disable-usb-wake.conf to /etc/tmpfiles.d.
The contents of the file are;
w /proc/acpi/wakeup - - - - PTXH
Detailed steps are here.
“PTXH” is USB Bolt Receiver or entire USB…
Anyways.

When PTXH is off, the computer may go to sleep and not wake up by itself. But I can’t wake it up with keyboard and mouse.
echo PTXH | sudo tee /proc/acpi/wakeup When I enter the command in terminal, PTXH is active again. I can put my computer to sleep and it doesn’t wake up by itself. I can wake it up with keyboard and mouse.
But every time I restart the computer I have to enter the command again.

Thank you for your interest. Let me look into your answer to the other thread.

in case you didn’t notice:
echo PTXH | How can I run sudo tee /proc/acpi/wakeup
is not a command

… I’ll have a look at your other thread …

1 Like

You are right.
I mentioned at the beginning of the other topic that I am new.
Thank you for your time.

If you cant get the service to run you can specify a script to run with sudo and no password. Let me be perfectly honest, you have to be very selective in what scripts you want to run this way. You should also limit who can run the file this way to only you.
Open a terminal and enter sudo visudo This will open the /etc/suders file for editing.
At the end of the file enter this line, modifying the username to your username and path to the file to run.
username ALL=(ALL) NOPASSWD: /path/to/script/to/run
To get the script to run automatically on login create a launcher in /etc/xdg/autostart that file should include

[Desktop Entry]
Comment=Whatever you want here
Exec=sudo /path/to/the/script
Name=Whatever you want here
Type=Application

Edit “Whatever you want here” to anything you want. Edit /path/to/the/script to the path to the script.
The script will then run on login.

2 Likes

Thanky you!!!

Your welcome. Its a real old school way of doing it from my pre systemd days.

1 Like

I don’t understand why you need to echo it manually if you already use tmpfiles.d to write into wakeup file.

I don’t understand too.
Can you take a look at the first thread?

When I turn off the USB with tmpfiles.d, the computer does not wake up by itself.
But, this time, I can’t wake it from sleep with keyboard and/or mouse.
After logging in, when I run the echo command in the terminal with my hand, when the computer goes to sleep, it does not wake up by itself.
And I can also wake it up with keyboard and mouse.
As I mentioned in the other thread, I created a service under /etc/systemd/system/, but no solution.

And now, I applied Jim.B’s solution.
This is very strange. It didn’t work too.
I added a 30 second delay to the bash script. It automatically run after i logged in, and enabled the “disabled USB” after 30 seconds…
I’m observing this. The code works automatically. But when I put the computer to sleep, it wakes up immediately.
Perhaps giving up is the healthiest thing to do. When I run the code by hand, I get what I want.

Reboot and post output of:

cat /proc/acpi/wakeup

And then do your command, and post same output again.


If you did it the right way, it should work just the same as typing it by hand. So you probably messed something up.

cat /proc/acpi/wakeup after reboot:

Device	S-state	  Status   Sysfs node
GP12	  S4	*enabled   pci:0000:00:07.1
GP13	  S4	*enabled   pci:0000:00:08.1
XHC0	  S4	*enabled   pci:0000:09:00.3
GP30	  S4	*disabled
GP31	  S4	*disabled
PS2K	  S3	*disabled
PS2M	  S3	*disabled
X161	  S4	*disabled  pci:0000:07:00.0
X162	  S4	*disabled
PTXH	  S4	*disabled  pci:0000:01:00.0
X1_1	  S4	*disabled
X1_2	  S4	*disabled
WIFI	  S4	*disabled  pci:0000:05:00.0
I225	  S4	*enabled   pci:0000:06:00.0
X163	  S4	*disabled
M2_2	  S4	*disabled  pci:0000:04:00.0

this is tmpfiles.d file:
cat /etc/tmpfiles.d/disable-usb-wake.conf
w /proc/acpi/wakeup - - - - PTXH

This is bash script in /etc/xdg/autostart/:
cat XXX.sh

#!/bin/bash
echo PTXH | tee /proc/acpi/wakeup

This is .desktop file /etc/xdg/autostart/:
cat XXX.desktop

[Desktop Entry]
Comment=Enable PTXH (For Logitech Bolt USB Receiver) after logged in.
Exec=sudo /etc/xdg/autostart/XXX.sh
Name=WakeUp
Type=Application

File details in /etc/xdg/autostart/:
ll | grep XXX

-rw-r--r-- 1 root root   154 Aug 20 22:57 XXX.desktop
-rwxr-xr-x 1 root root    46 Aug 20 22:58 XXX.sh

Last one:
sudo visudo

...
...
myusername ALL=(ALL) NOPASSWD: /etc/xdg/autostart/XXX.sh

And now applied Jim’s solution and reboot again;
cat /proc/acpi/wakeup

Device	S-state	  Status   Sysfs node
GP12	  S4	*enabled   pci:0000:00:07.1
GP13	  S4	*enabled   pci:0000:00:08.1
XHC0	  S4	*enabled   pci:0000:09:00.3
GP30	  S4	*disabled
GP31	  S4	*disabled
PS2K	  S3	*disabled
PS2M	  S3	*disabled
X161	  S4	*disabled  pci:0000:07:00.0
X162	  S4	*disabled
PTXH	  S4	*enabled   pci:0000:01:00.0
X1_1	  S4	*disabled
X1_2	  S4	*disabled
WIFI	  S4	*disabled  pci:0000:05:00.0
I225	  S4	*enabled   pci:0000:06:00.0
X163	  S4	*disabled
M2_2	  S4	*disabled  pci:0000:04:00.0

I still don’t get it; what state do you want PTXH to be in? Enabled or disabled?

Echoing device name into /proc/acpi/wakeup just toggles its enabled/disabled status. So in your case it should be enabled by default, and then you disable it with writing PTXH with help of tmpfiles.d, and then you enable it again with autostart script and it’s back where you started … :person_shrugging:

Also, take a look at: Wakeup triggers - ArchWiki

I solved the question I mentioned in the title with Jim.B’s method. Also, it can be done by creating a service. However, the viable solution for me is Jim.B’s method.

Actually, I don’t want to enable or disable it.

After that, it’s another issue. I’m wrong for putting it here.

If PTXH is enabled, the computer wakes up just as it went into sleep mode.

If PTXH is disabled, the problem of immediately waking the computer from sleep is resolved. However, I can’t wake it up with keyboard and/or mouse.

If PTXH is disabled while the computer is booting and, after user login, when I enable PTXH with my hand in Terminal, the computer wakes up immediately from sleep mode is resolved. I can also wake it up with the keyboard and/or mouse.

This method is confusing, and most likely wrong. However, I was able to solve the problem I mentioned in this way by going around behind something.

When I want to have the echo command done automatically, for some reason it doesn’t work as I want it to. The code works automatically, but the result is different…

Anyway, I leave it.
Thank you everyone.

One thing I have learned in my almost 17 years of running Linux is there is usually more than one way to accomplish something. As long as the desired result is accomplished, its all good. I can understand wanting to know the “why’s” but at some point it becomes an off topic mess if you are questioning someone who found a way that works but really doesnt have the fine details of why it works. A lot of people get good results by trying something they are not 100% sure of to see if it works.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.