Open Source

Jack Wallen, Jr.'s one-hour Linux tutorial, part 2

In part 2 of this Daily Drill Down series for new to intermediate Linux users, Jack Wallen, Jr. explains how to safely shut down your machine, manage power with various applications, and modify and augment simple commands.


In part two of this Daily Drill Down series, I will revisit the Linux crash course and take our journey a little deeper into the realm of Linuxland. In part one, I explained what to do when you first start your new Linux machine, as well as how to run simple commands, get connected to the Internet, install via .rpm files, move around the desktop, and do a bit of customizing. This time around, I'm going to dive a bit deeper into the system and explain how to safely shut down your machine, manage power with various applications, and modify and augment simple commands. Again, this tutorial makes the same assumptions as part one:
  • Linux is already installed on your computer
  • This computer will be used as a desktop machine
  • You're using the Red Hat 6.1 Linux distribution

So grab your best geek wear and your favorite caffeinated beverage, put on some tunes, and let's learn!

Shutdown
The process of shutting down a Linux machine, like Windows, must follow certain steps in order to ensure data coherence. When a Linux machine shuts down there are certain steps that must be taken. The first step is that all logged in users (if it's a single desktop machine, then that's you) are notified that the system is going down, and login is blocked. Next, all processes are first notified that the system is going down by the signal SIGTERM (signal termination). This gives programs like vi the time to save the file being edited, and mail and news processing programs a chance to exit cleanly. The shutdown command does its job by signaling the init process and asking it to change the runlevel to 0. Runlevel 0 (configured in the /etc/inittab file) is used to halt the system, runlevel 6 is used to reboot the system, and runlevel 1 is used to put the system into a state where administrative tasks can be performed.

The format of the command is:
/sbin/shutdown [-t sec] [-arkhncfF] time [warning-message]

In order to run the shutdown command manually, you must bring up a console (aterm, eterm, rxvt, Xterm) and run the command with the necessary flags and the time in which you would like shutdown to occur. For general use, there are two common flags for shutdown: -r and -h. The –r flag tells the shutdown command that you are rebooting the system, which will lead the machine through a standard shutdown/startup process. The –h flag tells the system that you are halting the shutdown process or, in more common terms, shutting down the system.

You must also enter a time command for shutdown to take effect. The most common time for shutdown is now; so the command:
shutdown -r now

immediately reboots your machine. The command:
shutdown -h now

immediately shuts down your machine.

The time argument can have different formats. First, it can be an absolute time in the format hh:mm, in which hh is the hour (one or two digits) and mm is the minute of the hour (in two digits). Second, it can be in the format +m, in which m is the number of minutes to wait. The word now is an alias for +0. So the command:
shutdown -r 17:00

will shut down your machine at 17:00 hours (or 5:00 P.M.) and will broadcast a message to all users that the machine is going down at 17:00. When issuing shutdown to bring down the system at a later time, it's advisable to put the process in the background by adding the [&] character to the end of the command. By adding & to the command, you will still be able to use your machine (until the decided shutdown time). So now the command looks like:
shutdown -r 17:00 &

Once the shutdown command is issued, it's possible to cancel the process. Depending on whether the shutdown command is in the foreground (no &) or background (&), you can terminate shutdown by pressing [Ctrl][C] if the command is in the foreground and you have no prompt, or you can type shutdown -c if the command is in the background.

Power management
Power management is a topic of great debate within the Linux community. Although this topic primarily affects laptop users, it does have weight in the desktop community. I'll discuss power management in a general sense, but be aware that many of its uses and aspects will relate primarily to laptops.

Because most laptops are created with the assumption that Windows will be the OS, manufacturers use hardware that works in conjunction with Windows software. Take, for instance, Linux's worst nemesis… the WinModem. Most vendors use WinModems because they're cheap and because CPU speeds have risen to the point that the extra cycles the WinModems place on the CPU have little to no impact. Linux, however, can't work with WinModems.

Unfortunately, many of the power management features won't work with Linux. For this reason, it's best to take other precautions with your laptop; otherwise you'll end up with a dead battery, as I did after only three months of use.

