5 quick and dirty Linux tips you may not know

Share Button

Remember that day you were sitting at a friendly bash$ prompt and discovered tab completion? It was like a choir of angels came down from the heavens and sang Hallelujah, the Shell Hath Come (yes, that’s a bourne again reference). While these 5 tips may not be that monumental, they are still things I wouldn’t be able to do without… perhaps they will help you as well.

1. Grep without the grep

I frequently see a lot of people running grep commands like this: ps -ef | grep pmon | grep -v grep

Since the ‘ps’ command is a process list, it unfortunately will not only return the pmon process you are looking for but the ‘grep pmon’ command you just ran.

[skaram@server2 ~]$ ps -ef | grep pmon
skaram   15755 15704  0 11:35 pts/0    00:00:00 grep pmon
oracle   31151     1  0  2012 ?        23:38:20 ora_pmon_ORCL

It is obnoxious, but easy to workaround. However, the ‘grep -v’ command is the long way. Here is the short way around the issue:

ps -ef | grep [p]mon

Simply wrap the first character of the search term in square brackets, and you are good to go.

[skaram@server2 ~]$ ps -ef | grep [p]mon
oracle   31151     1  0  2012 ?        23:38:20 ora_pmon_ORCL

2. Kill them all

Have you ever needed to, say, kill all LOCAL=NO processes on an Oracle server? Yeah, me neither. But if you did, not saying that I have, this is the kind of command I would use:

kill -9 `ps -ef | grep -i [l]ocal=no | awk '{print $2}'`

DISCLAIMER: DO NOT DO THAT ON A PRODUCTION SYSTEM.

3. Space hogs

This one is probably a little better known, but if you need to find the top space users in a directory just run the following command:

du -k | sort -n

[root@220883 log]# du -k | sort -n
4	./conman
4	./conman.old
4	./cups
4	./news/OLD
4	./pm
4	./ppp
4	./samba
4	./squid
4	./vbox
8	./mail
8	./news
52	./prelink
2864	./directadmin
4940	./httpd/domains
7000	./httpd
15576	./proftpd
20292	./audit
44856	./exim
216272	.

Note that the output number will be in kilobytes.

4. Getting the magic back

Does your job or client require you to use sudo in order to log into a database server as Oracle? Depending on the way you get X Windows going, that may have caused you grief in the past (particularly with installing Oracle). But you can easily grab your auth data and transfer it when you sudo to a new user like so:

sma11x:~ steve$ ssh -X skaram@server2
skaram@server2's password: 
[skaram@server2 ~]$ echo $DISPLAY
localhost:10.0
[skaram@server2 ~]$ xclock

And you get the clock, right? But you need to be oracle to do an install. Before you do that sudo command, type xauth list

[skaram@server2 ~]$ xauth list
server2/unix:10  MIT-MAGIC-COOKIE-1  c183a5016b775c9692b2da6b31552dae

When I sudo to oracle, notice xclock doesn’t work:

[skaram@server2 ~]$ sudo -u oracle -H bash
Password: 
bash-3.2$ xclock
X11 connection rejected because of wrong authentication.
X connection to localhost:10.0 broken (explicit kill or server shutdown).

But I can add back my authorization by using xauth add and pasting the entire line from the xauth list output.

bash-3.2$ xauth add server2/unix:10  MIT-MAGIC-COOKIE-1  c183a5016b775c9692b2da6b31552dae
xauth:  creating new authority file /export/home/oracle/.Xauthority
bash-3.2$ xclock

Doing so will make X it work once again, but now as the oracle user.

5. Screen your sessions

This one was a godsend. Have you ever been working on a long running operation and you lost your connection to Oracle, or had to go home, or god knows what else? It is a horrible pain and one that really messes up a lot if it gets you at the wrong time. That should be a thing of the past if you use screen.

Screen is a multiplexed terminal, allowing you to spawn multiple terminals in a single terminal session. Opening a new ‘screen’ is simple; simply type the word screen at a Linux command line:

[skaram@server2 ~]$ echo HELLO!
HELLO!
[skaram@server2 ~]$ screen

Notice your terminal clears and you start at a new prompt. Now I will go ahead and make it wait for input.

[skaram@server2 ~]$ read -p "Press Enter"
Press Enter

If I press CTRL+A, then CTRL+D, my screen will detach, putting me back on the ‘parent’ terminal session:

[skaram@server2 ~]$ 
[skaram@server2 ~]$ 
[skaram@server2 ~]$ 
[skaram@server2 ~]$ 
[skaram@server2 ~]$ echo HELLO!
HELLO!
[skaram@server2 ~]$ screen
[detached]
[skaram@server2 ~]$ 

If I want to get back to the child screen, I can type screen -rx to re-attach. Or start a new terminal with a fresh ‘screen’ command. You can even detach from multiple terminals and connect to the one of your choosing:

[skaram@server2 ~]$ screen -list
There are screens on:
	10913.pts-2.server2	(Detached)
	10883.pts-2.server2	(Detached)
	9700.pts-2.server2	(Detached)
3 Sockets in /var/run/screen/S-skaram.

[skaram@server2 ~]$ screen -rx 10883.pts-2.server2

But wait, there’s more! If you and a buddy, coworker, consultant, engineer, parole officer, etc. want to share the same screen, you can login as the same user via SSH and then both type screen -rx. You will both join the multiplexed session (screen) and be able to type and see what each other is typing. It is awesome for following along, mentoring, etc.

Conclusion

Good luck with these, and don’t do anything dangerous on production. If you have any of your own tips to share, feel free to leave a comment!

Share Button

10 comments

  1. I cannot count how many expensive consultants I have had to teach how to use xauth add.

    screen is priceless when logged in from home on a VPN with a timeout.

    Nice post!

  2. Re. Space hogs:

    (1) ‘du -sk | sort -nr | head’ gives you the Top 10 space hogs with the biggest ones first.

    (2) ‘du -sk *| sort -nr | head -25’ limits ‘du’ to the visible files/ directories and in this example gives you the top 25 entries of the results.

  3. I use “tmux” instead of “screen”. It is lighter, faster, has better support for resizeable panes.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.