Showing posts with label reset. Show all posts
Showing posts with label reset. Show all posts

Friday, October 31, 2008

11 Simple Ways To Recover Your Screen on Linux and Unix

Hey there,

If you're one of the many readers who submit a comment now and again (and even if you aren't ;), today's post is an homage to your helpful and insightful suggestions and tips. Some of you reading this right now may have emailed me with something that deserves to be posted as a comment to another post, and I can say, with great confidence, that it eventually will. I personally respond to all emails, so sometimes balancing that with work, family and more work can cause me to get a little behind. I'm still going to keep doing things this way, of course, not because I'm lazy or narrow-minded (okay, that's up for debate ;), but because I believe in actually responding to anyone who takes the time to send me a comment. And, just to clarify, this response-ethic of mine does not apply in any way shape or form to folks who write me one sentence vulgarities. If you want (or need) to hurl an insult my way, I strongly encourage it, but I probably won't write you back. Like with every other cycle, no matter who started it, someone has to end it :)

And, now, just to keep you on your toes, I'll include a hyperlink back to the original article on 9 simple ways to recover your screen in the same sentence that I let you know that this post is virtually the same material, only bigger, better and with extra sauce ;) Unless you want to read the rambling introduction (which is different in the original post), don't bother visiting the original page. All of the useful stuff from that post is included in today's version.

Once, again, a super-sized Thanks to everyone who wrote in with suggestions for this post and any other post. I truly appreciate your taking the time to write in and am almost always impressed, after a reading, with how much I don't know ;)

And, here we go again. Attribution for reader suggestions will accompany those points; otherwise, this post - from here on out - will be almost exactly the same as the last one.

Have a great weekend!

1. See if you can clear the screen the obvious way

host # clear

2. Or maybe resetting it might help

host # reset

3. stty is also usually available on most Linux and Unix distributions, and it has a sanity check built in. Worth a shot:

host # stty sane

4. tput is also fairly common. You can use it to clear your screen, or reset it, almost like the first two options did (just from a different angle):

host # tput clear

5. and...

host # tput reset

6. Or you can use it to re-initialize the screen (gross oversimplification, but you just want your screen back, right? ;)

host # tput init

7. Finally, you can try to get it to sort out the garbage on your screen by resetting the bold and underline definitions (this makes no sense to me at all, but it works sometimes;)

host # tput sgr 0 0

8. If your shell built-in echo can do this, it's fairly easy to take this route:

host # echo -e \033c

9. And, if it doesn't (or you just can't tell because your characters are still all goofed), you can type the control characters directly:

host # echo ^[c <-- Which, if you don't want to check out the link above, translates to typing: echo and then ctl-v followed by hitting the escape key and then typing the regular letter c

10. Suggested by Matthew Lenz! Here's a very simple way to get back your screen. Why does it work? I have no idea, but I tested it and it does on every machine I could try it out on :

host # cat /bin/cat

11. Suggested by James Ranks! One of these control key combinations will almost always work for you and remove the garbage from your screen (see point 9 for a link back on how to really represent control characters in Linux or Unix) :

host # ctrl-v followed by ctl-n

or

host # ctrl-v followed by ctl-o

Hopefully this little cheat sheet will come in handy at some time. You'll most likely be typing "in the dark."

Cheers,

, Mike



zcat made this comment regarding the post. It illustrates a good point: If things get too hairy, you have to know when to just bail and fix your terminal the old-fashioned way :)

I've catted plenty of binary files in the past and never needed anything
beyond 'reset' to recover from it. Perhaps I just got lucky.

But if I ever did need more than a reset, I think I would probably just
log out and back in at that point; the advantage of using a short
username like 'zcat' (and on the hosts where it's my full name I use
key-based authentication so I never have to type it)







Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Friday, August 22, 2008

9 Simple Ways To Recover Your Screen on Linux and Unix

Hey there,