This list of basic tips will help you reduce your laptop's power waste. These tips are from the Battery Powered Linux Mini-HOWTO.
  • Decrease or turn off your display's backlight when you don't need it.
  • Reducing the CPU clock speed will decrease power consumption. Several laptops offer a CPU clock selector that will toggle between normal and slow speed.
  • Turn off the CPU cooler. Many recent laptops offer a BIOS option called Cooling Control. If your system's CPU is becoming too hot, this option allows you to have it cooled by a tiny fan (setting Performance) or to have its CPU clock slowed down (setting Silence). To increase your uptime while using the battery, use Silence.
  • Avoid using external devices such as the printer, CRT (cathode-ray tube) screen, Zip drive, and portable camera with your computer while using the battery.
  • Avoid using any built-in device such as the floppy disk drive, hard disk, or CD-ROM unless absolutely necessary. Specifically, CD-ROM access will dramatically decrease your battery time.
  • PCMCIA cards can consume a great deal of power, so don't leave your modem or network adapter plugged in when it's not in use.
  • Use simple software. Using pico instead of StarOffice for editing simple text will save a great deal of battery life.
  • Newer versions of some upgradeable components consume less power.
  • If battery uptime is important, don't buy a laptop with a 2nd level cache. A computer with 2nd level cache is about 10 to 20 percent faster and it provides better performance with multimedia applications and number crunching, but it consumes a lot of power.
  • If uptime is critical, don't buy the latest, fastest CPU type. Manufacturers usually optimize older generation CPUs after some time—without notice. The "new" versions of old CPUs often create less heat and consume less power than the product's premiere version.
  • To have better control over laptop power management, take a look at the Advanced Power Management Daemon, which is a set of user-level applications that help to control the APM on both laptop and desktop computers. To learn more about this daemon, type man apmd.

Pay special attention to the above bullet point that suggests using simple software. Many of today's larger applications (specifically Microsoft applications, database applications, graphics manipulations, and browsers) consume a great deal of CPU energy, thereby sucking the life from the precious battery. Whenever you have an opportunity, substitute a simpler software package. Within Linux, this simple rule will get you far: ASCII is your friend! Working from simple text-based applications will drain your battery much more slowly than their GUI counterparts. But what can you substitute? Let's take a look:

Task GUI applications Text-based applications
Editing StarOffice, WordPerfect, Applixware pico, vi, emacs
E-mail Netscape, kmail, Arrow, eucalyptus pine, elm, mutt
Web browsing Netscape, StarOffice, kfm, Opera Lynx

If this seems like a small list, think about the typical workload placed upon a laptop. Many of the above GUI applications are much less draining than others, but going the console/text route is definitely the best way to go.

To GNOME or not to GNOME
With the advent of newer desktop environments such as GNOME and KDE, the Linux community has taken leaps and bounds ahead of most other computing environments. Unfortunately, such advances do have their downfalls. Like the Windows desktop environment, Linux desktop environments take a hit on the battery life. By employing these desktop environments, you'll use more of your machine's hardware, and therefore will use your battery faster. One way to get around this is to run your Linux laptop without a desktop environment.

Many of today's window managers are so full of features that you won't miss the desktop environment. AfterStep, WindowMaker, Enlightenment... they're all robust enough to be used solo. Of the three listed, Enlightenment takes the biggest hit on the machine's resources, so for a laptop, your best bet is to go with either AfterStep or WindowMaker. Take note, however, that you'll have to install a battery monitor separately because most window managers don't include battery monitors by default. There are many different window manager applets to use for power management monitoring. For instance, the Advanced Power Management Monitor for AfterStep is a tiny application (called an applet) that contains a readout of the percentage of battery power that's left, as well as how much time remains. The wmapm dockable applet for WindowMaker offers the same output as the AfterStep applet.

There are many more apm applets and applications for Linux. For example, a search of Freshmeat found the following results:

Searched for: Results
apm 11 hits
Power management 539 hits
Laptops 6 hits
Battery monitors 69 hits
Laptop monitors 77 hits

Although some of the hits were repeats, it's obvious that the Linux community has taken care to ensure that its many laptop users are covered. With these applets and applications, it's possible to run a lighter footprint GUI by eliminating the need for the desktop environment. Of course, certain elements won't be available to the window manager-only GUI, such as the drag-and-drop capabilities of GNOME and KDE; however, as a laptop user needing only the basics, this setup will suit you fine.

Getting the X Window System to boot only a window manager
In order to get your laptop to boot the window manager-only system, you must edit the .xinitrc file which can be found in your /home/USER directory. If this file doesn't exist, create it using your favorite text editor such as pico, vi, or emacs. The contents of this file will vary depending on which window manager you're using. Below you will find a listing of some of the more popular window manager entries for an .xinitrc file.

Window manager .xinitrc entry
AfterStep exec afterstep
WindowMaker exec wmaker
Enlightenment exec enlightenment
Fvwm2 exec fvwm2
Fvwm exec fvwm
evilwm exec evilwm
twm exec twm
icewm exec icewm
BlackBox exec blackbox

Although not a complete list, this gives you an idea on how to start your X session with the lightest possible footprint.

Modifying and augmenting simple commands
All Linux commands can be augmented and modified through the use of flags and arguments (these names are often either interchangeable or substituted with other such terms) that greatly enhance the usability and efficiency of the command. Many new users are stumped when it comes to command arguments and flags.

Arguments
There are two kinds of arguments: options and a file name or list of file names. An argument is sent with a command, often to inform the command what file to act on or how to act on the specified file. For example, the ls command (for listing files/directories and their attributes) can be run by typing ls. You can also add arguments to the command, such as ls -l, which is the long format—it returns permissions, ownership, size, and modification date of the entire directory in which you ran the command. However, suppose you don't want to see the entire contents of the directory and you want to list the attributes of only the file chump.txt. To do so, run:
ls -l chump.txt

