TransWikia.com

Systemd service does not save changes to disk

Unix & Linux Asked by Ken Lin on November 26, 2021

Wrote a simple keychron.service file to overwrite a parameter during each boot-up.

[Unit]                                                                                                                                                                                                    
Description=The command to make the Keychron K2 work                            
                                                                            
[Service]                                                                       
Type=oneshot                                                                    
ExecStart=/bin/echo 0 | sudo /usr/bin/tee /sys/module/hid_apple/parameters/fnmode
                                                                               
[Install]                                                                       
WantedBy=multi-user.target

This script is for patching my keyboard, and it simply writes a 0 into /sys/module/hid_apple/parameters/fnmode. Unfortunately I can’t get it to work. Here’s what I did in an attempt to troubleshoot it.

  1. Check that fnmode is 1
thekenu:~
$ cat /sys/module/hid_apple/parameters/fnmode
1
  1. Restart keychron.service, and use systemctl status to ensure it restarted successfully
thekenu:~
$ sudo systemctl restart keychron.service
thekenu:~
$ systemctl status keychron.service
● keychron.service - The command to make the Keychron K2 work
   Loaded: loaded (/etc/systemd/system/keychron.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Wed 2020-07-22 06:33:13 PDT; 3s ago
  Process: 28778 ExecStart=/bin/echo 0 | sudo /usr/bin/tee /sys/module/hid_apple/parameters/fnmode (code=exited, status=0/SUCCESS)
 Main PID: 28778 (code=exited, status=0/SUCCESS)

Jul 22 06:33:13 thekenu-XPS-15-9560 systemd[1]: Starting The command to make the Keychron K2 work...
Jul 22 06:33:13 thekenu-XPS-15-9560 echo[28778]: 0 | sudo /usr/bin/tee /sys/module/hid_apple/parameters/fnmode
Jul 22 06:33:13 thekenu-XPS-15-9560 systemd[1]: Started The command to make the Keychron K2 work.
  1. Expected fnmode to be 0, but it is still 1
thekenu:~
$ cat /sys/module/hid_apple/parameters/fnmode
1
  1. Run the ExecStart command from keychron.service
thekenu:~
$ /bin/echo 0 | sudo /usr/bin/tee /sys/module/hid_apple/parameters/fnmode
0
  1. Now fnmode is 0. But it seems like I merely typed out what keychrone.service was supposed to do.
thekenu:~
$ cat /sys/module/hid_apple/parameters/fnmode
0

One Answer

ExecStart is not a normal shell line. From systemd.service(5):

ExecStart=

Commands with their arguments that are executed when this service is started. The value is split into zero or more command lines according to the rules described below (see section "Command Lines" below).

You're operating a system unit, not a user unit, so you're already root and don't need to complicate the Exec with sudo:

ExecStart=/bin/bash -c "echo 0 > /sys/module/hid_apple/parameters/fnmode"

Answered by eleventyone on November 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP