ogp_agent.init.rh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/sh
  2. #
  3. # Startup/shutdown script for the OGP Agent.
  4. #
  5. # Linux chkconfig stuff:
  6. #
  7. # chkconfig: 2345 88 10
  8. # description: Startup/shutdown script for the OGP Agent
  9. agent_dir=OGP_AGENT_DIR
  10. agent_user=OGP_USER
  11. service=ogp_agent
  12. # Source function library.
  13. if [ -f /etc/rc.d/init.d/functions ] ; then
  14. . /etc/rc.d/init.d/functions
  15. elif [ -f /etc/init.d/functions ] ; then
  16. . /etc/init.d/functions
  17. fi
  18. if [ "$( whoami )" != "root" ]
  19. then
  20. if [ -f "/usr/bin/sudo" ] && [ "$( groups $agent_user | grep "\bsudo\b" )" != "" ]
  21. then
  22. sudo /etc/init.d/ogp_agent ${1:-''}
  23. exit
  24. else
  25. echo "Permission denied."
  26. exit
  27. fi
  28. fi
  29. start() {
  30. echo -n "Starting OGP Agent: "
  31. if [ -e "$agent_dir/ogp_agent_run.pid" ]; then
  32. PID=`cat $agent_dir/ogp_agent_run.pid`
  33. RET=$(kill -s 0 $PID &> /dev/null; echo $?)
  34. if [ $RET -eq 0 ]; then
  35. echo -n "already running."
  36. failure
  37. echo
  38. return 1
  39. fi
  40. fi
  41. # Lets the agent user to use sudo to enable FTP accounts and use renice and taskset.
  42. if [ "$( cat /etc/group | grep "^sudo" )" == "" ]
  43. then
  44. groupadd sudo &> /dev/null
  45. fi
  46. if [ "$( cat /etc/sudoers | grep "^%sudo" )" == "" ]
  47. then
  48. echo '%sudo ALL=(ALL) ALL' >> /etc/sudoers
  49. fi
  50. if [ "$( groups $agent_user | grep "\bsudo\b" )" == "" ]
  51. then
  52. usermod -a -G sudo $agent_user &> /dev/null
  53. fi
  54. group=`groups $agent_user | awk '{ print $3 }'`;
  55. # Had to add the "|| true" part to the end of the below command due to chown causing the entire bash script to exit on failure in case of protected files (that have chattr +i applied)
  56. # http://unix.stackexchange.com/questions/118217/chmod-silent-mode-how-force-exit-code-0-in-spite-of-error
  57. chown -Rf $agent_user:$group $agent_dir &> /dev/null || true
  58. # Lets the agent user to attach screens.
  59. if [ "$( groups $agent_user | grep "\btty\b" )" == "" ]
  60. then
  61. usermod -a -G tty $agent_user &> /dev/null
  62. fi
  63. chmod g+rw /dev/pts/* &> /dev/null
  64. chmod g+rw /dev/tty* &> /dev/null
  65. # Give access for ifconfig to the user of the agent
  66. if [ ! -f /usr/bin/ifconfig ]
  67. then
  68. ln -s /sbin/ifconfig /usr/bin/ifconfig
  69. fi
  70. # Check the FTP status
  71. if [ -f "/etc/init.d/pure-ftpd" ]
  72. then
  73. if [ "$( cat /etc/pure-ftpd/pure-ftpd.conf | grep "^PureDB" )" == "" ]
  74. then
  75. sed -i 's|.*PureDB.*|PureDB /etc/pure-ftpd/pureftpd.pdb|' /etc/pure-ftpd/pure-ftpd.conf
  76. sed -i 's|.*BrokenClientsCompatibility.*|BrokenClientsCompatibility yes|' /etc/pure-ftpd/pure-ftpd.conf
  77. sed -i 's|.*NoAnonymous.*|NoAnonymous yes|' /etc/pure-ftpd/pure-ftpd.conf
  78. sed -i 's|.*PAMAuthentication.*|PAMAuthentication no|' /etc/pure-ftpd/pure-ftpd.conf
  79. sed -i 's|.*CreateHomeDir.*|CreateHomeDir yes|' /etc/pure-ftpd/pure-ftpd.conf
  80. pure-pw mkdb &> /dev/null
  81. service pure-ftpd restart &> /dev/null
  82. fi
  83. PURE_STATUS=`service pure-ftpd status`
  84. if test $? -ne 0
  85. then
  86. service pure-ftpd restart &> /dev/null
  87. fi
  88. fi
  89. cd $agent_dir
  90. su -c "screen -d -m -t ogp_agent -c ogp_screenrc -S ogp_agent ./ogp_agent_run -pidfile ogp_agent_run.pid" $agent_user &> $agent_dir/ogp_agent.svc &
  91. echo -n "started successfully."
  92. success
  93. bold=`tput bold`
  94. normal=`tput sgr0`
  95. echo
  96. echo "Use ${bold}sudo su -c 'screen -S ogp_agent -r' $agent_user${normal} to attach the agent screen,"
  97. echo "and ${bold}ctrl+A+D${normal} to detach it."
  98. return 0
  99. }
  100. stop() {
  101. # Stop daemon
  102. echo -n "Stopping OGP Agent: "
  103. if [ -f $agent_dir/ogp_agent_run.pid ]
  104. then
  105. PID=`cat $agent_dir/ogp_agent_run.pid`
  106. RET=$(kill $PID &> /dev/null; echo $?)
  107. if [ $RET -ne 0 ]; then
  108. echo -n "not running."
  109. failure
  110. else
  111. echo -n "stopped successfully."
  112. success
  113. fi
  114. else
  115. echo -n "PID file not found ($agent_dir/ogp_agent_run.pid)"
  116. failure
  117. fi
  118. echo
  119. return 0
  120. }
  121. case "$1" in
  122. start)
  123. start
  124. RETVAL=$?
  125. ;;
  126. stop)
  127. stop
  128. RETVAL=$?
  129. ;;
  130. restart)
  131. stop
  132. ${!}
  133. start
  134. RETVAL=$?
  135. ;;
  136. *)
  137. echo "Usage: service ogp_agent start|stop|restart"
  138. RETVAL=1
  139. echo
  140. ;;
  141. esac
  142. exit $RETVAL