Advertisement
jvamos

crushftpinit

Apr 23rd, 2014
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.65 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Control script for CrushFTP
  4. #
  5. # chkconfig: - 86 14
  6. # description: CrushFTP
  7. #
  8. ### BEGIN INIT INFO  
  9. # Provides:          crushftp_init.sh  
  10. # Required-Start:    $local_fs  
  11. # Should-Start:      $network  
  12. # Required-Stop:      
  13. # Should-Stop:       $network  
  14. # Default-Start:     2 3 5  
  15. # Default-Stop:      2 5  
  16. # Short-Description: CrushFTP Server  
  17. # Description:       Starts Crush on boot  
  18. ### END INIT INFO  
  19. # THESE NEED TO BE SET
  20. CRUSH_DIR="/var/opt/CrushFTP7_PC/" #crushftp directory
  21. USER="root" # only work for this user
  22. JAVA="java"
  23. PS="ps"
  24. AWK="awk"
  25. GREP="grep"
  26. WHOAMI="whoami"
  27. NOHUP="nohup"
  28. LC_ALL=en_US.UTF-8
  29. export LC_ALL=en_US.utf8
  30.  
  31. # example of how to redirect a low port to a high port so Crush doesn't have to run as root
  32. # iptables -t nat -A PREROUTING -p tcp -m tcp --dport 21 -j REDIRECT --to-ports 2121
  33.  
  34. # We MUST start the server in the proper directory. If we can not change to that directory, we exit.
  35. change_dir()
  36. {
  37.  cd $CRUSH_DIR
  38.  ret_val=$?
  39.  if [ ${ret_val} -ne 0 ]; then
  40.    echo FAIL
  41.    echo could not change to CrushFTP directory
  42.    echo the directory is setup as:
  43.    echo $CRUSH_DIR
  44.    exit 1
  45.  fi
  46. }
  47.  
  48. # get PID from process list.  Not from a 'stored' file.  Since Crush updates will
  49. # restart the server, but NEVER run this script, then if we stored off the PID into
  50. # a file, then after an update, this script would not be able to shut down the
  51. # process.  We have added a couple greps into the get_pid() so that we 'know' we
  52. # are getting the proper PID if it exists.
  53. get_pid()
  54. {
  55.  CRUSH_PID="`$PS -ef | $GREP java | $GREP $CRUSH_DIR | awk '{print $2}'`"
  56.  CRUSH_PARENT="`$PS -ef | $GREP java | $GREP $CRUSH_DIR | awk '{print $3}'`"
  57. }
  58.  
  59. # if the wrong user runs this script then BAIL.  If this script should be run as user
  60. # OTHER than 'root' (or su or sudo), then you must redirect port 21 (or 22) up to a higher
  61. # port, such as 60021.  iptables can do this well.  Then setup the crush server to bind to
  62. # these higher ports.  Running as non-root is much more secure.  NOTE, it 'is' valid for root
  63. # to shut down the server (but not to start it, unless USER="root" is set at teh top of the file
  64. ROOT_OK=0
  65. chk_user()
  66. {
  67.   if [ "$USER" != `whoami` ]; then
  68.     if [ `whoami` = "root" -a "$ROOT_OK" = "1" ]; then
  69. # Not an error.  Root user is OK here, even if 'not' the proper user (such as killing the process).
  70.        echo
  71.     else
  72.        #echo FAIL
  73.        echo "Wrong user.  The user running this script MUST be $USER, but you are `whoami`"
  74.        #exit 1;
  75.     fi
  76.   fi
  77. }
  78.  
  79. #############################################################################################
  80. # Here is the 'main' script.  We can either start the server, or shutdown the current       #
  81. # running server.   There is error checking to make sure the proper user is being used.     #
  82. #############################################################################################
  83. case "$1" in
  84.         start)
  85.              chk_user
  86.              get_pid
  87.              if [ "$CRUSH_PID" ]; then
  88.                echo FAIL
  89.                echo Found an already running instance of CrushFTP.
  90.                echo It is not valid o start 2 sessions in the same directory.
  91.                exit 1;
  92.              fi
  93.              echo -n "Starting CrushFTP... "
  94.              change_dir
  95.  
  96.              # run daemon
  97.              #$NOHUP $JAVA -Ddir=$CRUSH_DIR -Xmx384M -jar CrushFTP.jar -d & >/dev/null 2>&1
  98.              $NOHUP $JAVA -Ddir=$CRUSH_DIR -Xmx384M -jar plugins/lib/CrushFTPJarProxy.jar -d & >/dev/null 2>&1
  99.              echo OK
  100.         ;;
  101.  
  102.          stop)
  103.              # root or $USER is ok to shut down the server.
  104.              ROOT_OK=1
  105.              chk_user
  106.              get_pid
  107.              if [ ! "$CRUSH_PID" ]; then
  108.                echo FAIL
  109.                echo Could not find Crush PID
  110.                exit 1
  111.              fi
  112.  
  113.              echo -n Shutting down CrushFTP...
  114.              kill $CRUSH_PID
  115.              ret_val=$?
  116.              if [ ${ret_val} -ne 0 ]; then
  117.                 echo FAIL
  118.                 echo could not kill PID
  119.                 exit 1
  120.              fi
  121.              echo OK
  122.         ;;
  123.  
  124.         status)
  125.              get_pid
  126.              if [ ! "$CRUSH_PID" ]; then
  127.                echo stopped
  128.                exit 3
  129.              else
  130.                if [ "$CRUSH_PARENT" = "1" ]; then
  131.                  echo "running as daemon (pid $CRUSH_PID)"
  132.                else
  133.                  echo "running as user (pid $CRUSH_PID)"
  134.                fi
  135.              fi
  136.         ;;
  137.  
  138.         *)
  139.              echo "Usage: $0 [start|stop]   NOTE you must be logged in as $USER to run this script"
  140. esac
  141.  
  142. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement