The OpenNET Project / Index page

[ новости /+++ | форум | wiki | теги | ]

Поиск:  Каталог документации

3.11. System and Administrative Commands

The startup and shutdown scripts in /etc/rc.d illustrate the uses (and usefulness) of these comands. These are usually invoked by root and used for system maintenance or emergency filesystem repairs. Use with caution, as some of these commands may damage your system if misused.


Output system specifications (OS, kernel version, etc.) to stdout. Invoked with the -a option, gives verbose system info.

uname -a outputs something like:
Linux localhost.localdomain 2.2.15-2.5.0 #1 Sat Feb 5 00:13:43 EST 2000 i586 unknown


Sets an upper limit on system resources. Usually invoked with the -f option, which sets a limit on file size (ulimit -f 1000 sets a 1-meg limit on files).


Shows how long the system has been running, along with associated statistics.

bash$ uptime
10:28pm  up  1:57,  3 users,  load average: 0.17, 0.34, 0.27

Runs a program or script with certain environmental variables set or changed (without changing the overall system environment).


This command permits changing shell options on the fly. Works with version 2 of bash only.
shopt -s cdspell
# Allows misspelling directory names with 'cd' command.


This utility is part of the procmail package ( It creates a lock file, a semaphore file that controls access to a file, device, or resource. The lock file serves as a flag that this particular file, device, or resource is in use by a particular process ("busy"), and permitting only restricted access (or no access) to other processes. Lock files are used in such applications as protecting system mail folders from simultaneously being changed by multiple users, indicating that a modem port is being accessed, and showing that an instance of Netscape is using its cache. Scripts may check for the existence of a lock file created by a certain process to check if that process is running. Note that if a script attempts create a lock file that already exists, the script will likely hang.


Administrative program scheduler, performing such duties as cleaning up and deleting system log files and updating the slocate database. This is the superuser version of at. It runs as a daemon (background process) and executes scheduled entries from /etc/crontab.


CHange ROOT directory. Normally commands are fetched from $PATH, relative to /, the default root directory. This changes the root directory to a different one (and also changes the working directory to there). A chroot /opt would cause references to /usr/bin to be translated to /opt/usr/bin, for example. This is useful for security purposes, for instance when the system administrator wishes to restrict certain users, such as those telnetting in, to a secured portion of the filesystem. Note that after a chroot, the execution path for system binaries is no longer valid.

The chroot command is also handy when running from an emergency boot floppy (chroot to /dev/fd0), or as an option to lilo when recovering from a system crash. Other uses include installation from a different filesystem (an rpm option). Invoke only as root, and use with caution.


Show shared lib dependencies for an executable file.

bash$ ldd /bin/ls => /lib/ (0x4000c000)
/lib/ => /lib/ (0x80000000)

Show all users logged on to the system.

whoami is a variant of who that lists only the current user.


Show all logged on users and the processes belonging to them. This is an extended version of who. The output of w may be piped to grep to find a specific user and/or process.

bash# w | grep startx
grendel  tty1     -                 4:22pm  6:41   4.47s  0.45s  startx

Echoes the name of the current user's terminal. Note that each separate xterm window counts as a different terminal.

bash# tty

This is an acronym for "write all", i.e., sending a message to all users every terminal logged on in the network. It is primarily a system administrator's tool, useful, for example, when warning everyone that the system will shortly go down due to a problem.

wall System going down for maintenance in 5 minutes!

Identifies the processes (by pid) that are accessing a given file, set of files, or directory. May also be invoked with the -k option, which kills those processes. This has interesting implications for system security, especially in scripts preventing unauthorized users from accessing system services.


Appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger.
logger Experiencing instability in network connection at 23:10, 05/21.
# Now, do a 'tail /var/log/messages'.


Lists all jobs running in the background.


Shows currently running processes owned by user. Usually invoked as ps ax, showing all running processes.


Keeps a command running even after user logs off. The command will run as a foreground process unless followed by &. If you use nohup within a script, consider coupling it with a wait to avoid creating an orphan or zombie process.


Shows memory and cache usage in tabular form. The output of this command lends itself to parsing, using grep, awk or Perl.

bash$ free
                total       used       free     shared    buffers     cached
   Mem:         30504      28624       1880      15820       1608       16376
   -/+ buffers/cache:      10640      19864
   Swap:        68540       3128      65412

Forces writing all updated data from buffers to hard drive. While not strictly necessary, a sync assures the sys admin or user that the data just changed will survive a sudden power failure. In the olden days, a sync sync was a useful precautionary measure before a system reboot.


The init command is the parent of all processes. Called in the final step of a bootup, init determines the runlevel of the system from /etc/inittab. Invoked by its alias telinit, and by root only.


Symlinked to init, this is a means of changing the system runlevel, usually done for system maintenance or emergency filesystem repairs. Invoked only by root. This command can be dangerous - be certain you understand it well before using!


Shows the current and last runlevel, that is, whether the system is halted (runlevel 0), in single-user mode (1), in multi-user mode (2 or 3), in X Windows (5), or rebooting (6).

halt, shutdown, reboot

Command set to shut the system down, usually just prior to a power down.


This is actually a system call that replaces the current process with a specified command. It is mostly seen in combination with find, to execute a command on the files found. When used as a standalone in a script, this forces an exit from the script when the exec'ed command terminates. An exec is also used to reassign file descriptors. exec <zzz-file replaces stdin with the file zzz-file.

Example 3-59. Effects of exec


exec echo "Exiting $0."
# Exit from script.

# The following lines never execute.
echo "Still here?"

exit 0

Network interface configuration utility.


Show info about or make changes to the kernel routing table.


Show current network information and statistics, such as routing tables and active connections.


Creates block or character device files (may be necessary when installing new hardware on the system).


Mount a filesystem, usually on an external device, such as a floppy or CDROM. The file /etc/fstab provides a handy listing of available filesystems, including options, that may be automatically or manually mounted. The file /etc/mtab shows the currently mounted filesystems (including the virtual ones, such as /proc).
mount -t iso9660 /dev/cdrom /mnt/cdrom
# Mounts CDROM
mount /mnt/cdrom
# Shortcut, if /mnt/cdrom listed in /etc/fstab


Unmount a currently mounted filesystem. Before physically removing a previously mounted floppy or CDROM disk, the device must be umount'ed, else filesystem corruption may result.
umount /mnt/cdrom


List installed kernel modules.


Force insertion of a kernel module. Must be invoked as root.


Module loader that is normally invoked automatically in a startup script.


Creates module dependency file, usually invoked from startup script.


Get info about or make changes to root device, swap space, or video mode. The functionality of rdev has generally been taken over by lilo, but rdev remains useful for setting up a ram disk. This is another dangerous command, if misused.

Using our knowledge of administrative commands, let us examine a system script. One of the shortest and simplest to understand scripts is killall, used to suspend running processes at system shutdown.

Example 3-60. killall, from /etc/rc.d/init.d


# --> Comments added by the author of this HOWTO marked by "-->".

# --> This is part of the 'rc' script package
# --> by Miquel van Smoorenburg, <>

# --> This particular script seems to be Red Hat specific
# --> (may not be present in other distributions).

# Bring down all unneeded services that are still running (there shouldn't 
# be any, so this is just a sanity check)

for i in /var/lock/subsys/*; do
        # --> Standard for/in loop, but since "do" is on same line,
        # --> it is necessary to add ";".
	# Check if the script is there.
	[ ! -f $i ] && continue
	# --> This is a clever use of an "and list", equivalent to:
	# --> if [ ! -f $i ]; then continue

	# Get the subsystem name.
	# --> Match variable name, which, in this case, is the file name.
	# --> This is the exact equivalent of subsys=`basename $i`.
	# --> It gets it from the lock file name, and since if there
	# --> is a lock file, that's proof the process has been running.
	# --> See the "lockfile" entry, above.

	# Bring the subsystem down.
	if [ -f /etc/rc.d/init.d/$subsys.init ]; then
	    /etc/rc.d/init.d/$subsys.init stop
	    /etc/rc.d/init.d/$subsys stop
	# --> Suspend running jobs and daemons
        # --> using the 'stop' shell builtin.

That wasn't so bad. Aside from a little fancy footwork with variable matching, there is no new material there.

Exercise. In /etc/rc.d/init.d, analyze the halt script. It is a bit longer than killall, but similar in concept. Make a copy of this script somewhere in your home directory and experiment with it (do not run it as root). Do a simulated run with the -vn flags (sh -vn scriptname). Add extensive comments. Change the "action" commands to "echos".

Now, look at some of the more complex scripts in /etc/rc.d/init.d. See if you can understand parts of them. Follow the above procedure to analyze them. For some additional insight, you might also examine the file sysvinitfiles in /usr/doc/initscripts-X.XX, which is part of the "initscripts" documentation.

For those scripts needing a single do-it-all tool, a Swiss army knife, there is Perl. Perl combines the capabilities of sed, awk, and throws in a large subset of C, to boot. It is modular and contains support for everything ranging from object oriented programming up to and including the kitchen sink. Short Perl scripts can be effectively embedded in shell scripts, and there may even be some substance to the claim that Perl can totally replace shell scripting.

Example 3-61. Perl embedded in a bash script


perl -e 'print "This is an embedded Perl script\n"'

# Some shell commands may follow.

exit 0

Inferno Solutions
Hosting by

Закладки на сайте
Проследить за страницей
Created 1996-2022 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру