1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# daemon-ctl 1.1, 2001/02/17 (dws) |
4 |
|
5 |
use strict; |
6 |
|
7 |
my $action = shift @ARGV; |
8 |
my $name = shift @ARGV; |
9 |
my @args = @ARGV; |
10 |
|
11 |
my $PIDDIR='/var/run'; |
12 |
my $PIDFILE="$PIDDIR/$name.pid"; |
13 |
|
14 |
-d "$PIDDIR" || mkdir("$PIDDIR",755) || die "$0: can't mkdir $PIDDIR\n"; |
15 |
|
16 |
my $usage=1; |
17 |
if($action eq 'stop' || $action eq 'restart') { |
18 |
print "stopping $name..."; |
19 |
if(open(PID, "<$PIDFILE")) { |
20 |
my $pid=<PID>; |
21 |
chomp $pid; |
22 |
close(PID); |
23 |
kill("TERM", $pid); |
24 |
unlink "$PIDFILE" or warn "$0: can't remove $PIDFILE\n"; |
25 |
print " ok.\n"; |
26 |
} |
27 |
else { |
28 |
print " no pid.\n"; |
29 |
}; |
30 |
$usage=0; |
31 |
} |
32 |
if($action eq 'start' || $action eq 'restart') { |
33 |
print "starting $name..."; |
34 |
if(! -x $args[0]) { |
35 |
print " $args[0] not found.\n"; |
36 |
} |
37 |
else { |
38 |
my $pid=fork; |
39 |
if($pid) { |
40 |
open(PID, ">$PIDFILE") or die "$0: can't write to $PIDFILE\n"; |
41 |
print PID "$pid\n"; |
42 |
close(PID); |
43 |
print " ok.\n"; |
44 |
} |
45 |
else { |
46 |
exec(@args) or die "$0: can't exec @args\n"; |
47 |
} |
48 |
} |
49 |
$usage=0; |
50 |
} |
51 |
if($action eq 'getpid') { |
52 |
open(PID, "<$PIDFILE") or do { |
53 |
print "$0: can't open $PIDFILE\n"; |
54 |
exit 1; |
55 |
}; |
56 |
my $pid=<PID>; |
57 |
print "$pid"; |
58 |
$usage=0; |
59 |
} |
60 |
if($usage) { |
61 |
print "usage: $0 {start|restart|stop|getpid}\n"; |
62 |
} |
63 |
|
64 |
exit 0; |
65 |
|
66 |
__END__ |
67 |
|
68 |
=head1 NAME |
69 |
|
70 |
daemon-ctl - start/stop a program in the background |
71 |
|
72 |
=head1 SYNOPSIS |
73 |
|
74 |
daemon-ctl I<action> I<name> I<program> [I<args ...>] |
75 |
|
76 |
=head1 DESCRIPTION |
77 |
|
78 |
This program is a init-script helper. It starts/stops a program in the |
79 |
background by using a file (in F</var/run>) to store the PID of the started |
80 |
program. |
81 |
|
82 |
I<action> can be one of: |
83 |
|
84 |
=over 10 |
85 |
|
86 |
=item start |
87 |
|
88 |
Start the program I<program> with it's arguments I<args> and store it's pid in |
89 |
F</var/run/name.pid>. |
90 |
|
91 |
=item stop |
92 |
|
93 |
Stop the program by sending a TERM signal to the process written in |
94 |
F</var/run/name.pid>. |
95 |
|
96 |
=item restart |
97 |
|
98 |
Do a stop and immediately a start. |
99 |
|
100 |
=item getpid |
101 |
|
102 |
Print the PID of the program. |
103 |
|
104 |
=head1 EXAMPLE |
105 |
|
106 |
#!/bin/sh -e |
107 |
|
108 |
DAEMONCTL=/usr/drwho/vault/isgptp/bin/daemon-ctl |
109 |
GEDAFED=/usr/drwho/vault/isgptp/bin/gedafed |
110 |
|
111 |
test -f $DAEMONCTL || exit 0 |
112 |
|
113 |
$DAEMONCTL $1 gedafed $GEDAFED |
114 |
|
115 |
=head1 AUTHOR |
116 |
|
117 |
David Schweikert <dws@ee.ethz.ch> |