Need to run command at startup

Hi @destructa,

Yes, to both your questions. But I’m wondering if the second one is necessary.

I’m recommend creating a script that contains the commands, then creating a systemd unit file to execute the script on startup. And AFAIK the systemd units run as root, so sudo shouldn’t be necessary.

First, let’s create the script to execute on startup, I’ll call it set-fans.sh but feel free to adapt it as wished:

sudo touch /usr/local/set-fans.sh

And let’s make it executable:

chown +x /usr/local/set-fans.sh

Now, let’s open it with nano

nano /usr/local/set-fans.sh

and insert the required commands:

#!/usr/bin/env bash

opencorsairlink --device 1 --fan channel=0,pwm=60
opencorsairlink --device 1 --fan channel=1,pwm=60
opencorsairlink --device 1 --fan channel=2,pwm=60
opencorsairlink --device 1 --fan channel=3,pwm=60
opencorsairlink --device 1 --fan channel=4,pwm=60
opencorsairlink --device 1 --fan channel=5,pwm=60

(Feel free to simply copy-and-paste it yourself.)

When done save, Ctrl+S, and exit, Ctrl+X nano.

Then it’s best to test the script, so reboot, and when you’d normally execute those commands, run the script:

sudo ./usr/local/set-fans.sh

If it works satisfactorily, we can create the systemd unit.

To setup a systemd unit, we’ve got to create a file for it:

nano /etc/systemd/system/set-fans.service

(Again, I’ve called it set-fans.service but you’re welcome to adjust it, as long as you keep the .service extension.)

In nano editor paste the following text:

[Unit]
Description=Set fans
#After=network.target

[Service]
Type=oneshot
ExecStart=/usr/local/set-fans.sh
RemainAfterExit=true
StandardOutput=journal

[Install]
WantedBy=multi-user.target

Note that we must specify RemainAfterExit=true so that systemd considers the service as active after the setup action is successfully finished.

When done save, Ctrl+S, and exit, <Ctrl+X nano. Now we should test and enable the unit to start at boot:

sudo systemctl enable --now set-fans.service

If you’ve got no errors, congratulations. Everything should work if you reboot now. So go ahead and do exactly that. Report back.

3 Likes