ogp_agent.init.dbn 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. # 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)
  48. # http://unix.stackexchange.com/questions/118217/chmod-silent-mode-how-force-exit-code-0-in-spite-of-error
  49. chown -Rf `id -u $agent_user`:`id -g $agent_user` $agent_dir >/dev/null 2>&1 || true
  50. # Lets the agent user to attach screens.
  51. if [ "$( groups $agent_user | grep "\btty\b" )" == "" ]
  52. then
  53. usermod -aG tty $agent_user >/dev/null 2>&1
  54. fi
  55. chmod g+rw /dev/pts/* >/dev/null 2>&1
  56. chmod g+rw /dev/tty* >/dev/null 2>&1
  57. # Check the FTP status
  58. if [ -f "/etc/init.d/pure-ftpd" ] && [ -e "/etc/pure-ftpd/conf" ]
  59. then
  60. echo no > /etc/pure-ftpd/conf/PAMAuthentication
  61. echo no > /etc/pure-ftpd/conf/UnixAuthentication
  62. echo yes > /etc/pure-ftpd/conf/CreateHomeDir
  63. if [ ! -f /etc/pure-ftpd/pureftpd.passwd ]
  64. then
  65. touch /etc/pure-ftpd/pureftpd.passwd
  66. fi
  67. if [ ! -f /etc/pureftpd.passwd ]
  68. then
  69. ln -s /etc/pure-ftpd/pureftpd.passwd /etc/pureftpd.passwd
  70. fi
  71. if [ ! -f /etc/pure-ftpd/auth/50pure ]
  72. then
  73. ln -s /etc/pure-ftpd/conf/PureDB /etc/pure-ftpd/auth/50pure
  74. fi
  75. if [ ! -f /etc/pureftpd.pdb ]
  76. then
  77. ln -s /etc/pure-ftpd/pureftpd.pdb /etc/pureftpd.pdb
  78. fi
  79. pure-pw mkdb >/dev/null 2>&1
  80. service pure-ftpd force-reload >/dev/null 2>&1 &
  81. fi
  82. cd $agent_dir
  83. 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 &
  84. log_success_msg "OGP Agent started successfully."
  85. echo "Use \"sudo su -c 'screen -S ogp_agent -r' $agent_user\" to attach the agent screen,"
  86. echo "and CTRL+A+D to detach it."
  87. return 0
  88. }
  89. stop() {
  90. if [ -e "$agent_dir/ogp_agent_run.pid" ]
  91. then
  92. if [ "X$(ps -A | grep -o `cat $agent_dir/ogp_agent_run.pid`)" == "X" ]
  93. then
  94. log_failure_msg "OGP Agent not running."
  95. else
  96. kill `cat $agent_dir/ogp_agent_run.pid` >/dev/null 2>&1
  97. log_success_msg "OGP Agent stopped successfully."
  98. fi
  99. else
  100. log_failure_msg "PID file not found ($agent_dir/ogp_agent_run.pid)"
  101. fi
  102. return 0
  103. }
  104. case "${1:-''}" in
  105. start)
  106. start
  107. RETVAL=$?
  108. ;;
  109. stop)
  110. stop
  111. RETVAL=$?
  112. ;;
  113. restart)
  114. stop
  115. sleep 1
  116. start
  117. RETVAL=$?
  118. ;;
  119. *)
  120. echo "Usage: service ogp_agent start|stop|restart"
  121. RETVAL=1
  122. ;;
  123. esac
  124. exit $RETVAL