summarylogtreecommitdiffstats
path: root/JDownloaderHeadlessCtl
blob: 06de4b7f478dac545220368e7853c05446333d65 (plain)
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
#!/usr/bin/env bash
set -u

THIS_PATH="$(dirname "$(readlink -f "$0")")"

source "$THIS_PATH/functions.sh"
changePath

#Common variable for the pid of running instance (initially none)
JDPID=0

#check if jdownloader is running (0=yes, 2=no). This function sets environment variable JDPID
function getStatus(){
  if [ -f "JD2.lock" ]; then
    FUSER_STR="$(fuser "JD2.lock" 2>/dev/null)"
    if [ $? -eq 0 ]; then
      # get the PID
      JDPID="$(echo "$FUSER_STR" | rev | cut -f 1 -d ' ' | rev)"
      return 0
    else
      return 2
    fi
    
  else
    return 2
  fi  
}



ACTION="${1:-}"

if [ -z "$ACTION" ]; then
  echo "$0 start/stop/status"
  exit 2 
fi

getStatus
JDRUNS=$?

if [ "$ACTION" = "status" ]; then
  if [ $JDRUNS -eq 0 ]; then
    echo "JDownloader is running under PID $JDPID"
    exit 0
  else
    echo "JDownloader is currently not running"
    exit 2
  fi
elif [ "$ACTION" = "start" ]; then
  if [ $JDRUNS -eq 0 ]; then
    echo "JDownloader is already running under PID $JDPID"
    exit 0
  fi
  if [ "$JD_SCOPE" = "global" ]; then
    if [ -t 0 ] && isRoot ; then
      systemctl status jdownloader >/dev/null
      if [ $? -ne 0 ] || isRoot ; then
        echo "redirecting to systemctl..."
        systemctl start jdownloader
        exit $?
      fi
    fi
  fi
  
  
  /usr/bin/JDownloaderHeadless --daemon
  exit $?
elif [ "$ACTION" = "stop" ]; then
  if [ $JDRUNS -ne 0 ]; then
    echo "JDownloader is not running"
    exit 2
  fi
  if [ "$JD_SCOPE" = "global" ]; then
    if [ -t 0 ] && isRoot ; then
      systemctl status jdownloader >/dev/null
      if [ $? -eq 0 ]; then
        echo "redirecting to systemctl..."
        systemctl stop jdownloader
        exit $?
      fi
    fi
  fi
  
  while true; do
    ps -p $JDPID >/dev/null 2>/dev/null
    if [ $? -ne 0 ]; then
      break
    fi
    echo "trying to kill jdownloader with PID $JDPID"
    kill $JDPID
    sleep 5
  done
  exit 0
else
  echo "unknown command!"
  exit 2
fi