前面说到了使用原码编译 transmission-daemon qbittorrent-nox nginx ,但是需要自己配置启动脚本。

transmission-daemon

之前使用 apt 安装的 tr-d ,虽然很容易可以使用 sudo systemctl enable|start|stop|status transmission-daemon,但是不得不忍受一个很长很傻的运行用户名,而且修改起来特别麻烦,文件权限也是乱的很。

 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
sudo vim /etc/init.d/transmission-daemon

# 添加下面的脚本,并给执行权限

#!/bin/sh -e

### BEGIN INIT INFO
# Provides:          transmission-daemon
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start or stop the transmission-daemon.
# Description:       Enable service provided by transmission-daemon.
### END INIT INFO

NAME=transmission-daemon
DAEMON=/usr/bin/$NAME
USER=rachpt
STOP_TIMEOUT=30

export PATH="${PATH:+$PATH:}/sbin"

[ -x $DAEMON ] || exit 0

[ -e /etc/default/$NAME ] && . /etc/default/$NAME

. /lib/lsb/init-functions

start_daemon () {
    if [ $ENABLE_DAEMON != 1 ]; then
        log_progress_msg "(disabled)"
		    log_end_msg 255 || true
    else    
        start-stop-daemon --start \
        --chuid $USER \
		$START_STOP_OPTIONS \
        --exec $DAEMON -- $OPTIONS || log_end_msg $?
		log_end_msg 0
    fi
}

case "$1" in
    start)
        log_daemon_msg "Starting bittorrent daemon" "$NAME"
        start_daemon
        ;;
    stop)
        log_daemon_msg "Stopping bittorrent daemon" "$NAME"
        start-stop-daemon --stop --quiet \
            --exec $DAEMON --retry $STOP_TIMEOUT \
            --oknodo || log_end_msg $?
        log_end_msg 0
        ;;
    reload)
        log_daemon_msg "Reloading bittorrent daemon" "$NAME"
        start-stop-daemon --stop --quiet \
            --exec $DAEMON \
            --oknodo --signal 1 || log_end_msg $?
        log_end_msg 0
        ;;
    restart|force-reload)
        log_daemon_msg "Restarting bittorrent daemon" "$NAME"
        start-stop-daemon --stop --quiet \
            --exec $DAEMON --retry $STOP_TIMEOUT \
            --oknodo || log_end_msg $?
        start_daemon
        ;;
    status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
    *)
        log_action_msg "Usage: /etc/init.d/$NAME {start|stop|reload|force-reload|restart|status}" || true
        exit 2
        ;;
esac

exit 0

文件的开头 ### 部分为LSB tags,格式比较固定,可以参考其他的修改,如果没有会有如下类似报错:

1
2
3
4
ssh.service is not a native service, redirecting to systemd-sysv-install
Executing /lib/systemd/systemd-sysv-install enable ssh
insserv: warning: script 'ssh' missing LSB tags and overrides
update-rc.d: error: ssh Default-Start contains no runlevels, aborting.

