ogp_agent.init.rh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. chown -Rf $agent_user:$group $agent_dir &> /dev/null
  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. success
  91. bold=`tput bold`
  92. normal=`tput sgr0`
  93. echo
  94. echo "Use ${bold}sudo su -c 'screen -S ogp_agent -r' $agent_user${normal} to attach the agent screen,"
  95. echo "and ${bold}ctrl+A+D${normal} to detach it."
  96. return 0
  97. }
  98. stop() {
  99. # Stop daemon
  100. echo -n "Stopping OGP Agent: "
  101. if [ -f $agent_dir/ogp_agent_run.pid ]
  102. then
  103. PID=`cat $agent_dir/ogp_agent_run.pid`
  104. RET=$(kill $PID &> /dev/null; echo $?)
  105. if [ $RET -ne 0 ]; then
  106. echo -n "not running."
  107. failure
  108. else
  109. echo -n "stopped successfully."
  110. success
  111. fi
  112. else
  113. echo -n "PID file not found ($agent_dir/ogp_agent_run.pid)"
  114. failure
  115. fi
  116. echo
  117. return 0
  118. }
  119. case "$1" in
  120. start)
  121. start
  122. RETVAL=$?
  123. ;;
  124. stop)
  125. stop
  126. RETVAL=$?
  127. ;;
  128. restart)
  129. stop
  130. ${!}
  131. start
  132. RETVAL=$?
  133. ;;
  134. *)
  135. echo "Usage: service ogp_agent start|stop|restart"
  136. RETVAL=1
  137. echo
  138. ;;
  139. esac
  140. exit $RETVAL