You are not logged in.

#1 2018-03-09 18:47:40

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Wrote a script to turn off my RX460 fans - systemd service doesnt work

I have a RX460 and I'm using the AMDGPU driver. I use GNOME3 as my DE.

I wrote a Python script which acts as a fancontrol for my RX460. I want it to be 0 RPM when I'm doing desktop work and the slowest possible speed for when I'm gaming. The script is pretty simple. It gets the GPUs temperature and then writes a "0" to /sys/class/drm/card0/device/hwmon/hwmon3/pwm1. (Or other values >0 depending on the temperature.)

Now when I start the script manually it works just fine. However when I make a systemd service for it and enable it, it doesn't do anything.

There's no error message and the journalctl logs don't show anything that looks suspicious.

Does anyone have any idea why this isn't working?
Is there a way to define the order of systemd services? I want my python script to be the very last service that gets started, so I can make sure amdgpu or any other driver/tool that might interfere with my script doesn't get in the way.

Thanks a lot in advance!!

Last edited by ArchArrow (2018-03-09 18:48:42)

Offline

#2 2018-03-09 19:11:55

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

Can you please post the script? And how you installed / enabled the service?

Offline

#3 2018-03-09 19:18:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

ArchArrow wrote:

However when I make a systemd service for it and enable it, it doesn't do anything.

Did you also start the service, or did you reboot?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2018-03-09 19:59:37

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,739

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

Moving to Newbie Corner.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2018-03-09 22:01:18

loqs
Member
Registered: 2014-03-06
Posts: 17,192

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

man 7 systemd.special defines various targets used by systemd.  For dependency handling see Systemd#Handling_dependencies as well as man 5 systemd.unit.

Offline

#6 2018-03-13 13:41:33

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

Yes, I can start the systemd service manually once I'm logged into Gnome3. And yes I enabled the service. Yet it still doesn't load. How can I make this service load as the very last service? Is there a way to specify this?

Another cause of error could be my service file:

[Unit]
Description=gpud service

[Service]
Type=idle
ExecStart=/usr/bin/python /home/user/gpud.py

[Install]
WantedBy=graphical-target.target

Offline

#7 2018-03-13 13:50:23

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

There's absolutely nothing in there about unit dependencies or timing.  Read loqs link and at very least, add an After= field.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2018-03-13 18:40:59

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

I added "After=gdm.service" and "Wants=gdm.service" to the Unit section and rebooted, but nothing changed. It still doesn't start sucessfully. Yet it still works just fine if I start it manually.

Does anyone have any idea why this is?

Last edited by ArchArrow (2018-03-13 18:41:44)

Offline

#9 2018-03-13 18:59:41

loqs
Member
Registered: 2014-03-06
Posts: 17,192

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

drcouzelis wrote:

Can you please post the script? And how you installed / enabled the service?

[Install]
WantedBy=graphical-target.target

graphical-target.target should be graphical.target.  Please also post the status of the service and journalctl output for the service.

Offline

#10 2018-03-13 20:16:28

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

OK I changed it to graphical.target. Yet still no automatic startup. It still works just fine when starting it manually.

This is the journalctl output after a fresh reboot (before starting the service manually):

● gpud.service - gpud service
   Loaded: loaded (/etc/systemd/system/gpud.service; enabled; vendor preset: disabled)
   Active: inactive (dead)

This is my current service file:

[Unit]
Description=gpud service
After=gdm.service
Wants=gdm.service

[Service]
ExecStart=/usr/bin/python /home/user/gpud.py

[Install]
WantedBy=graphical.target

As previously stated, the script and service runs just fine when I start it manually. I tried making a *.desktop file so I can autostart it when logging into Gnome, but that asks me for my sudo password every single time after login, so it is less then ideal. I cannot imagine that this cannot be done with systemd. Does anyone have any idea how to solve this? My corrected service file should suffice, yet it still isn't starting the service. WHY?

Offline

#11 2018-03-13 20:26:16

loqs
Member
Registered: 2014-03-06
Posts: 17,192

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

ArchArrow wrote:

This is the journalctl output after a fresh reboot (before starting the service manually):

● gpud.service - gpud service
   Loaded: loaded (/etc/systemd/system/gpud.service; enabled; vendor preset: disabled)
   Active: inactive (dead)

That is the output of systemctl status not the journal output for the unit gpud.service
What is the output of

ls /etc/systemd/system/graphical.target.wants/
systemctl status gdm.service

Offline

#12 2018-03-15 10:41:37

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

No problem, here are your infos:

sudo journalctl -u gpud
Mär 13 21:04:14 archlinux systemd[1]: Stopping gpud service...
Mär 13 21:04:14 archlinux systemd[1]: Stopped gpud service.
-- Reboot --
Mär 14 11:53:10 archlinux systemd[1]: Started gpud service.
Mär 14 13:45:50 archlinux systemd[1]: Stopping gpud service...
Mär 14 13:45:50 archlinux systemd[1]: Stopped gpud service.
-- Reboot --
Mär 15 00:58:38 archlinux systemd[1]: Started gpud service.
Mär 15 02:09:27 archlinux systemd[1]: Stopping gpud service...
Mär 15 02:09:27 archlinux systemd[1]: Stopped gpud service.
$ ls /etc/systemd/system/graphical-target.target.wants/
gpud.service
$ sudo systemctl status gpud
● gpud.service - gpud service
   Loaded: loaded (/etc/systemd/system/gpud.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
$ sudo systemctl start gpud && sudo systemctl status gpud
● gpud.service - gpud service
   Loaded: loaded (/etc/systemd/system/gpud.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-03-15 11:40:32 CET; 13ms ago
 Main PID: 4064 (python)
    Tasks: 1 (limit: 4915)
   Memory: 2.8M
   CGroup: /system.slice/gpud.service
           └─4064 /usr/bin/python /home/user/gpud.py

Mär 15 11:40:32 archlinux systemd[1]: Started gpud service.

So it looks like it should work. Somehow the service still isn't started though. Why?

Update: I added this to the top of my script:

print("This script is running")
sys.stdout.flush()

so that I can see if it is actually running. (flush is needed to "force" the stdout to journalctl)

But when I look at the journalctl output "This script is running" does not show up. When I start it manually and then check the journalctl output, the "This script is running" appears. So this must mean that the script isn't even starting. I don't understand how this can be? Python should be available during boot, so the scrpit should run just fine. but it isn't executed it seems?!

Last edited by ArchArrow (2018-03-15 11:47:42)

Offline

#13 2018-03-15 17:35:47

loqs
Member
Registered: 2014-03-06
Posts: 17,192

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

systemctl status gdm.service

Also try adding debug to the boot options and see if there is any extra output related to gpud.

Offline

#14 2018-03-20 11:50:24

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

$ sudo systemctl status gdm
● gdm.service - GNOME Display Manager
   Loaded: loaded (/usr/lib/systemd/system/gdm.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2018-03-20 11:55:00 CET; 53min ago
 Main PID: 484 (gdm)
    Tasks: 3 (limit: 4915)
   Memory: 9.0M
   CGroup: /system.slice/gdm.service
           └─484 /usr/bin/gdm

Mär 20 11:55:00 archlinux systemd[1]: Starting GNOME Display Manager...
Mär 20 11:55:00 archlinux systemd[1]: Started GNOME Display Manager.
Mär 20 11:55:01 archlinux gdm[484]: GdmDisplay: display lasted 0.393944 seconds
Mär 20 11:55:01 archlinux gdm[484]: Child process -522 was already dead.
Mär 20 11:55:01 archlinux gdm[484]: Child process 505 was already dead.
Mär 20 11:55:01 archlinux gdm[484]: Unable to kill session worker process

Looks alright to me. It is so weird that it isn't working. There isn't even any sort of error message in dmesgn or journalctl.

I will have to try your tip with the debug boot options. Will report back

Offline

#15 2018-03-20 12:03:09

seth
Member
Registered: 2012-09-03
Posts: 49,951

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

$ ls /etc/systemd/system/graphical-target.target.wants/
gpud.service

graphical-target.target should be graphical.target

Offline

#16 2018-03-23 12:02:10

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

seth wrote:

graphical-target.target should be graphical.target

OK we're getting there. This did indeed help. (It was already correct in my gpud.service file but I had to disable and re-enable the service.

So, the script is now called correctly, but it doesn't have access to the files it needs.

This is the systemctl status output of my gpud service:

Mär 23 12:54:37 archlinux python[455]: This script is running
Mär 23 12:54:37 archlinux python[455]: Cannot read fan1_input
Mär 23 12:54:37 archlinux python[455]: Cannot read temp1_input
Mär 23 12:54:37 archlinux python[455]: Cannot read pwm1

And this is my Python script (with some extra debugging lines):

import sys
import time

print("This script is running")
sys.stdout.flush()

while True:
	try:
		f = open("/sys/class/drm/card0/device/hwmon/hwmon1/fan1_input", "r")
		print("I CAN READ fan1_input")
		sys.stdout.flush()
	except:
		print("Cannot read fan1_input")
		sys.stdout.flush()
	try:
		t = open("/sys/class/drm/card0/device/hwmon/hwmon1/temp1_input", "r")
		print("I CAN READ temp1_input")
		sys.stdout.flush()
	except:
		print("Cannot read temp1_input")
		sys.stdout.flush()
	try:
		p = open("/sys/class/drm/card0/device/hwmon/hwmon1/pwm1", "r+")
		print("I CAN OPEN pwm1")
		sys.stdout.flush()
	except:
		print("Cannot read pwm1")
		sys.stdout.flush()

I also tried adding a PathExists line to the service file, but that doesn't seem to do anything.

[Unit]
Description=gpud service
After=gdm.service
Wants=gdm.service

[Service]
Type=simple
ExecStart=/usr/bin/python /home/user/gpud/gpud.py

[Path]
PathExists=/sys/class/drm/card0/device/hwmon/hwmon1/pwm1

[Install]
WantedBy=graphical.target


The code works perfectly fine when I start it manually while in Gnome. But for some reason when started as a systemd service the file(s) do(es)n't seem to exist yet.

Does anybody know why that is?

Last edited by ArchArrow (2018-03-23 12:05:42)

Offline

#17 2018-03-23 12:11:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

Checking that the path exists in the service file is the right way to go.  But where did you get that syntax for doing so?  There is no "PathExists" option, it's "ConditionPathExists" or "ConditionPathExistsGlob" and either of these must be in the "[Unit]" section, there is no "[Path]" section.  Please read `man systemd.unit` and/or the wiki page on writing service files.

Last edited by Trilby (2018-03-23 12:11:30)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#18 2018-03-23 12:53:03

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

Trilby wrote:

Checking that the path exists in the service file is the right way to go.  But where did you get that syntax for doing so?  There is no "PathExists" option, it's "ConditionPathExists" or "ConditionPathExistsGlob" and either of these must be in the "[Unit]" section, there is no "[Path]" section.  Please read `man systemd.unit` and/or the wiki page on writing service files.

I read about it on a SO page. I changed it to "ConditionPathExists", but upon rebooting I get:

● gpud.service - gpud service
   Loaded: loaded (/etc/systemd/system/gpud.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
Condition: start condition failed at Fri 2018-03-23 13:47:43 CET; 18s ago
           └─ ConditionPathExists=/sys/class/drm/card0/device/hwmon/hwmon1/pwm1 was not met

So it is still a timing issue.
When I boot the fans spin up pretty loudly and during boot (and before gdm) I can hear them be turned down (yet still not completely off). So that means that the driver was loaded and that the pwm1/fan1_input/etc files must exist in order for the driver to control the RPMs.
Yet why does it not work for my systemd file? It works perfectly fine in Gnome3 when I run it from a terminal.

Is there anyone with enough knowledge about the AMDGPU driver that can tell me more about the timing and what to do with the systemd service file?

Offline

#19 2018-03-23 13:16:28

ArchArrow
Member
Registered: 2017-02-15
Posts: 26

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

Update:

I just fixed it. It seems like the timing that I need isn't possible with systemd, so I just added a

time.sleep(3)

to the top of my script and it now works as it should.

Thanks so much for all your help!

Offline

#20 2018-03-23 13:55:06

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,441
Website

Re: Wrote a script to turn off my RX460 fans - systemd service doesnt work

If you are going to take that approach, you'd be better off ditching all the Wants/After in the service file and just use a timer with OnBootSec


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB