ogp_agent.init.dbn 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. #
  3. ### BEGIN INIT INFO
  4. # Provides: ogp_agent
  5. # Required-Start: $all
  6. # Required-Stop: $all
  7. # Should-Start: $all
  8. # Should-Stop: $all
  9. # Default-Start: 2 3 4 5
  10. # Default-Stop: 0 1 6
  11. # Short-Description: Start and stop the OGP Agent
  12. # Description: Start and stop the OGP Agent
  13. ### END INIT INFO
  14. #
  15. set -e
  16. set -u
  17. ${DEBIAN_SCRIPT_DEBUG:+ set -v -x}
  18. . /lib/lsb/init-functions
  19. agent_dir=OGP_AGENT_DIR
  20. agent_user=OGP_USER
  21. #
  22. # main()
  23. #
  24. if [ "X`whoami`" != "Xroot" ]
  25. then
  26. log_failure_msg "Permission denied."
  27. exit 1
  28. fi
  29. start() {
  30. if [ -e "$agent_dir/ogp_agent_run.pid" ]
  31. then
  32. if [ "X$(ps -A | grep -o `cat $agent_dir/ogp_agent_run.pid`)" != "X" ]
  33. then
  34. log_failure_msg "OGP Agent already running."
  35. return 1
  36. fi
  37. fi
  38. # Lets the agent user to use sudo to enable FTP accounts and use renice and taskset.
  39. if [ "$( groups $agent_user | grep "\bsudo\b" )" == "" ]
  40. then
  41. if [ "$( egrep -i "^sudo" /etc/group )" == "" ]
  42. then
  43. groupadd sudo >/dev/null 2>&1
  44. fi
  45. usermod -aG sudo $agent_user >/dev/null 2>&1
  46. fi
  47. chown -Rf `id -u $agent_user`:`id -g $agent_user` $agent_dir >/dev/null 2>&1
  48. # Lets the agent user to attach screens.
  49. if [ "$( groups $agent_user | grep "\btty\b" )" == "" ]
  50. then
  51. usermod -aG tty $agent_user >/dev/null 2>&1
  52. fi
  53. chmod g+rw /dev/pts/* >/dev/null 2>&1
  54. chmod g+rw /dev/tty* >/dev/null 2>&1
  55. # Check the FTP status
  56. if [ -f "/etc/init.d/pure-ftpd" ] && [ -e "/etc/pure-ftpd/conf" ]
  57. then
  58. echo no > /etc/pure-ftpd/conf/PAMAuthentication
  59. echo no > /etc/pure-ftpd/conf/UnixAuthentication
  60. echo yes > /etc/pure-ftpd/conf/CreateHomeDir
  61. if [ ! -f /etc/pure-ftpd/pureftpd.passwd ]
  62. then
  63. touch /etc/pure-ftpd/pureftpd.passwd
  64. fi
  65. if [ ! -f /etc/pureftpd.passwd ]
  66. then
  67. ln -s /etc/pure-ftpd/pureftpd.passwd /etc/pureftpd.passwd
  68. fi
  69. if [ ! -f /etc/pure-ftpd/auth/50pure ]
  70. then
  71. ln -s /etc/pure-ftpd/conf/PureDB /etc/pure-ftpd/auth/50pure
  72. fi
  73. if [ ! -f /etc/pureftpd.pdb ]
  74. then
  75. ln -s /etc/pure-ftpd/pureftpd.pdb /etc/pureftpd.pdb
  76. fi
  77. pure-pw mkdb >/dev/null 2>&1
  78. service pure-ftpd force-reload >/dev/null 2>&1 &
  79. fi
  80. cd $agent_dir
  81. 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 &
  82. log_success_msg "OGP Agent started successfully."
  83. echo "Use \"sudo su -c 'screen -S ogp_agent -r' $agent_user\" to attach the agent screen,"
  84. echo "and CTRL+A+D to detach it."
  85. return 0
  86. }
  87. stop() {
  88. if [ -e "$agent_dir/ogp_agent_run.pid" ]
  89. then
  90. if [ "X$(ps -A | grep -o `cat $agent_dir/ogp_agent_run.pid`)" == "X" ]
  91. then
  92. log_failure_msg "OGP Agent not running."
  93. else
  94. kill `cat $agent_dir/ogp_agent_run.pid` >/dev/null 2>&1
  95. log_success_msg "OGP Agent stopped successfully."
  96. fi
  97. else
  98. log_failure_msg "PID file not found ($agent_dir/ogp_agent_run.pid)"
  99. fi
  100. return 0
  101. }
  102. case "${1:-''}" in
  103. start)
  104. start
  105. RETVAL=$?
  106. ;;
  107. stop)
  108. stop
  109. RETVAL=$?
  110. ;;
  111. restart)
  112. stop
  113. sleep 1
  114. start
  115. RETVAL=$?
  116. ;;
  117. *)
  118. echo "Usage: service ogp_agent start|stop|restart"
  119. RETVAL=1
  120. ;;
  121. esac
  122. exit $RETVAL