Must create a service to run sudo command at start-up?

I wanted to disable mouse wake-up from sleep. The command did not survive reboot, and the author told me to use rc.local, which does not seem to work in EOS. An Arch BBS post said to create a service, but do I really have to create a service for that? Isn’t there any already-existing place where I can add a sudo command?

You might want to see this: https://wiki.archlinux.org/title/Power_management/Wakeup_triggers#Event-driven_with_udev

This is the way I disabled the mouse waking up the machine.
And that used to work well for a long time, but some recent update (maybe about two months ago) broke it.
So far I haven’t investigated it further, as this fortunately is no big deal for me.

So far in my analysis I think some systemd update has changed something to cause this issue. Now it looks like systemd no more triggers the mouse adding event at boot time.

I circumvented this by creating a simple systemd service that runs at system startup. The service essentially writes word disabled into file

/sys/bus/usb/devices/$id/power/wakeup

You just need to find the value for variable $id with the help of this article: https://wiki.archlinux.org/title/Udev#Waking_from_suspend_with_USB_device

That was the command I had used. And it needs to be executed with sudo each time the PC restarts. That is why I was looking for ways to to run sudo commands at start-up, and I wondered if there is an easier way than creating a service.

A simple service is not that hard. And once you know how to create one, you will know how to deal with possible other similar needs.

For example, the service file:

[Unit]
Description=Disable mouse wakeup

[Service]
Type=oneshot
ExecStart=/usr/bin/mouse-wakeup-disable

and the timer file:

[Unit]
Description=Disable mouse wakeup

[Timer]
OnStartupSec=30sec

[Install]
WantedBy=timers.target

and the executable /usr/bin/mouse-wakeup-disable:

#!/bin/bash

Main() {
    local ids=(1-1.2.1   2-1.2.1)   # these depend on your system!
    local id
    local file

    for id in "${ids[@]}" ; do
        file=/sys/bus/usb/devices/$id/power/wakeup
        if [ -r $file ] ; then
            echo "disabled" > $file
        fi
    done
}

Main "$@"

1 Like

A system service is always running as root, so no need of sudo in the script.

For startup, and especially for power events (sleep, suspend, hibernate, etc.) a service is the easiest, safest, appropriate method. It just needs to start reading, to understand how it works. Then you will know and use for many other cases in the future (it is worth learning it). :wink:

How about crontab:

@reboot device=$(/usr/bin/grep c52b /sys/bus/usb/devices/*/idProduct | /usr/bin/sed "s|/idProd.*||");/usr/bin/echo disabled >$device/power/wakeup

but a service as Manuel and petsam says, is properly the better way to go…

1 Like

What is not working? I am using the rc.local service myself and it works just fine.

Nice lookup method! Thanks for sharing. :+1:

It is not VERY difficult to create a service, but it is cumbersome to do that just to run one line of command, so I wondered if there is an easier way in EOS.

If service is the only way, I will do that.

I don’t know. It does not work. I searched the web, and the result said that it is a legacy script.

You have package rc-local installed?

If yes, you need to create a file /etc/rc.local and put all your commands in it.
Make the file executable: sudo chmod +x /etc/rc.local
You need to enable the service: systemctl enable rc-local
Next boot it should be executed.

PS
Before you reboot I suggest you execute /etc/rc.local in a terminal to see that it works properly.

1 Like

Well, you could just create one system service file to call a startup script file. Then just keep placing more commands in the same script file instead of making a new service file for every command?

This is exactly the idea of the rc-local service.

1 Like

I searched and what you mentioned seems to be an AUR package that creates the service automatically. No, it was not installed. I guess I can use this instead of manually creating the service.

No, the original idea of rc-local service was to keep using a former init script (/etc/rc.local) from the former init system (SysV) in the new one (systemd).

You could create a service that start on boot, which execute a script containing all the commands you want, that would be enough.

1 Like

In the former init system /etc/rc.local had the same purpose as today with the rc-local service. There is no difference. It gives you a place where you can put all your individual commands which are then executed at boot time.

# /usr/lib/systemd/system/rc-local.service
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
Wants=network-online.target
After=network-online.target multi-user.target

[Service]
Type=oneshot
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

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