User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Easily disable / enable serial console

Sun Sep 08, 2013 11:22 am

Hi all,

I was getting tired of always editing /etc/inittab and /boot/cmdline.txt to turn off and on the serial console (more info here) running on the Pi's UART, so I've now written a neat little Bash script to automate this task 8-)
https://github.com/lurch/rpi-serial-console

For advanced users it also gives you the option to change the baud-rate that the serial console runs at.

Feedback welcome!

EDIT:
After installing the script, the serial console can be easily disabled with just

Code: Select all

sudo rpi-serial-console disable
and re-enabled again just as easily with

Code: Select all

sudo rpi-serial-console enable
Last edited by AndrewS on Thu Sep 12, 2013 12:28 pm, edited 1 time in total.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Easily disable / enable serial console

Thu Sep 12, 2013 9:53 am

Well.... that went down like a lead balloon! ;)

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Easily disable / enable serial console

Thu Sep 12, 2013 10:38 am

AndrewS wrote:Well.... that went down like a lead balloon! ;)
Heh heh. 'Tis the way of the 'Net.

You could develop a 100% sure cure for cancer, publish it on a "hacker-oriented" board (such as this one), and get no responses at all.

This doesn't mean, of course, that people haven't (and/or won't) download and use your solution, of course...
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Easily disable / enable serial console

Thu Sep 12, 2013 10:49 am

Joe Schmoe wrote:You could develop a 100% sure cure for cancer, publish it on a "hacker-oriented" board (such as this one), and get no responses at all.

This doesn't mean, of course, that people haven't (and/or won't) download and use your solution, of course...
...and then copy it, claim they developed it themselves, and claim all the credit? :mrgreen:

SteveSpencer
Posts: 351
Joined: Thu Mar 28, 2013 9:19 am
Location: Nottingham, UK

Re: Easily disable / enable serial console

Thu Sep 12, 2013 11:51 am

The reboot really isn't necessary.
You can run

Code: Select all

telinit q
(as root, of course, so "sudo telinit q" is more likely correct, unless you're running the rpi-serial-console via sudo anyway)
and that will either start or stop the getty accordingly. Much more convenient and quicker than a reboot.

Steve
Steve S
No, I can't think of anything funny that won't offend someone if they want it to...

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Easily disable / enable serial console

Thu Sep 12, 2013 12:16 pm

ednl wrote:All right, here's some meta-feedback
Thanks! 8-)
despite being a total Mac-head I loathe the use of the term "simply" … Almost always redundant and often inappropriate because totally subjective.
Sorry, I guess you'll have to simply get used to it / ignore it ;)
Also, why didn't you post the code and readme here on the forum instead of on Github? (I know, version control.) The extra click probably cost you readers.
People are really that lazy? :( Maybe I'll edit the first post to provide a bit more detail.
Other than that, the script seems quite handy :-) Too bad it needs a reboot; is there absolutely no way to avoid that? Maybe killing/starting/restarting certain processes?
I don't think so - the reboot is necessary so that the kernel will re-read cmdline.txt and then not run a serial console on /dev/ttyAMA0. If the serial console could be killed without requiring a kernel reboot, it would make it fairly useless as a debugging device (which is what the serial console is traditionally used for).
- Test for $(whoami) == "root" instead of relying on $EUID.
Does that make much difference? I always thought that testing for an effective id was better than testing for an actual id?
- Sometimes case is easier to use and/or read than if statements.
*shrug* ;-)
I tend to save 'case' for when I have an 'if/elif/elif/else' block, and I've not got any 'elif's in this code.
- What if the .bak files already exist? Maybe try something like "i=1; while (exist file.$i.bak) ++i" (pseudo code)
If .bak files already exist, then I assume they're already backups of the original files. I didn't do something like you suggested, because I did something similar before (on an entirely unrelated project) and the user never deleted the backup files, so I ended up with files like filename.21.bak (!)
- The /boot/cmdline.txt edit seems a bit convoluted, might want to look into sed substitution to do it in one line. But hey, it works.
Exactly, "it works" ;-) And the solution I've used is probably more "accurate" than a simple sed would be.

Thanks for your comments.

Just made a small tweak to the script so that error messages get printed to STDERR instead of STDOUT.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Easily disable / enable serial console

Thu Sep 12, 2013 12:22 pm

SteveSpencer wrote:The reboot really isn't necessary.
You can run

Code: Select all

telinit q
Ooh, interesting... from the telinit man page:
The following arguments serve as directives to telinit:
Q or q tell init to re-examine the /etc/inittab file.
So that would indeed kill the getty, but wouldn't the kernel still be running the serial console on /dev/ttyAMA0 ? So a reboot is still necessary?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Easily disable / enable serial console

Thu Sep 12, 2013 6:37 pm

...or if you wanted to be really clever, you could eliminate the 'cat' and 'xargs' and do it all with a single 'sed' command:

Code: Select all

u=ttyAMA0; sed -i -e "s/\\S*$u\\S*//g" -e "s/ \+/ /g" -e "s/^ //" -e "s/ $//" /boot/cmdline.txt
:ugeek:

But I'm happy with the way my script does it currently, I don't plan to change it.

SiriusHardware
Posts: 500
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Easily disable / enable serial console

Thu Oct 03, 2013 5:54 pm

This is a decent idea and thanks for doing a stand-alone version of it, it certainly is a chore going around manually editing various files to claim ownership of the serial port. Ulimately the ideal place for this would be in Raspi-Config, allowing the user to simply choose either the current default (console) mode or the 'free for the user to use' mode. The same goes for things like SPI enabled / not enabled, etc - maybe some of these things are already present in the newest Raspbian? I'm running a fairly old version now.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Easily disable / enable serial console

Thu Oct 03, 2013 11:40 pm

SiriusHardware wrote:Ulimately the ideal place for this would be in Raspi-Config, allowing the user to simply choose either the current default (console) mode or the 'free for the user to use' mode.
Great idea! I may send a PM to asb to suggest it :)
The same goes for things like SPI enabled / not enabled, etc - maybe some of these things are already present in the newest Raspbian? I'm running a fairly old version now.
Yup, latest version of raspi-config now includes an option for enabling SPI http://www.raspberrypi.org/archives/4959
Comments on that article also provide details for upgrading your existing install without having to reflash a new SD image 8-)

silverk
Posts: 2
Joined: Sat May 25, 2013 10:25 pm
Location: EST-TLN

Re: Easily disable / enable serial console

Sun Nov 24, 2013 12:54 pm

Oh thank you.

this tread saved me little bit thinking why serial console was disabled after boot.

If you want to use Raspberry Pi to Arduino shields connection bridge you need to disable serial console. I forgot already, that I did it several months ago.

error message

Code: Select all

Inconsistent status! /boot/cmdline.txt is enabled, but /etc/inittab is disabled
pointed me to right direction.

tlankford01
Posts: 4
Joined: Mon Aug 18, 2014 4:28 am

Re: Easily disable / enable serial console

Thu Sep 11, 2014 1:23 am

Thank you for this script. That made that easy.

User avatar
DougieLawson
Posts: 35358
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Easily disable / enable serial console

Thu Sep 11, 2014 8:27 am

tlankford01 wrote:Thank you for this script. That made that easy.
This thread is so old that the script is now a built-in feechur of raspi-config.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: Easily disable / enable serial console

Thu Sep 11, 2014 5:29 pm

Hmm... how hard would it be to check a push on/off switch and set accordingly?

I've got a nice led ring clock project part-way completed and it would be super convenient to be able to switch it from 'uart as terminal' mode to 'uart inputs to my python script' mode.

It's on a model a, the network's stopped working for some reason, and at the moment it's hanging on the wall with me doing occasional tweaks via Bluetooth serial module. It'd be nice to have the finished item controllable with one of the many 'arduino control' android apps.

Hope that wasn't too unclear...
note: I may or may not know what I'm talking about...

klricks
Posts: 6435
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Easily disable / enable serial console

Thu Sep 11, 2014 7:37 pm

toxibunny wrote:Hmm... how hard would it be to check a push on/off switch and set accordingly?

I've got a nice led ring clock project part-way completed and it would be super convenient to be able to switch it from 'uart as terminal' mode to 'uart inputs to my python script' mode.

It's on a model a, the network's stopped working for some reason, and at the moment it's hanging on the wall with me doing occasional tweaks via Bluetooth serial module. It'd be nice to have the finished item controllable with one of the many 'arduino control' android apps.

Hope that wasn't too unclear...
I am sure it would be pretty easy to switch especially if you call the script that the newest raspi-config uses but I am pretty sure you have to reboot after changing each time.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

Return to “Interfacing (DSI, CSI, I2C, etc.)”