The ls command above uses two arguments: -l, and chump.txt.

Command arguments aren't limited to a one-at-a-time instance. Let's say you wish to list all files in a directory, including hidden files (files that begin with a period [.]), and you also want:
  • Long lists.
  • The listing in human-readable form.
  • To only print one entry per line of output.
  • To list the files by status change time (not creation/modification time).
  • Times listed fully, not abbreviated.
  • The output in color.

This command would look like:
ls -alh -1 —full-time —color

When you run this command, you'll see entries such as:
-rw-r—r— 1 jlwallen jlwallen 2.1k Tue Dec 21 01:12:11 1999 .Xdefaults

Some commands require that all arguments be combined with a single [-], while others require each command have its own [-]. The man page for the individual command will highlight the requirement.

But how are you supposed to know what is an argument and what is a flag? Unfortunately, there are no definite rules. When viewing Linuxbox, you'll find the definition of a flag as being preceded by a hyphen, which would indicate that the entire al1h section we used with the above ls command would all be flags. However, the man page for ls (man ls) lists the command syntax as:
ls [OPTION]... [FILE]...

which would lead us to believe that our example contained options. In the case of ls, this will not work. When combining the alh1 arguments, the "1" overrides all other arguments. In order to get these arguments to work together, you must separate the "1" from the alh. As you can see, semantics isn't our friend, and our definitions aren't as cut and dried as we'd like them to be. Definitions aside, nearly all (if not all) commands can be sent with modifiers (to use a relatively safe term) that will either act on the command itself or on a specified file or directory.

Directing output of a command
It's also possible to direct the output of a command to a file in order to make reading and analyzing data more efficient and flexible. Say, for instance, you need the output of uptime (a simple application that lists how long your machine has been running) but you don't have time to read it (although the output is very brief) or you would like to catalog its output. To run uptime in the standard fashion, type uptime and you'll see output similar to:
11:14am up 33 days, 22:23, 1 user, load average: 0.00, 0.02, 0.03

Some admins like to keep a running diary of this output, which would append the output of each run of uptime to the end of a particular file. To see how this works, type touch uptime.diary to create an empty file called uptime.diary. Next, output your first run of uptime to the newly created file. To do this, type uptime > uptime.diary, then type less uptime.diary to see the first entry. To add the next entry or append the entry, you'll use the append operator, or >>:
uptime >> uptime.diary

Now when you run less uptime.diary you'll see two entries. Now let's say you want this file appended at a certain time or times each day. To do this, use cron to run the command and append the file daily. As root, cd to /etc/cron.daily, open your favorite text editor, and type the following:
#!/bin/sh
uptime >> /home/USERNAME/uptime.diary


(Replace USERNAME with the user name that you use to log on to your system.)

Another nice little trick will help the admin or user to examine output either at a different time or allow you to send the output to a fellow user. With the Linux mail application, you can e-mail the output of a command. By running:
uptime | mail thisguy@thisplace.com
you will e-mail the output of uptime to thisguy@thisplace.com. You could even combine the above with the at command and tell your machine to e-mail the output of uptime (or nearly any command) with:
at 5pm <press enter here>
uptime | mail thisguy@thisplace.com <press enter here>
<press ctrl-d>

Author's Note
In the above section, anytime it indicates <press enter here>, you must press the enter key and so forth.

Directing output to a printer
Being able to send a file directly to a printer with a simple command is often more desirable than opening an application in order to print. For this task, the lpr command (the offline printer which uses a spooler daemon) is perfect.

Let's say, for example, you have an ASCII table used as a simple database generated by a cron job and you want it printed out from a console-only Linux box. With a file named database, you would cd to the directory containing the file and run the command:
lpr database

This command will send the file to the spooler daemon, and out to the printer.

Let's say that you want to have this same file printed every day. Add an entry in /etc/cron.daily like:
lpr /path/to/file

Make sure the permissions are -rwxr-xr-x (as root, type chmod 755 FILENAME where filename is the name of the file you just created above) and you will have a daily printout.

Like all good Linux commands, you can use several arguments with the lpr command. The most useful of these arguments are:
  • -P: Forces output to a specific printer. Normally the default printer is used (site dependent), or the value of the environment variable PRINTER is used.
  • -h: Suppresses the printing of the burst page.
  • -m: Sends mail upon completion.
  • -r: Removes the file upon completion of spooling. Can't be used with the -s option due to security concerns.

Conclusion
This has been part two in an ongoing series of Daily Drill Downs that strives to make the new to intermediate user a bit more comfortable with the Linux operating system. If there's an aspect of Linux that's causing you grief, please feel free to drop me a line and I'll try to cover it in an upcoming tutorial.

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox