Execute command upon reboot (systemd timer)

Long story short: I need to execute this command (sudo rmmod int3403_thermal) every time my system reboots. I’ve been told that the best way to do that is to build a .service and a .timer, so here they are:

[Edit: Following feedback from various sources, the algorithm is the following:

  • To execute the service upon reboot, a .timer file is not needed. A .service file such as the one below is all it takes, which should be placed in /etc/systemd/system/, and enabled using sudo systemctl enable rmmod.service. Upon reboot, the service will be executed automatically;
  • For services that need to be executed at a certain time interval after reboot, a timer is required–see below (time in seconds). The .timer file needs to also be placed in /etc/systemd/system/, and enabled using sudo systemctl enable rmmod.timer --now. No need to enable the service, just the timer;
  • In order to check whether these have been enabled, and the status in general, use systemctl is-enabled rmmod.service and systemctl status rmmod.service (same for the .timer file).

]

# rmmod.service

[Unit]
Description=run rmmod upon reboot

[Service]
Type=oneshot
ExecStart=/bin/bash -c "rmmod int3403_thermal"

[Install]
WantedBy=multi-user.target

and

# rmmod.timer

[Unit]
Description=Runs the rmmod service

[Timer]
OnBootSec=60
Unit=rmmod.service

[Install]
WantedBy=timers.target

By chatgpt

[Unit]
Description=My command

[Service]
Type=oneshot
ExecStart=/path/to/my/command

[Install]
WantedBy=multi-user.target

Thank you for the reply. I must be doing something else wrong, because what should be a trivial task has turned into a nightmare. Documentation about this on the Internet is an absolute disaster: Does the service need to be enabled, started, or both? Does the timer need to be enabled, started, or both? Does the service need to be made executable? And do you have to do a daemon-reload? If yes, when? It’s simply atrocious, what could be a very simple cronjob otherwise.

Could it be the ExecStart?

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/sh -c “rmmod psmouse && modprobe psmouse”

[Install]
WantedBy=multi-user.target

as in here mouse - Alternative to running rmmod and modprobe at every boot - Ask Ubuntu

1 Like

Thanks. I’ll give it a try. In the meantime, see what you can make of this below. Looks like if I run the service manually (systemctl enable rmmod.service --now), things work just fine, so I doubt there’s an issue with the ExecStart line.

dad@DadsGram~ $ systemctl status rmmod.service
× rmmod.service - run rmmod upon reboot
     Loaded: loaded (/etc/systemd/system/rmmod.service; enabled; preset: disabled)
     Active: failed (Result: exit-code) since Mon 2023-02-20 22:54:45 EST; 4min 29s ago
TriggeredBy: ● rmmod.timer
    Process: 417 ExecStart=/usr/bin/rmmod int3403_thermal (code=exited, status=1/FAILURE)
   Main PID: 417 (code=exited, status=1/FAILURE)

Feb 20 22:54:45 DadsGram systemd[1]: Starting rmmod.service...
Feb 20 22:54:45 DadsGram rmmod[417]: rmmod: ERROR: Module int3403_thermal is in use
Feb 20 22:54:45 DadsGram systemd[1]: rmmod.service: Main process exited, code=exited, status=1/FAILURE
Feb 20 22:54:45 DadsGram systemd[1]: rmmod.service: Failed with result 'exit-code'.
Feb 20 22:54:45 DadsGram systemd[1]: Failed to start rmmod.service.
dad@DadsGram~ $ systemctl enable rmmod.service --now
dad@DadsGram~ $ systemctl status rmmod.service
○ rmmod.service - run rmmod upon reboot
     Loaded: loaded (/etc/systemd/system/rmmod.service; enabled; preset: disabled)
     Active: inactive (dead) since Mon 2023-02-20 22:59:36 EST; 24s ago
TriggeredBy: ● rmmod.timer
    Process: 2348 ExecStart=/usr/bin/rmmod int3403_thermal (code=exited, status=0/SUCCESS)
   Main PID: 2348 (code=exited, status=0/SUCCESS)

Feb 20 22:59:36 DadsGram systemd[1]: Starting rmmod.service...
Feb 20 22:59:36 DadsGram systemd[1]: rmmod.service: Deactivated successfully.
Feb 20 22:59:36 DadsGram systemd[1]: Finished rmmod.service.
dad@DadsGram~ $

Apparently I might not even need a .timer file: It looks like enabling a service via systemctl enable actually enables that service to be executed at boot time.

Yes, you are right, it was the ExecStart line. Yes, it had to be prefixed as you said, by /bin/bash -c. Problem solved.

Did you try simply blacklisting the module?

sudo mkdir -p /etc/modprobe.d
echo "blacklist int3403_thermal" | sudo tee /etc/modprobe.d/int3403_thermal.conf
1 Like

Thanks, I actually had a friend suggest that to me yesterday, and I guess it’s the best solution in this specific scenario. Nevertheless, it doesn’t hurt to have a crystal clear algorithm for how to use systemd’s .service and .timer to use in more general cases.

Did some experiments with blacklisting the module vs. running the rmmod command, and it revealed some interesting facts. But first, here’s some background: my computer is an LGGram and, as such, it suffers form this bug. (And no, it’s not specific to Ubuntu: Clear Linux suffers from it too, so it’s likely a kernel matter.) Two of the most annoying issues about this bug are (a) the syslog spamming and (b) the high CPU temperature.

Running the rmmod command fixes (a), but not (b), while blacklisting the module fixes them both, though at the expense of very high CPU utilization(!) that renders the whole system pretty much unusable. At this point, it looks to me that running the rmmod command is the best compromise in this matter.