Issue #5 March 2005

Tips & Tricks

Red Hat's customer service team receives technical support questions from users all over the world. As they are received, Red Hat technicians add the questions and answers to the Red Hat Knowledgebase on a daily basis. Individuals with a redhat.com login are granted limited access. Red Hat Enterprise Linux subscribers are granted full access — with expanded categories and advanced search capabilities. Every month, Red Hat Magazine offers a preview into the Red Hat Knowledgebase by highlighting some of the most recent entries.

Command line scripts can not only impress your friends, but it can also save you valuable time and effort. An easy to use command can perform this task in seconds instead of minutes or hours. Last month's article How I learned to stop worrying and love the command line, part 1 and this month's article How I learned to stop worrying and love the command line, part 2 prove this. This month's Tips & Tricks unveil some simple yet powerful Linux commands anyone can add to their arsenal.

Tips from RHCEs

Interrupting those pesky "uninterruptible" processes

Anyone whose dealt with NFS administration has sooner or later had to confront a downed server that results in a hoard of unkillable processes that build up waiting for the NFS server to return. A quick glance at the NFS mount options in mount(8) will tell us we can mount with the "intr" or "soft" options to instruct our client to allow interrupts or to time out in the case of an unresponsive server, but what if we didn't anticipate it? Or what if we chose to use uniterruptible for a reason such as to avoid risks to data integrity if the application is writing over NFS?

Often the advice you'll find in newsgroups is to stop these processes is to simply reboot the machine, but even that can be problematic since a system shutdown can be hampered by lingering dead NFS mounts.

The reason these processes are unkillable is because it's not actually the process that is running, but rather the kernel is interacting with a device or filesystem on the processes behalf. If you've ever tried to mount a damaged floppy or CD and had the mount command block, then you've run into this. In those cases the solution is simply to eject the media; cause the subsystem to fail and the hung process will then error and exit.

In the case of NFS, the subsystem is handled by a kernel thread called rpciod. The quick and dirty way is the old kill -9 <pid> on the rpciod kernel thread (or killall -9 rpciod). Don't worry, it won't actually die, but it will respond to the signal by delivering errors to processes waiting on it. A more elegant approach is to simply cause rpciod to decide it is not worth while to continue its attempts to reach the server. Bringing down your network interface or setting a firewall rule that will reject with "icmp-port-unreachable" will do the trick... just be patient, it can take a few minutes before rpciod gives up, but it will give up, even with hard mounts. Assuming the offending server was 192.168.0.5, inserting this rule on the client should do the trick.

iptables -I OUTPUT -d 192.168.0.5 -j REJECT

How do I find the prime factors of a certain number?

Using the factor command its easy to find out the prime factors of any number. Make sure that the coreutils package is installed in order to use the factor command. It could be acquired either from the Red Hat Enterprise Linux CDs or from Red Hat Network (RHN) using up2date coreutils from a system registered with RHN.

To use the factor command:

factor <number>

For example:

factor 210

produces the results:

210: 2 3 5 7

If a number is specified on the command line, factor reads numbers from standard input delimited by newlines, tabs, or spaces.

How do I determine the architecture of an installed package?

A query with the rpm -q command does not display architecture by default. Use rpm with these options to view the architecture:

rpm -q --queryformat="%{n}-%{v}-%{r}.%{arch}.rpm\n" <package-name>

Replace <package-name> with the name of the package that needs to be queried. If unsure of the package name, the same --queryformat option can be used in conjunction with the rpm -qa command piped through grep to search for all packages whose names match the specified string:

rpm -qa --queryformat="%{n}-%{v}-%{r}.%{arch}.rpm\n" | grep <package name>

How can I tell when an RPM package was installed on my system?

You can use the rpm command to achieve this. To get a list of all installed packages along with the date and time they were installed, use the command:

rpm -qa --last

For information about a specific package, specify the package_name in the command:

rpm -q --last >package-name<

For example, you can use this command to look at the filesystem package:

rpm -q --last filesystem

which returns:

filesystem-2.2.1-3                            Wed 07 Jan 2004 05:30:57 AM EST

How do I copy file and directories across to another server and preserve symlinks?

To preserve symlinks while copying files and directories across a network the use of the rsync command is recommended. If scp is used, the symlinked file is copied to the remote directory, and the link relationship is lost.

For example, to copy and preserve the symlink relationship between test.log and /home/test/test.log in the local /rsync_test directory to machine serverOne's /rsync_test directory:

 
rsync -a -e ssh /rsync_test/ serverOne:/rsync_test

How do I find out who is currently logged in to the system?

There are several ways to monitor who is currently logged into a system. One of which is to use the command last.

The last command displays all users logged into a system along with their current ttys. However, it only displays login information starting from the time /var/log/wtmp was created. Thus, make sure /var/log/wtmp exists with the command:

touch /var/log/wtmp

Another command related to last is lastb. The difference is that the latter only displays all bad login attempts provided that the /var/log/btmp file exists.

Meanwhile, the command w is also very useful. The w command shows who is currently logged on and what they are doing (process wise). The following entries are displayed for each user: login name, tty name, remote host, login time, idle time, JCPU, PCPU, and the command of the user's current process.

The JCPU time is the time used by all processes attached to the tty. It does not include past background jobs but does include currently running background jobs.

The PCPU time is the time used by the current process which is listed in the "what" field.

This article is protected by the Open Publication License, V1.0 or later. Copyright © 2004 by Red Hat, Inc.