How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
I am trying to run "echo 'on 0' | cec-client -s -d 1" on system boot in order to turn my TV on when Linux boots.
However I am not having any luck, the command works perfectly fine when manually entered into the terminal, but I need it to run automatically when the system boots.
I have tried putting the command in the startup applications custom command section https://imgur.com/a/pROPgn7 but it doesn't work.
Please help, I want to move away from using Windows as my HTPC. Windows has CEC-Client with a GUI so it's easy to use, but I want to use Linux now and all I need is to get this working when the system turns on.
Of course once I get this working I'll also want to run the command to turn the TV off on system shutdown as well.
Thanks for any help!
However I am not having any luck, the command works perfectly fine when manually entered into the terminal, but I need it to run automatically when the system boots.
I have tried putting the command in the startup applications custom command section https://imgur.com/a/pROPgn7 but it doesn't work.
Please help, I want to move away from using Windows as my HTPC. Windows has CEC-Client with a GUI so it's easy to use, but I want to use Linux now and all I need is to get this working when the system turns on.
Of course once I get this working I'll also want to run the command to turn the TV off on system shutdown as well.
Thanks for any help!
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
You are attempting to execute a shell command line and should as such do so via a shell indeed. I.e., try e.g.
as the most direct answer.
Code: Select all
sh -c 'echo on 0 | cec-client -s -d 1'
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Hi PsychedelicHell,
I believe the interpreter the startup commands thingamajig uses is too basic. Try this command instead exactly as typed:
EDIT: Now why didn't the forum tell me rene beat me this time?
I believe the interpreter the startup commands thingamajig uses is too basic. Try this command instead exactly as typed:
Code: Select all
bash -c "echo 'on 0' | cec-client -s -d 1"
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Hey Rene & MrEen,
Your solution works, but in the meantime I managed to get it working myself!
I got it working by making basic .sh file, I called mine tvon.sh inside that file I put only the command "echo 'on 0' | cec-client -s -d 1" I then made a new folder in Home and called it custom, I placed the tvon.sh file in there. Then I went to the startup applications menu, and clicked add, I gave it a title, description and then command section I simply put in "sh /home/media/custom/tvon.sh"
However, your solution seems a lot cleaner as I don't need to have a .sh file sitting in a Home folder.
Thank you for your command, I spent hours on this!
Now is there any chance you could help me run "echo 'standby 0' | cec-client -s -d 1" on system shutdown?
Your solution works, but in the meantime I managed to get it working myself!
I got it working by making basic .sh file, I called mine tvon.sh inside that file I put only the command "echo 'on 0' | cec-client -s -d 1" I then made a new folder in Home and called it custom, I placed the tvon.sh file in there. Then I went to the startup applications menu, and clicked add, I gave it a title, description and then command section I simply put in "sh /home/media/custom/tvon.sh"
However, your solution seems a lot cleaner as I don't need to have a .sh file sitting in a Home folder.
Thank you for your command, I spent hours on this!
Now is there any chance you could help me run "echo 'standby 0' | cec-client -s -d 1" on system shutdown?
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
I'll have to defer on that one. I'm using Xfce, which would normally require altering /usr/bin/xfce4-session. Although at least in 19.3 there are some triggers available in the Application Autostart section that might include on shutdown. I know it at least has on logout. I don't know if that same setting is available on other DE's, but I suspect it might be.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Can't see any shutdown/logout variables, these https://imgur.com/a/OLJf9X3 are the only options available to me in the startup applications menu
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Yeah, that's definitely not like what Xfce has. Someone will be able to help you I'm sure, just give it a little time.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
As far as I am aware every desktop environment has this fundamental asymmetry startup applications, yes, shutdown applications, no, and given that you also need a shutdown one that's to say you shouldn't use the desktop startup applications for the startup one either, but turn this into a systemd service. I believe the following should work: disable the desktop startup application again and create as root a file e.g. /etc/systemd/system/cec-client.service consisting ofPsychedelicHell wrote: ⤴Wed Jun 24, 2020 2:29 pm Now is there any chance you could help me run "echo 'standby 0' | cec-client -s -d 1" on system shutdown?
Code: Select all
[Unit]
Description=Turn on/off TV on boot/shutdown
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo on 0 | /usr/bin/cec-client -s -d 1'
ExecStop=/bin/sh -c 'echo standby 0 | /usr/bin/cec-client -s -d 1'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
sudo systemctl enable cec-client.service
. Feel of course free to name it anything other than "cec-client".Could clearly not test this but should work as a general template.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
I agree with rene regarding the use of a systemd service.
Regarding Xfce, it can run an application on shutdown as I just checked a VM of 19.3. See screenshot:
Regarding Xfce, it can run an application on shutdown as I just checked a VM of 19.3. See screenshot:
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Oh hey, I had never noticed. For jobs (such as supposedly here) that don't need the GUI up and running first the systemd route is probably best, but otherwise, sure, adding a shutdown job in that sense then also works.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Code: Select all
[Unit]
Description=Turn on/off TV on boot/shutdown
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo on 0 | /usr/bin/cec-client -s -d 1'
ExecStop=/bin/sh -c 'echo standby 0 | /usr/bin/cec-client -s -d 1'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
I followed your instructions but the code is only half working, the TV did not turn off on shutdown but it did turn on at system boot.
I'm guessing maybe this is because there is a 3ish second delay between pressing enter and the command actually turning the TV off. On my end once the command is entered an echo returns with "opening a connection to the CEC adapter..." then it turns the TV off.
Is the computer perhaps turning off before it's able to actually send the command to the CEC adapter? Is there a way to add a small delay after the shutdown command to allow it enough time to execute?
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Did not turn of the first time you shut down after putting that file in place and enabling it, or still not? First time would be expected (I sh/could have made you do a
sudo systemctl daemon-reload
) but if stilll not now I wouldn't know why that would be. In that case, if you in a terminal run journalctl -b -1
and with "/TV" search for cec-client.service stopping, do you notice anything odd? It seems the delay should not be needed.Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
.... but since I'm now really off: if it's indeed still not working also the second time, yes, a delay is easy to insert. Making the ExecStop line be
should be all there's to it. Quite horrible, but if needed...
Code: Select all
ExecStop=/bin/sh -c 'echo standby 0 | /usr/bin/cec-client -s -d 1; /bin/sleep 3'
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Hey Rene,
I appreciate all your help but it's still not turning off on shutdown.
I think I'll swap my Cinnamon for Xfce and try out the codes there, I'll report back here with my results.
EDIT: Same issue in Xfce, can get the screen to turn on with boot but not turn off on shutdown.
I appreciate all your help but it's still not turning off on shutdown.
I think I'll swap my Cinnamon for Xfce and try out the codes there, I'll report back here with my results.
EDIT: Same issue in Xfce, can get the screen to turn on with boot but not turn off on shutdown.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Unfortunately debugging systemd shutdown is one the absolute worst things about systemd but you'd in any case need to do that journalctl digging I advised to see if some specific error occurs: I can as said not test the specifics here, and the general setup works fine for me in other circumstances. I already made it part of multi-user.target on purpose which should as far as I am aware also mean it's stopped early enough to not have to deal with e.g. a read-only root but the one thing I can think of now is trying it without the
systemd's graphical.target I've found on numerous occasions before to not be in fact functional at least on Ubuntu/Mint so if that horrible systemd system manager again Just Does Not Work after the above --- mind you, explicitly including that journalctl viewage to look for an explicit error --- I'd be left advising the Xfce shutdown application route that MrEen found to exist, in that case to be paired with my initial suggestion for the startup job.
-d 1
switch (or perhaps -d 0
or similar if logging to disk is default behaviour) so as to further lessen its environmental dependencies.systemd's graphical.target I've found on numerous occasions before to not be in fact functional at least on Ubuntu/Mint so if that horrible systemd system manager again Just Does Not Work after the above --- mind you, explicitly including that journalctl viewage to look for an explicit error --- I'd be left advising the Xfce shutdown application route that MrEen found to exist, in that case to be paired with my initial suggestion for the startup job.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Only one?is one the absolute worst things about systemd
Luddite here, some of us liked init
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Well, while that clearly should've read "one of the worst things" it's sort of evident then that I would not hold it to be the only bad thing. "Opaqueness" as I have stated before in its context: while I may (or may not) be able to generally sort through things when having the issue in front of me internet-debugging especially for/with someone not necessarily fully conversant with things breaks down quite as a matter of course.
"So use all standard components then and let GNOME take care of things without you needing to worry your pretty little head about it, now, you great big silly!" Or something. Yes, well, welcome, Lindows.
Anyways. Let's not rant-ise this until OP's been helped, one way or the other.
"So use all standard components then and let GNOME take care of things without you needing to worry your pretty little head about it, now, you great big silly!" Or something. Yes, well, welcome, Lindows.
Anyways. Let's not rant-ise this until OP's been helped, one way or the other.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
Did you try putting the /bin/sh command in the Application Autostart tab of Session and Startup with the on shutdown Trigger?PsychedelicHell wrote: ⤴Thu Jun 25, 2020 1:19 am Hey Rene,
I appreciate all your help but it's still not turning off on shutdown.
I think I'll swap my Cinnamon for Xfce and try out the codes there, I'll report back here with my results.
EDIT: Same issue in Xfce, can get the screen to turn on with boot but not turn off on shutdown.
I'm more than half expecting that not to work either, but just wanted to check if you'd tried.
Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?
The command I was using for shutdown is and this does not work.
I tried in the terminal which didn't work as Xfce reports back with "bash: -c command not found". I also tried it in the session start up/shut down and the computer shutdown without turning the screen off first, I have the same results with restart and logout too. I also tried which did nothing.
works perfectly to turn the screen on with either the terminal or automatically with the startup trigger.
works to turn the screen off via the terminal but does not work with the shutdown trigger.
Just for fun I tried on startup to see if it would turn the screen off when logging in and it did about half a second after the desktop appears.
So as far as I can see the commands and are fine, it's just something in Linux that won't let it execute on shutdown/reboot/logout.
Code: Select all
bash -c echo 'standby 0' | cec-client -s -d 1
I tried
Code: Select all
ExecStop=/bin/sh -c 'echo standby 0 | /usr/bin/cec-client -s -d 1'
Code: Select all
/bin/sh -c 'echo standby 0 | /usr/bin/cec-client -s -d 1'
Code: Select all
bash -c "echo 'on 0' | cec-client -s -d 1"
Code: Select all
bash -c "echo 'standby 0' | cec-client -s -d 1"
Just for fun I tried
Code: Select all
bash -c "echo 'standby 0' | cec-client -s -d 1"
So as far as I can see the commands
Code: Select all
bash -c "echo 'on 0' | cec-client -s -d 1
Code: Select all
bash -c "echo 'standby 0' | cec-client -s -d 1
Okay, I'll uninstall Xfce and reinstall Cinnamon and try your systemd code with the changes again.