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_scripts

On 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 service

If 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.

  • If the service has a startup script, run the script with the stop argument:

     /path/to/startup_script stop 

  • If the service uses its own control script (you'll find out about this in the service's man pages), follow the instructions in the service's man pages. They usually have a stop script, or a script that accepts a stop or exit argument.

  • If you can't find any information about cleanly stopping a service and you really need it to stop, use the kill command to send it a terminate signal:

     kill -TERM pid 

    How do you find the process ID, pid? Using ps and grep (Code Listing 5.4):

     ps -ax | grep service_name 

    The first column of the output is the PID, and you can ignore the line with your grep command in it.

  • Some systems (including Fedora Core and FreeBSD) have a pkill command that will kill a process by name:

     pkill -TERM ntpd 

  • Fedora Core and Mac OS X have a service command, which will send a command to the named service:

     service ntpd stop 

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.

  • If the service has a startup script, run the script with the start argument:

     /path/to/startup_script start 

  • If the service uses its own control script (you'll find out about this in the service's man pages), follow the instructions in the service's man pages. They usually have a start script, or a script that accepts a start or run argument.

  • If you can't find any information about properly starting a service, you can probably run it directly from the command line:

     /path/to/service & 

    Check the service's man pages for a -d or -D option (usually marked "run as daemon" in the documents) and any other useful arguments you might need to include.

To restart a running service

There are two ways to restart a running service.

  • kill -HUP service_pid

    Sending 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 restart

    Modern 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

  • Fedora Core has a nice graphical service editing tool created by Red Hat, the Service Configuration tool (Figure 5.1). You can start this by going to the Applications menu in the top left of the screen and choosing System Settings > Server Settings > Services.

    Figure 5.1. Red Hat's Service Configuration tool lets you modify the running services using a graphical interface.


  • If you've installed a Cygwin daemon as a Windows service (using the cygrunsrv command), you can use the Services item in the Administrative Tools folder of Control Panel (Figure 5.2). Scroll down the list until you find the service you're interested in, then right-click it and choose your desired action from the contextual pop-up menu.

    Figure 5.2. The Services control panel application under Windows lets you control the myriad services installed on a modern Windows system.


  • Mac OS X services can mostly be controlled through the Sharing pane of System Preferences (Figure 5.3). Simply select the desired service, and then click the Start/Stop button.

    Figure 5.3. The Sharing pane in the Mac OS X System Preferences controls several standard Internet services.


  • Need to kill multiple copies of a process? Try this:

     ps ax | grep process_name |  awk -e '{ print $1; }' |  xargs -n 1 kill -TERM 

    This ps command lists all of the processes currently running on your system. grep then picks out the ones that match process_name, and awk takes those and prints only the process IDs. The xargs command takes those PIDs and fires off one kill -TERM command per PID to tell each instance of the service to exit.

  • For a pesky service that won't exit with the TERM signal, use KILL instead. The advantage of using TERM is that it gives the program a chance to clean up after itself. With KILL, it just exits immediately.




    Unix Advanced. Visual QuickPro Guide
    Unix Advanced: Visual QuickPro Guide
    ISBN: 0321205499
    EAN: 2147483647
    Year: 2003
    Pages: 116

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net