Wow, it's been a long work week. Figuratively, of course. The official version is that it was a standard 5 days ;) Tonight's post is a bit on the lighter side (both the material covered and the overall tone), as I haven't had much time to write this week. It's like my wife says; I should take some of the 1500 word rants I put down and split them up every once in a while. She's a smart lady. One of these days I'll start listening to her and take it easy on my keyboard ;)

Hopefully, today's title didn't give anyone the wrong impression. If you've gone and thrown your monitor off the balcony in a fit of rage, this post can't help you, except to offer the following advice: Go to the store and buy a buy a new one (they're much cheaper now than when you may have bought yours, which will help offset the counseling, therapy and court-ordered community service expenses ;) Then make a solemn resolution. Promise yourself that, the next time you work all night on a complex program that, even to you, barely makes any sense by the time you've accidentally deleted random chunks of it before remembering to write the buffer to disk, you will take 5 seconds, walk away, huff in a deep deep breath, count to 10 and then smash it with a brick, instead. You'll be as happy as you were last time, but you probably won't get arrested ;)

If you're a pragmatist, take a moment to reflect on the fact that (although this is a very interesting phenomena that I experience from time to time), even though you desperately want to smash your monitor into bits, it has absolutely nothing to do with the problem. For instance, I often whack my monitor on the side when Windows takes 45 minutes to boot up. I should be smacking the OS install disks, or the computer itself. If I was in full possession of my faculties (or, at least, my sense of reason) I would be kicking my own arse for expecting anything more ;)

Alas, enough pontificating. To get your weekend started off right, here are 9 ways you can, hopefully, get your terminal output back to normal if you ever cat a binary executable or do anything else that makes it all turn "Chinese" on you. Enjoy and have a safe weekend :)

Start out with the classics (NOTE: All examples will be given as command line mock-ups which end in a carriage return or enter-key press - I'm assuming you'll be typing blind)

1. See if you can clear the screen the obvious way

host # clear

2. Or maybe resetting it might help

host # reset

3. stty is also usually available on most Linux and Unix distributions, and it has a sanity check built in. Worth a shot:

host # stty sane

4. tput is also fairly common. You can use it to clear your screen, or reset it, almost like the first two options did (just from a different angle):

host # tput clear

5. and...

host # tput reset

6. Or you can use it to re-initialize the screen (gross oversimplification, but you just want your screen back, right? ;)

host # tput init

7. Finally, you can try to get it to sort out the garbage on your screen by resetting the bold and underline definitions (this makes no sense to me at all, but it works sometimes;)

host # tput sgr 0 0

8. If your shell built-in echo can do this, it's fairly easy to take this route:

host # echo -e \033c

9. And, if it doesn't (or you just can't tell because your characters are still all goofed), you can type the control characters directly:

host # echo ^[c <-- Which, if you don't want to check out the link above, translates to typing: echo and then ctl-v followed by hitting the escape key and then typing the regular letter c

Hopefully this little cheat sheet will come in handy at some time. You'll most likely be typing "in the dark." If none of these options work, I usually give in and just disconnect and re-login (It's amazing how much work goes into being lazy ;)

Cheers,

, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Thursday, June 12, 2008

Using chkconfig To Manage Linux Service Run Levels

Hey there,

If you're like me, you've probably gotten used to creating init scripts and all the various linking to run level directories that needs to be done so that certain services will start and stop at the run levels you decide you want them to.

Even so, I'm always impressed with ways to make this sort of setup easier. Especially when it comes to manipulation of start and stop (init) scripts that come with system software. Solaris' mish-mosh of hard and soft linked run level scripts can be a headache, for sure, but Redhat, Fedora and other Linux flavours can be a bit of a pain, as well. The main problem I run into is making a customized change that ends up getting overwritten by an RPM upgrade or things of that nature.

Thankfully, that issue has been addressed, more than adequately, in a few ways. One of my favorites is the chkconfig command (usually located in the /sbin directory). This tool makes it very easy for an administrator to control at what run levels virtually every system-controlled service starts and stops.

