How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
PsychedelicHell

How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by PsychedelicHell »

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!
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.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by rene »

You are attempting to execute a shell command line and should as such do so via a shell indeed. I.e., try e.g.

Code: Select all

sh -c 'echo on 0 | cec-client -s -d 1'
as the most direct answer.
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by MrEen »

Hi PsychedelicHell,

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"
EDIT: Now why didn't the forum tell me rene beat me this time?
PsychedelicHell

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by PsychedelicHell »

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?
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by MrEen »

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.
PsychedelicHell

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by PsychedelicHell »

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
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by MrEen »

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.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by rene »

PsychedelicHell 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?
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 of

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
and enable it via 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.
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by MrEen »

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:
Attachments
shutdown1.png
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by rene »

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.
PsychedelicHell

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by PsychedelicHell »

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
Hey Rene,

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?
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by rene »

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.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by rene »

.... 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

Code: Select all

ExecStop=/bin/sh -c 'echo standby 0 | /usr/bin/cec-client -s -d 1; /bin/sleep 3'
should be all there's to it. Quite horrible, but if needed...
PsychedelicHell

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by PsychedelicHell »

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.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by rene »

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 -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.
User avatar
AndyMH
Level 21
Level 21
Posts: 13728
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by AndyMH »

is one the absolute worst things about systemd
Only one?
Luddite here, some of us liked init :D
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by rene »

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.
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by MrEen »

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.
Did you try putting the /bin/sh command in the Application Autostart tab of Session and Startup with the on shutdown Trigger?

I'm more than half expecting that not to work either, but just wanted to check if you'd tried.
PsychedelicHell

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by PsychedelicHell »

The command I was using for shutdown is

Code: Select all

bash -c  echo 'standby 0' | cec-client -s -d 1
and this does not work.

I tried

Code: Select all

ExecStop=/bin/sh -c 'echo standby 0 | /usr/bin/cec-client -s -d 1'
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

Code: Select all

/bin/sh -c 'echo standby 0 | /usr/bin/cec-client -s -d 1'
which did nothing.

Code: Select all

bash -c "echo 'on 0' | cec-client -s -d 1"
works perfectly to turn the screen on with either the terminal or automatically with the startup trigger.

Code: Select all

bash -c "echo 'standby 0' | cec-client -s -d 1"
works to turn the screen off via the terminal but does not work with the shutdown trigger.

Just for fun I tried

Code: Select all

bash -c "echo 'standby 0' | cec-client -s -d 1"
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

Code: Select all

bash -c "echo 'on 0' | cec-client -s -d 1
and

Code: Select all

bash -c "echo 'standby 0' | cec-client -s -d 1
are fine, it's just something in Linux that won't let it execute on shutdown/reboot/logout.

rene wrote: Thu Jun 25, 2020 7:59 am 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.
Okay, I'll uninstall Xfce and reinstall Cinnamon and try your systemd code with the changes again.
User avatar
MrEen
Level 23
Level 23
Posts: 18343
Joined: Mon Jun 12, 2017 8:39 pm

Re: How can I run "echo 'on 0' | cec-client -s -d 1" on system boot?

Post by MrEen »

Hang on a sec ...
Locked

Return to “Scripts & Bash”