Controlling Services To live with services that run all the time (as opposed to the ones that are started when necessary by a super-server that monitors network ports (such as inetd and xinetd), you'll need to know how to start and stop them without rebooting the system. Reboots mean downtime, and that's not a good thing in the server world.Code listing 5.1. Using the find command to look for xinetd's startup script.
[chrish@dhcppc0 ~]$ cd /etc/rc.d/init.d [chrish@dhcppc0 init.d]$ find . -name
\*inetd\* ./xinetd
Code listing 5.2. Searching for inetd's startup script on FreeBSD.
freebsd# cd /etc/rc.d freebsd# find . -name \*inetd\* ./inetd
To find a service's control script If a service has a control script, it will be installed in the standard startup-script directory, along with the service's configuration scripts, or with the service's program itself.
1. | Log in as root, or use su (or sudo) to get a root shell.Unless you've started the service from your normal user account, you'll need root's power to stop or start the service. | 2. | cd path_to_startup_scriptsOn Fedora Core Linux (and other Unixes that follow the lead of AT&T's System V), the path_to_startup_scripts is /etc/rc.d/init.d (Code Listing 5.1), with symbolic links made into each of the rc.d directories for specific run levels.On FreeBSD, the path_to_startup_scripts is /etc/rc.d (Code Listing 5.2).On Cygwin, daemons are usually started as Windows services (see the "Tips" section below), or from inetd or xinetd.On Mac OS X, the path_to_startup_scripts is /System/Library/StartupItems or /Library/StartupItems (Code Listing 5.3). Look in both directories, but keep in mind that /Library/StartupItems takes precedence over the /System directory. | 3. | find . -name \*service\*Use the find command to search the startup-script directory and any sub directories for a script named after the service you're interested in.Note that a simple ls might also do the job, unless there are a huge number of services on your system.Mac OS X systems might use different names (such as SystemLog instead of syslogd) for the startup scripts. | 4. | man serviceIf you haven't found anything in the startup-script directory, use the service's man pages to find out where its control scripts are, if any exist. |
Code listing 5.3. Looking for startup scripts on Mac OS X can be more of a challenge, because the names have changed slightly.
bender:~ chrish$ cd /System/Library/StartupItems bender:/System/Library/StartupItems chrish$ ls AMD CoreGraphics LoginWindow Postfix Accounting CrashReporter NFS PrintingServices Apache Cron NIS RemoteDesktopAgent AppServices DirectoryServices NetInfo SNMP AppleShare Disks Network SecurityServer AuthServer IPServices NetworkExtensions SystemLog BIND KernelEventAgent NetworkTime SystemTuning ConfigServer LDAP Portmap DNSResponder bender:/System/Library/StartupItems chrish$ find . -name \*Log\* ./LoginWindow ./LoginWindow/LoginWindow ./SystemLog ./SystemLog/SystemLog
To stop a running service Stopping a running service depends on how it was started.
Code listing 5.4. Using the ps command and grep to find the process ID of the NTP daemon.
bender:~ chrish$ ps ax | grep ntpd 321 ?? Ss 0:00.14 ntpd -f
/var/run/ntp.drift -p /var/run/ntpd.pid 647 std R+ 0:00.00 grep ntpd bender:~ chrish$
To start a service Starting a service depends on whether it has a startup script.
To restart a running service There are two ways to restart a running service.- kill -HUP service_pidSending the HUP signal to a service usually tells it to reload its configuration and restart. I say "usually" because this is only a convention, not a requirement. Check the man page for your service to be sure. You can generally find the service_pid using the ps command and less or grep. You might also find the service's program ID in /var/run/service.pid.
- /path/to/startup
scripts/service_script restartModern Unix systems all use scripts to launch services during system startup (see Chapter 2), and the restart option will restart a running service. If restart isn't supported on your system, use stop and then start to have the same effect.
Tips
|