You are not logged in.

#1 2013-06-04 21:14:58

calzon65
Member
Registered: 2013-06-01
Posts: 11

[SOLVED] /usr/bin/echo from systemd .service

I use systemd to execute a shell script to change the status of an LED after boot when I reach multi-user state.  It is working fine, but I wanted to execute the shell script lines from within my systemd .service file instead of the systemd .service file executing the shell script.  Below is the systemd .service file but it’s not working.  The systemctl status shows no issues, but I have a gut feeling my problem might be related to me redirecting the /usr/bin/echo to a device. Could someone lend a suggestion?

Description=Set Green LED
After=multi-user.target

[Service]
Type=oneshot
ExecStart=/usr/bin/echo none > /sys/class/leds/status\:blue\:health/trigger
ExecStart=/usr/bin/sleep 1
ExecStart=/usr/bin/echo default-on > /sys/class/leds/status\:green\:health/trigger

[Install]
WantedBy=multi-user.target

Here is my systemctl status output:

setgreenled.service - Set Green LED
   Loaded: loaded (/etc/systemd/system/setgreenled.service; enabled)
   Active: inactive (dead) since Tue 2013-06-04 14:12:52 PDT; 3s ago
  Process: 273 ExecStart=/usr/bin/echo default-on > /sys/class/leds/status\:green\:health/trigger (code=exited, status=0/SUCCESS)
  Process: 270 ExecStart=/usr/bin/sleep 1 (code=exited, status=0/SUCCESS)
  Process: 268 ExecStart=/usr/bin/echo none > /sys/class/leds/status\:blue\:health/trigger (code=exited, status=0/SUCCESS)

Jun 04 14:12:51 pogoplug systemd[1]: Starting Set Green LED...
Jun 04 14:12:52 pogoplug echo[273]: default-on > /sys/class/leds/status\:green\:health/trigger
Jun 04 14:12:52 pogoplug systemd[1]: Started Set Green LED.

If I execute the lines below from the command-line it works fine.

/usr/bin/echo none > /sys/class/leds/status\:blue\:health/trigger
/usr/bin/echo default-on > /sys/class/leds/status\:green\:health/trigger

If I put the commands into a shell script and use systemd .service to execute the shell script, it works fine.  Here is that .service script

[Unit]
Description=Set Green LED
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/sh /home/public/setgreenled.sh

[Install]
WantedBy=multi-user.target

Last edited by calzon65 (2013-06-05 00:55:37)

Offline

#2 2013-06-04 21:33:11

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,304

Re: [SOLVED] /usr/bin/echo from systemd .service

It seems that systemd-tmpfiles functionality is more appropriate for this sort of thing:
https://wiki.archlinux.org/index.php/Sy … rary_files

tmpfiles may also be used to write values into certain files on boot.

Offline

#3 2013-06-04 21:37:24

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,201

Re: [SOLVED] /usr/bin/echo from systemd .service

berbae wrote:

tmpfiles may also be used to write values into certain files on boot.

Except... The OP wants it to happen after the multiuser target starts.  Kind of an annunciator that the system is ready.


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

#4 2013-06-04 21:52:29

calzon65
Member
Registered: 2013-06-01
Posts: 11

Re: [SOLVED] /usr/bin/echo from systemd .service

berbae, I did see the temp file stuff in the systemd wiki page earlier when I read through the wiki yesterday, but it did not seem that it was relevant to my needs.  Plus, I was not able to decipher how to create a temp file ... some lines start with d, some with x, etc., I could not learn much from the wiki page.

ewaller, you are correct, setting the LED light when the system reaches multi-user state is the objective.  As I mentioned earlier, everything works fine if I put the /usr/bin/echo commands into a shell script and use my systemd .service file to execute the shell script.  I just wanted to simplify things by putting the /usr/bin/echo commands into my .services file and eliminate the shell script file (e.g., one less file to worry about)

Last edited by calzon65 (2013-06-04 21:55:00)

Offline

#5 2013-06-04 22:09:27

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED] /usr/bin/echo from systemd .service

Systemd services are not shells.  So redirects will not work in that way.  You can use "bash -c '<command here>'" to accomplish this as that will create a shell for the command to run.  But I think that berbae is right that there are better alternatives for this.

You can use tmpfiles (man tmpfiles.d) which you can use to simply write a value to the given path, or you can use a udev rule to run a command when that particular path is detected.  Beware though that for some things, the tmpfiles way will sometimes try to run itself before the actual creation of the directory.  So you may run into some pace conditions with that method.  For some things, it is totally reliable though... so you'll just have to test and see.

A tmpfiles.d configuration is way easier to write than a udev rule.  But what you are trying to do would be a pretty basic udev rule, so it would probably not take you very long to learn how to do it.  It simply involves learning how to use the udevadm command really.

Offline

#6 2013-06-05 00:54:07

calzon65
Member
Registered: 2013-06-01
Posts: 11

Re: [SOLVED] /usr/bin/echo from systemd .service

WonderWoofy, thank you for the advice and to everyone else for your help.  I had a feeling, as I said in the top of my post, that the redirection I was trying to do might be the problem.  I really wanted to avoid needing both a .service script and a shell script, so with your advice WonderWoffy, I modified my .service script and it's now working as desired.

[Unit]
Description=Set Green LED
After=multi-user.target

[Service]
Type=oneshot
ExecStart=/usr/bin/bash -c '/usr/bin/echo none > /sys/class/leds/status\:blue\:health/trigger'
ExecStart=/usr/bin/sleep 1
ExecStart=/usr/bin/bash -c '/usr/bin/echo default-on > /sys/class/leds/status\:green\:health/trigger'

[Install]
WantedBy=multi-user.target

Last edited by calzon65 (2013-06-05 00:55:17)

Offline

#7 2013-06-05 20:51:31

calzon65
Member
Registered: 2013-06-01
Posts: 11

Re: [SOLVED] /usr/bin/echo from systemd .service

I experimented with a tmpfiles.d .conf file and that also works well, thank you.

w /sys/class/leds/status\:blue\:health/trigger - - - - none
w /sys/class/leds/status\:green\:health/trigger - - - - default-on

Last edited by calzon65 (2013-06-05 20:56:37)

Offline

#8 2013-06-05 20:55:18

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [SOLVED] /usr/bin/echo from systemd .service

You know though that you don't need both, right?  I mean, if you are going to use the tmpfiles.d, then disable/stop the service.  I actually think that if tmpfiles.d works, then it is in fact a cleaner solution.  Though I tend to favor udev rules since they are triggered by the actual existence of the directory rather than a service.

Offline

#9 2013-06-05 20:58:38

calzon65
Member
Registered: 2013-06-01
Posts: 11

Re: [SOLVED] /usr/bin/echo from systemd .service

Indeed WonderWoofy.  I wanted to experiment with multiple solutions to better embrace the new systemd and other methods ... in essence ... the ArchLinux way smile

And you read my mind, I also favored the tmpfiles.d .conf approach, so I deleted my original .services method.  I really appreciate all the feedback, everyone's responses have been positive and helpful.

Last edited by calzon65 (2013-06-05 20:59:50)

Offline

Board footer

Powered by FluxBB