Need to run command at startup

I am a bit of a newbie at Linux. I know the basics, but still learning. My system has the Corsair Commander Pro, which is configured in Windows 11, but on logging into Manjaro KDE the fans are running at the incorrect speeds (they need to be lower than Windows as the system doesn’t work as hard in Linux and the temps are generally better).

Running Kernel 5.16.18-1.

I am using OpenCorsairLink to control my system fans.

This is what I currently have to paste into terminal every time after booting into Linux having used Windows 11.

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

Is there any way to make this happen automatically on on Startup?

Also is there a way to run these commands with out sudo first?

Help will be greatly appreciated.

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

Thanks.

I am away from home until the weekend. I’ll give this a go then and report back.

Appreciate the response.

Hello there,

I was wondering if I can ask you a couple questions about your approach to this problem?

Thank you!

Hey!

You are most welcome, but do it in a PM, not the main forum.

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