daemonlib.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2009, Cisco Systems Inc.
  4. # Ngie Cooper, August 2009
  5. # Copyright (C) 2012-2014 Linux Test Project
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. # running under systemd?
  22. if command -v systemctl >/dev/null 2>&1; then
  23. HAVE_SYSTEMCTL=1
  24. else
  25. HAVE_SYSTEMCTL=0
  26. fi
  27. # Check to see if syslogd, syslog-ng or rsyslogd exists
  28. SYSLOG_DAEMON=""
  29. if command -v syslogd >/dev/null 2>&1; then
  30. SYSLOG_DAEMON="syslog"
  31. elif command -v syslog-ng >/dev/null 2>&1; then
  32. SYSLOG_DAEMON="syslog-ng"
  33. elif command -v rsyslogd >/dev/null 2>&1; then
  34. SYSLOG_DAEMON="rsyslog"
  35. fi
  36. # Check to see if cron or crond exists
  37. CROND_DAEMON=""
  38. if command -v crond >/dev/null 2>&1; then
  39. CROND_DAEMON="crond"
  40. elif command -v cron >/dev/null 2>&1; then
  41. CROND_DAEMON="cron"
  42. fi
  43. start_daemon()
  44. {
  45. if [ $HAVE_SYSTEMCTL -eq 1 ]; then
  46. systemctl start $1.service > /dev/null 2>&1
  47. elif command -v service >/dev/null 2>&1; then
  48. service $1 start > /dev/null 2>&1
  49. else
  50. /etc/init.d/$1 start > /dev/null 2>&1
  51. fi
  52. }
  53. stop_daemon()
  54. {
  55. if [ $HAVE_SYSTEMCTL -eq 1 ]; then
  56. systemctl stop $1.service > /dev/null 2>&1
  57. elif command -v service >/dev/null 2>&1; then
  58. service $1 stop > /dev/null 2>&1
  59. else
  60. /etc/init.d/$1 stop > /dev/null 2>&1
  61. fi
  62. }
  63. status_daemon()
  64. {
  65. if [ $HAVE_SYSTEMCTL -eq 1 ]; then
  66. systemctl is-active $1.service > /dev/null 2>&1
  67. elif command -v service >/dev/null 2>&1; then
  68. service $1 status > /dev/null 2>&1
  69. else
  70. /etc/init.d/$1 status > /dev/null 2>&1
  71. fi
  72. }
  73. restart_daemon()
  74. {
  75. if [ $HAVE_SYSTEMCTL -eq 1 ]; then
  76. systemctl restart $1.service > /dev/null 2>&1
  77. elif command -v service >/dev/null 2>&1; then
  78. service $1 restart > /dev/null 2>&1
  79. else
  80. /etc/init.d/$1 restart > /dev/null 2>&1
  81. fi
  82. }