Running a command at startup

1

1

How would I go about running a command at startup in ArchLinux using Systemd like rc.local in Sysv?

Jordan Doyle

Posted 2013-12-24T01:01:21.707

Reputation: 127

Answers

7

Depends on the command. For the most common cases, you don't need a command.

  • If you want to adjust a sysctl (a file in /proc/sys), those can be configured in /etc/sysctl.d/*.conf (generally 99-sysctl.conf or 99-local.conf; manual page):

    kernel.sysrq = 1
    kernel.pid_max = 4194304
    
  • If it's a module parameter under /sys/modules, it should be set when the module is first loaded, in /etc/modprobe.d/*.conf (generally modprobe.conf; see manual page):

    options kafs rootcell=stacken.kth.se
    
  • If you want to write to a device parameter in /sys, or if you want to run a program to change the device's settings, write an udev rule that would do this when the device is plugged in and put it in /etc/udev/rules.d/*.rules. The manual page is udev(7), and you'll find udevadm info useful when trying to match the right device.

    ACTION=="add", SUBSYSTEM=="net", KERNEL=="eth*", \
        RUN+="/usr/bin/ethtool -s %k wol d"
    
    # This rule checks if a device has an attribute in its /sys subdir:
    ACTION=="add", \
        SUBSYSTEM=="scsi_host", \
        TEST=="link_power_management_policy", \
        ATTR{link_power_management_policy}="medium_power"
    
  • If you want to write to a file anywhere else, or create a file or directory, use /etc/tmpfiles.d (manual page).

  • If you want to load a module, put its name in a file in /etc/modules-load.d/*.conf (manual page).

  • Finally, if you want to run a general command or start a daemon, write a .service unit file (one of many manual pages). Put it in /etc/systemd/system/*.service, and use the many examples in /lib/systemd/system. It'll be managed through systemctl.

    A few things to note: the Type= parameter has to be set right (simple vs forking vs oneshot), and the ExecStart= parameter requires a simple command line and does not accept shell-like syntax (no >, no &&, no $(...) and so on, only simple $ENVVAR and %x.)

    It is possible to order services after a specific device appears, using After=name.device (e.g. After=sys-subsystem-net-devices-%i.device).

Both #archlinux and #systemd have their IRC channels on the freenode network.

user1686

Posted 2013-12-24T01:01:21.707

Reputation: 283 655

It's a daemon I want to run, I attempted creating a service file but it seemed to hang or something and would never run the command but it worked if I ran it manually. Thanks for the detailed response. – Jordan Doyle – 2013-12-24T02:17:22.760

@JordanDoyle: You might find this blog post useful. Pay also close attention to the Type= parameter in systemd.service(5). Also try to ask Arch's IRC channel or forums, as systemd units should be part of the package just like rc.d scripts once were.

– user1686 – 2013-12-24T02:51:58.040