aria2.init 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2014-2016 nanpuyue <nanpuyue@gmail.com>
  3. # Copyright (C) 2016 kuoruan <kuoruan@gmail.com>
  4. START=99
  5. SERVICE_WRITE_PID=1
  6. SERVICE_DAEMONIZE=1
  7. append_params() {
  8. local p; local v; local s="$1"; shift
  9. for p in $*; do
  10. config_get v "$s" "$p"
  11. [ -n "$v" ] && (
  12. p=$(echo "$p" | sed -e 's|_|-|g');
  13. echo "$p=$v" >> $config_file
  14. )
  15. done
  16. }
  17. section_enabled() {
  18. local result
  19. config_get_bool result "$1" 'enabled' 0
  20. [ $result -eq 1 ]
  21. }
  22. option_disabled() {
  23. local result
  24. config_get_bool result "$1" "$2" 1
  25. [ $result -eq 0 ]
  26. }
  27. start_instance() {
  28. local s="$1"
  29. local user
  30. section_enabled "$s" || return 1
  31. config_get config_dir "$s" 'config_dir' '/var/etc/aria2'
  32. config_get dir "$s" 'dir'
  33. config_get user "$s" 'user'
  34. config_file="$config_dir/aria2.conf"
  35. session_file="$config_dir/aria2.session"
  36. dht_file="$config_dir/dht.dat"
  37. log_file="$config_dir/aria2.log"
  38. [ -d "$config_dir" ] || {
  39. mkdir -m 0755 -p "$config_dir"
  40. touch "$config_file"
  41. }
  42. [ -d "$dir" ] || {
  43. mkdir -m 0755 -p "$dir" # create download dir
  44. touch "$dir"
  45. }
  46. touch "$session_file" # create session file
  47. echo -e "enable-rpc=true\nrpc-allow-origin-all=true\nrpc-listen-all=true\nquiet=true" > $config_file
  48. echo -e "continue=true\ninput-file=$session_file\nsave-session=$session_file" >> $config_file
  49. option_disabled "$s" 'enable_dht' || echo "dht-file-path=$dht_file" >> $config_file
  50. option_disabled "$s" 'enable_log' || {
  51. [ -f "$log_file" ] && echo > $log_file # if log file exist, clear it
  52. echo -e "log=$log_file" >> $config_file
  53. }
  54. # if user is set, change dir owner
  55. [ -z "$user" ] || {
  56. chown -R $user:$user $config_dir
  57. chown -R $user:$user $dir
  58. }
  59. append_params "$s" \
  60. file_allocation bt_enable_lpd enable_dht rpc_user rpc_passwd rpc_listen_port dir bt_tracker disk_cache \
  61. max_overall_download_limit max_overall_upload_limit max_download_limit max_upload_limit max_concurrent_downloads \
  62. max_connection_per_server min_split_size split save_session_interval follow_torrent listen_port bt_max_peers \
  63. peer_id_prefix user_agent rpc_secret log_level
  64. config_list_foreach "$s" extra_settings append_extrasettings
  65. SERVICE_UID="$user" \
  66. service_start /usr/bin/aria2c --conf-path="$config_file" # start service
  67. }
  68. append_extrasettings() {
  69. echo "$1" >> $config_file
  70. }
  71. start() {
  72. logger -t ARIA2C 'Starting aria2c service'
  73. config_load 'aria2'
  74. config_foreach start_instance 'aria2'
  75. return 0
  76. }
  77. stop() {
  78. if [ -n "`pidof aria2c`" ]; then
  79. logger -t ARIA2C 'Shutting down aria2c service'
  80. service_stop /usr/bin/aria2c
  81. fi
  82. return 0
  83. }
  84. restart() {
  85. logger -t ARIA2C 'Restarting aria2c service'
  86. stop
  87. sleep 2 # give time to shutdown
  88. start
  89. }