Today, we're going to look at a few common ways to use chkconfig to make administration of run levels more convenient.

The first way is the most obvious. If you want to know everything that's going on with every service that's being controlled, simply type:

host # /sbin/chkconfig --list

and you'll get output similar to this:

acpid 0:off 1:off 2:off 3:off 4:on 5:on 6:off
anacron 0:off 1:off 2:on 3:on 4:on 5:on 6:off
...


An easy way to find out what services are "on" is to just pipe the above command to grep. If you want to know what's running "now" (or, at least, what should be ;), you can find your run level with the who command and then use chkconfig to find all the active services:

host # who -r
run-level 3...
host # chkconfig --list|grep "3:on"


or, if you're only interested in one service, you can specify it and chkconfig will query its state at your current run level, like so:

host # chkconfig anacron

More specifically (since that would only show you services that get started at run level 3, you could do this:

host # chkconfig --list|egrep '[123]:on'

and you should see all the services that are running currently (assuming your machine boots normally, through run levels 1 and 2, before getting to 3).

You can also use chkconfig to configure any listed service to start and/or stop at any run level you choose. You can even do multiple run levels at once. So, all of the following commands would be valid (assuming you have the service "anacron" on your machine):

host # chkconfig anacron on
host # chkconfig anacron off
host # chkconfig --level 3 anacron on
host # chkconfig --level 3 anacron off
host # chkconfig --level 235 anacron on
host # chkconfig --level 046 anacron off


Also note that on some systems, on and off only apply to run levels 2, 3, 4 and 5. Setting other levels may not work, but should not cause failure.

An alternative to on or off, is to use reset. This keyword will reset the service to start and stop at the run levels it defaults to (look near the bottom of this post to see from where chkconfig gets that information).

host # chkconfig anacron reset

Some simple scripting can make it so that you can replicate this behaviour for multiple services. At this point, to my knowledge, you can only either make changes globally or per-service. In any event, something like this is simple enough to work out at the command line (after checking out all of the services with --list, put the ones you want to start at runlevels 3 and 5 , and be turned off at all other levels, into a text file, one service per line). Note, that this is just my little command line script, and can be done a lot of different ways, so you should do this however makes the most sense for you:

host # for x in `cat MYSERVICES`;do /sbin/chkconfig --level 35 $x on;/sbin/chkconfig --level 01246 off;done

And the final (and most helpful, since it keeps you from having to remember how to format an init script to work with chkconfig) thing chkconfig does, is allow you to add and delete services. If your service isn't standard, you may still have to write your own init script, but for all packaged (and handled) services, this is a snap.

To remove a service that's currently active, just type:

host # chkconfig --del anacron

And to reinstate it, just type

host # chkconfig --add anacron

And, bingo, you're all set :) Note that the above add/del function really only creates and destroys symlinks from the init directory to the run level directories, so if you've deleted the actual init script, using chkconfig to re-add the service probably won't work. If you still have access to the RPM for that package, you can fix it to replace any missing files that way first.

If you do decide to create your own init script, and want it to be controlled by chkconfig, just make sure that you have these lines near the top of the script, in the comment section, below the shebang line and above any other comments in the script, like this:

#!/bin/sh

#
# chkconfig: 235 10 60


This part of the init script is what chkconfig looks for, and, in this instance, it tells chkconfig that the service should start at run levels 2, 3 and 5, it should be turned off for all other run levels (chkconfig's default assumption) and that it should have a start priority of 10 and a stop priority of 60. The priority numbers that you include in your script are relative and shouldn't generally be an issue. The defaults are usually 20 and 80. You can also add a "#description:" line (directly after the required chkconfig line) to your script if you want to be able to see that in chkconfig output, but it's not necessary.

Here's hoping this helps your run level management step up and get easier, all at the same time! :)

Cheers,

, Mike