[参考文档tps://my.oschina.net/apis/blog/1647729?p=1)。

使用命令:

1
2
3

sudo update-rc.d transmission-daemon defaults

更新 system 脚本。

至此不出意外就可以使用

1
sudo service transmission-daemon stop|start|status

开机启动

前面的 service 可以一系统服务的方式启动 daemon,而且可以指定运行的用户名,但会不能开机自启动。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
cd /etc/systmd/system

vim transmission-daemion.service

# 填入

[Unit]
Description=Transmission BitTorrent Daemon
After=network.target

[Service]
User=rachpt
Type=simple
ExecStart=/usr/bin/transmission-daemon -f --log-error --logfile /var/log/transmission-daemon.log
ExecReload=/bin/kill -s HUP $MAINPID

[Install]
WantedBy=multi-user.target

在这里卡了半天,无法成功,-f 好像很重要,删除后无法运行,无法设置开机运行。

1
2
3
sudo systemctl daemon-reload

sudo systemctl enable transmission-daemon
1
2
# 查考所有服务
sudo systemctl list-unit-files

记得备份所有 transmission-daemon 目录下的 resume 和 torrent 等文件目录。 血的教训。

nginx

service 脚本参考 此文

开机启动

 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
# 直接复制官方的


vim /etc/systemd/system/nginx.service


# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

注意路径,其他和 transmission-daemon 一样。

qbittorrent

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19

vim /etc/systemd/system/qbittorrent.service
# 或者 /lib/systemd/system/qbittorrent.service

[Unit]
Description=qBittorrent Daemon Service
After=network.target

[Service]
User=rachpt
Type=forking
RemainAfterExit=yes
ExecStart=/usr/bin/qbittorrent-nox -d
ExecStop=/usr/bin/killall -w qbittorrent-nox
Restart=on-abort
Nice=19

[Install]
WantedBy=multi-user.target

好像不用 init.d 里面运行脚本。

flexget

这东西也可以 开机自动已 daemon 运行。

参考文档

在此 copy 过来。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 新建一个配置文件

sudo vim /etc/default/flexget

# Configuration for /etc/init.d/flexget

# User to run flexget as.
# Daemon will not start if left empty.
FGUSER="rachpt"

# Full path to the flexget config.yml file to use.
# Defaults to FGUSER $HOME/.flexget/config.yml
CONFIG="/home/rachpt/.config/flexget/config.yml"

# Path to the directory where flexget should log. Do not add trailing slash.
# Defaults to the FGUSER $HOME/.flexget directory
LOG="/home/rachpt/.config/flexget"

# Log verbosity 
# Available options : none critical error warning info verbose debug trace
# Defaults to info
LEVEL="info"

建运行脚本 /etc/init.d/flexget

  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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash

### BEGIN INIT INFO
# Provides:          flexget
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Flexget
# Description:       FlexGet is a multipurpose automation tool 
#                    for content like torrents, nzbs, podcasts,
#                    comics, series, movies, etc.
### END INIT INFO

# Author: Antoine Joubert, 19/01/2014

NAME="flexget"
DAEMON="/usr/local/bin/flexget"
SETTINGS="/etc/default/$NAME"

DESC="Flexget"
PIDFILE="/var/run/$NAME.pid"

set -e

. /lib/lsb/init-functions

unset FGUSER CONFIG LOG LEVEL

# Exit if flexget not installed
if [ ! -x "$DAEMON" ]; then
        log_action_msg "$DESC: Could not find flexget executable. Exiting."
        exit 2
fi

# Read configuration variables
if [ -r /etc/default/$NAME ]; then
        . /etc/default/$NAME
else
        log_action_msg "$DESC: /etc/default/$NAME not found. Exiting."
        exit 2
fi

# Exit if FGUSER has not been set in /etc/default/flexget
if [ -z $FGUSER ]; then
        log_action_msg "$DESC: FGUSER not set in /etc/default/$NAME. Exiting."
        exit 2
fi

# Function to verify if flexget is already running
run_check() {
        if [ -e $PIDFILE ]; then
               status_of_proc -p $PIDFILE $DAEMON $NAME > /dev/null && RETVAL=0 || RETVAL="$?"
        else
                RETVAL="2"
        fi
}

end_log() {
        if [ $RETVAL -eq 0 ]; then
                log_end_msg 0
                return 0
        else
                log_end_msg 1
                exit 1
        fi
}

# Function to define config file, log file and log level
conf_check() {
        if [ -z "$CONFIG" ]; then
                OPTIONS="$OPTIONS"
        else
                OPTIONS="-c $CONFIG"
        fi
        if [ -z "$LOG" ]; then
                OPTIONS="$OPTIONS"
        else
                OPTIONS="$OPTIONS -l $LOG/flexget.log"
                if [ ! -d "$LOG" ]; then 
                        mkdir -p -m 750 $LOG
                        chown $FGUSER $LOG
                fi
        fi

        if [ -z $LEVEL ]; then
                OPTIONS="$OPTIONS"
        else
                OPTIONS="$OPTIONS -L $LEVEL"
        fi
}

start_flexget() {
        run_check
        if [ $RETVAL = 0 ]; then
                log_action_msg "$DESC: Already running with PID $(cat $PIDFILE). Aborting."
                exit 2
        else
                conf_check
                log_daemon_msg "$DESC: Starting the daemon."
                start-stop-daemon --start --background --quiet --pidfile $PIDFILE --make-pidfile --chuid $FGUSER \
                --user $FGUSER --exec $DAEMON -- $OPTIONS daemon start
                RETVAL=$?
                end_log
        fi
}

stop_flexget() {
        run_check
        if [ $RETVAL = 0 ]; then
                log_daemon_msg "$DESC: Stopping the daemon."
                start-stop-daemon --stop --quiet --chuid "$FGUSER" --pidfile "$PIDFILE" --retry 30
                RETVAL=$?
                [ -e "$PIDFILE" ] && rm -f "$PIDFILE"
                end_log
        else
                log_action_msg "$DESC: Not currently running. Aborting."
                exit 2
        fi
}

status_flexget() {
        run_check
        if [ $RETVAL = 0 ]; then
                log_action_msg "$DESC: Currently running with PID $(cat $PIDFILE)."
        else
                log_action_msg "$DESC: Not currently running."
        fi
        exit $RETVAL
}

case "$1" in
        start)
                start_flexget
        ;;
        stop)
                stop_flexget
        ;;
        restart)
                stop_flexget && sleep 2 && start_flexget
        ;;
        status)
                status_flexget
        ;;
        *)
                echo "Usage: $0 {start|stop|restart|status}"
esac

exit 0

记得加执行权限。

1
sudo update-rc.d flexget defaults

建立 flexget.service 文件。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
sudo vim /etc/systemd/system/flexget.service 

[Unit]
Description=Flexget Daemon
After=network.target

[Service]
User=rachpt
Type=simple
WorkingDirectory=/home/rachpt/.config/flexget
ExecStart=/usr/local/bin/flexget daemon start
ExecStop=/usr/local/bin/flexget daemon stop
ExecReload=/usr/local/bin/flexget daemon reload

[Install]
WantedBy=multi-user.target

其他和前面一样,不再重复。

使用 pidtree 可以看到所有进程的父进程。