ogp_agent.init.rh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. return 1
  37. fi
  38. fi
  39. # Lets the agent user to use sudo to enable FTP accounts and use renice and taskset.
  40. if [ "$( cat /etc/group | grep "^sudo" )" == "" ]
  41. then
  42. groupadd sudo &> /dev/null
  43. fi
  44. if [ "$( cat /etc/sudoers | grep "^%sudo" )" == "" ]
  45. then
  46. echo '%sudo ALL=(ALL) ALL' >> /etc/sudoers
  47. fi
  48. if [ "$( groups $agent_user | grep "\bsudo\b" )" == "" ]
  49. then
  50. usermod -a -G sudo $agent_user &> /dev/null
  51. fi
  52. group=`groups $agent_user | awk '{ print $3 }'`;
  53. # 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)
  54. # http://unix.stackexchange.com/questions/118217/chmod-silent-mode-how-force-exit-code-0-in-spite-of-error
  55. chown -Rf $agent_user:$group $agent_dir &> /dev/null || true
  56. # Lets the agent user to attach screens.
  57. if [ "$( groups $agent_user | grep "\btty\b" )" == "" ]
  58. then
  59. usermod -a -G tty $agent_user &> /dev/null
  60. fi
  61. chmod g+rw /dev/pts/* &> /dev/null
  62. chmod g+rw /dev/tty* &> /dev/null
  63. # Give access for ifconfig to the user of the agent
  64. if [ ! -f /usr/bin/ifconfig ]
  65. then
  66. ln -s /sbin/ifconfig /usr/bin/ifconfig
  67. fi
  68. # Check the FTP status
  69. if [ -f "/etc/init.d/pure-ftpd" ]
  70. then
  71. if [ "$( cat /etc/pure-ftpd/pure-ftpd.conf | grep "^PureDB" )" == "" ]
  72. then
  73. sed -i 's|.*PureDB.*|PureDB /etc/pure-ftpd/pureftpd.pdb|' /etc/pure-ftpd/pure-ftpd.conf
  74. sed -i 's|.*BrokenClientsCompatibility.*|BrokenClientsCompatibility yes|' /etc/pure-ftpd/pure-ftpd.conf
  75. sed -i 's|.*NoAnonymous.*|NoAnonymous yes|' /etc/pure-ftpd/pure-ftpd.conf
  76. sed -i 's|.*PAMAuthentication.*|PAMAuthentication no|' /etc/pure-ftpd/pure-ftpd.conf
  77. sed -i 's|.*CreateHomeDir.*|CreateHomeDir yes|' /etc/pure-ftpd/pure-ftpd.conf
  78. pure-pw mkdb &> /dev/null
  79. service pure-ftpd restart &> /dev/null
  80. fi
  81. PURE_STATUS=`service pure-ftpd status`
  82. if test $? -ne 0
  83. then
  84. service pure-ftpd restart &> /dev/null
  85. fi
  86. fi
  87. cd $agent_dir
  88. 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 &
  89. echo -n "started successfully."
  90. bold=`tput bold`
  91. normal=`tput sgr0`
  92. echo
  93. echo "Use ${bold}sudo su -c 'screen -S ogp_agent -r' $agent_user${normal} to attach the agent screen,"
  94. echo "and ${bold}ctrl+A+D${normal} to detach it."
  95. return 0
  96. }
  97. stop() {
  98. # Stop daemon
  99. echo -n "Stopping OGP Agent: "
  100. if [ -f $agent_dir/ogp_agent_run.pid ]
  101. then
  102. PID=`cat $agent_dir/ogp_agent_run.pid`
  103. RET=$(kill $PID &> /dev/null; echo $?)
  104. if [ $RET -ne 0 ]; then
  105. echo -n "not running."
  106. else
  107. echo -n "stopped successfully."
  108. fi
  109. else
  110. echo -n "PID file not found ($agent_dir/ogp_agent_run.pid)"
  111. fi
  112. echo
  113. return 0
  114. }
  115. case "$1" in
  116. start)
  117. start
  118. RETVAL=$?
  119. ;;
  120. stop)
  121. stop
  122. RETVAL=$?
  123. ;;
  124. restart)
  125. stop
  126. ${!}
  127. start
  128. RETVAL=$?
  129. ;;
  130. *)
  131. echo "Usage: service ogp_agent start|stop|restart"
  132. RETVAL=1
  133. echo
  134. ;;
  135. esac
  136. exit $RETVAL