How to make this command run as root automatically at bootup?

Hi,

I have a new wireless keyboard and everytime I go to suspend/sleep mode, my PC immediately wakes up again. Obviously the keyboard is constantly sending commands to the USB receiver.

I found out how to disable it from waking my PC by disabling the USB port where the receiver is plugged in:

sudo echo "disabled" > /sys/bus/usb/devices/2-6/power/wakeup

Now I want this command to be executed automatically on bootup. I’ve tried several guides. For example by creating a systemd service unit. When I follow this* guide, everything seems to work. When I start the service manually (as mentioned at the end of the guide) by doing systemctl start disable-usb.service the USB port gets successfully disabled. But when I reboot it is enabled again…

What am I doing wrong? Or is there any other solution?

PS: I also tried this** method, didn’t work either (USB port was also not disabled after reboot).

*linuxconfig. org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux
**askubuntu. com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password

systemctl enable disable-usb.service --now
1 Like

I also did that (I followed all the steps from the guide). I then have to enter my pw and the port gets successfully disabled, however, when I reboot it is enabled again.

Share your .service file. Also refer to the first answer here.

I followed all these steps and didn’t receive any error message. My service looks like this:

[Unit]
Description=Disable-USB

[Service]
ExecStart=/usr/local/bin/.disable-usb.sh

[Install]
WantedBy=default.target

I also tried it with

[Install]
WantedBy=multi-user.target

share this too. I assume that your script is executable as it is starting on using systemctl start so no problem there.

#!/bin/sh

echo "disabled" > /sys/bus/usb/devices/2-6/power/wakeup

Yes, it is executable.

Try with this service:

[Unit]
Description=Disable-USB

[Service]
Type=oneshot
ExecStart=/usr/bin/echo 'disabled' > '/sys/bus/usb/devices/2-6/power/wakeup'

[Install]
WantedBy=multi-user.target

Doesn’t work :frowning:

When I do cat /sys/bus/usb/devices/2-6/power/wakeup after a reboot it still says “enabled”.

Try with:

sh -c "echo 'disabled' > /sys/bus/usb/devices/2-6/power/wakeup"

These services are executed in parallel. This means a different service could revert this change. For example tlp or something like this.
So you should make sure, this service is started very late in the boot process. You might want check the journal at which time your service is started and what happened after that.

You instruct systemd with the After= keyword in the unit section of your service to start this service after other services.

1 Like

Also not working :confused:

I guess thats it. When I type journalctl -u disable-usb I see lots of the following entries:

-- Boot 02a639134a124c11834a7ff3daccfdcc --
Jan 26 07:00:52 test-s6 systemd[1]: Started Disable-USB.
Jan 26 07:00:52 test-s6 disable-usb.sh[651]: /usr/local/bin/disable-usb.sh: line 3: /sys/bus/usb/devices/2-6/power/wakeup: No such file or d>
Jan 26 07:00:52 test-s6 systemd[1]: disable-usb.service: Main process exited, code=exited, status=1/FAILURE
Jan 26 07:00:52 test-s6 systemd[1]: disable-usb.service: Failed with result 'exit-code'.

When I start the service manually I get

Jan 26 09:22:21 test-s6 systemd[1]: Starting Disable-USB...
Jan 26 09:22:21 test-s6 systemd[1]: disable-usb.service: Deactivated successfully.
Jan 26 09:22:21 test-s6 systemd[1]: Finished Disable-USB

It looks like the service runs too early that the folder is not even mounted yet.

How can I make it start last? How do I find out which was the last service started?

This depends on your system. Network stuff is usually late or your display manager. But you can use systemd-analyze to get a better idea.

For example

systemd-analyze plot > ~/plot.svg

Then open the SVG image. Sometimes a normal image viewer can’t open it, a browser usually works.

1 Like

@cizz I dont think that a service is good solution. Use instead a udev rule: https://wiki.archlinux.org/title/Udev#Waking_from_suspend_with_USB_device

Instead of enable, use disable in that example.

According to this, lightdm.service is started last on my system. So I added the line
After=lightdm.service
in my disable-usb.service file in the [unit] section. However, when I restart and do systemd-analyze plot > ~/plot2.svg, the service is still started in the same position as before and not at the end.

I followed the guide from your link. Since I didn’t have a file called 50-wake-on-device.rules in the folder /etc/udev/rules.d/, I created it and put the following into it:

ACTION=="add", SUBSYSTEM=="usb", DRIVERS=="usb", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c368", ATTR{power/wakeup}="disabled", ATTR{driver/2-6/power/wakeup}="disabled"

But that didn’t work either. Or would I have to do anything else except just create this file!?

Thanks for your effort already guys! @ishaan2479 :heart: @xabbu :heart: @megavolt :heart:

To trigger the new rule, run this:

sudo udevadm control --reload-rules

and check it like that:

cat /sys/bus/usb/devices/usb*/power/wakeup

I think something is not entirely correct in this approach. If I run

sudo udevadm control --reload-rules

and then

cat /sys/bus/usb/devices/usb*/power/wakeup

Everything is disabled. However,

cat /sys/bus/usb/devices/2-6/power/wakeup

still returns enabled and the issue of an immediate wakeup still exists. So it obviously didn’t effect the 2-6 resource. I think I would have to edit the SUBSYSTEM=="usb", DRIVERS=="usb" part somehow…?

Maybe try that:

ACTION=="add|change", SUBSYSTEM=="usb", DRIVERS=="usb", ATTR{power/wakeup}="disabled"

This should disable it on any usb port.

1 Like

Unfortunately I still need to be able to wake it with my USB mouse :frowning:

So this is your mouse?