File: init.d

package info (click to toggle)
apt-p2p 0.1.6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 9,260 kB
  • ctags: 986
  • sloc: python: 6,744; sh: 122; makefile: 46
file content (121 lines) | stat: -rwxr-xr-x 3,221 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/sh

### BEGIN INIT INFO
# Provides:          apt-p2p
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs
# Should-Start:      $named
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: apt helper for peer-to-peer downloads of Debian packages
# Description:       Apt-p2p is a helper for downloading Debian packages
#                    files with APT. It will download any needed files from
#                    other Apt-p2p peers in a bittorrent-like manner, and so
#                    reduce the strain on the Debian mirrors.
### END INIT INFO

# /etc/init.d/apt-p2p: start and stop the apt-p2p daemon

PATH=/sbin:/bin:/usr/sbin:/usr/bin

rundir=/var/run/apt-p2p/ 
pidfile=$rundir/apt-p2p.pid 
logfile=/var/log/apt-p2p.log
application=/usr/sbin/apt-p2p
twistd=/usr/bin/twistd
user=apt-p2p
group=nogroup
enable=true

[ -r /etc/default/apt-p2p ] && . /etc/default/apt-p2p

test -x $twistd || exit 0
test -r $application || exit 0

case "x$enable" in
    xtrue|xfalse)   ;;
    *)              echo -n "Value of 'enable' in /etc/default/apt-p2p must be either 'true' or 'false'; "
                    echo "not starting apt-p2p daemon."
                    exit 1
                    ;;
esac

# return true if at least one pid is alive
alive()
{
    if [ -z "$*" ]; then
        return 1
    fi
    for i in $*; do
        if kill -0 $i 2> /dev/null; then
            return 0
        fi
    done

    return 1
}


case "$1" in
    start)
        if "$enable"; then
            echo -n "Starting apt-p2p"
            [ ! -d $rundir ] && mkdir $rundir
            [ ! -f $logfile ] && touch $logfile
            chown $user $rundir $logfile 
            [ -f $pidfile ] && chown $user $pidfile
            # Make cache files readable
            umask 022
            start-stop-daemon --start --quiet --exec $twistd -- \
                --pidfile=$pidfile --rundir=$rundir --python=$application \
                --logfile=$logfile --no_save
            echo "."        
        else
            echo "apt-p2p daemon not enabled in /etc/default/apt-p2p, not starting..."
        fi
    ;;

    stop)
        echo -n "Stopping apt-p2p"
        start-stop-daemon --stop --quiet --pidfile $pidfile
        #
        # Continue stopping until daemon finished or time over
        #
        count=0
        pid=$(cat $pidfile 2>/dev/null)
        while alive $pid; do
                if [ $count -gt 20 ]; then
                        echo -n " aborted"
                        break;
                elif [ $count = 1 ]; then
                        echo -n " [wait $count"
                elif [ $count -gt 1 ]; then
                        echo -n " $count"
                fi
                count=$(expr $count + 1)
                sleep 1
                start-stop-daemon --stop --quiet --pidfile $pidfile
        done
        if [ $count -gt 1 ]; then
                echo -n "]"
        fi
        echo "."        
    ;;

    restart)
        $0 stop
        $0 start
    ;;
    
    force-reload)
        $0 restart
    ;;

    *)
        echo "Usage: /etc/init.d/apt-p2p {start|stop|restart|force-reload}" >&2
        exit 1
    ;;
esac

exit 0