在Linux系统下配置service启动和关闭

  1. 通过命令cd /etc/init.d文件夹下
  2. 再通过命令 vim tomcat 进入vim编辑界面
  3. 用过 i键 现在把下面代码贴入编辑界面

    shell脚本如下

  1. #!/bin/bash
  2. # This is the init script for starting up the
  3. # Jakarta Tomcat server
  4. #
  5. # chkconfig: 345 91 10
  6. # description: Starts and stops the Tomcat daemon.
  7. #
  8. # Source function library.
  9. . /etc/rc.d/init.d/functions
  10. # Get config.
  11. . /etc/sysconfig/network
  12. # Check that networking is up.
  13. [ "${NETWORKING}" = "no" ] && exit 0
  14. export JAVA_HOME=/usr/local/javaweb/jdk1.8.0_192 #自己的jdk安装目录
  15. tomcat_home=/usr/local/tomcat/tomcat #自己的tomcat安装目录
  16. startup=$tomcat_home/bin/startup.sh
  17. shutdown=$tomcat_home/bin/shutdown.sh
  18. start(){
  19. echo -n "Starting Tomcat service:"
  20. cd $tomcat_home
  21. $startup
  22. echo "tomcat is succeessfully started up"
  23. }
  24. stop(){
  25. echo -n "Shutting down tomcat: "
  26. cd $tomcat_home
  27. $shutdown
  28. echo "tomcat is succeessfully shut down."
  29. }
  30. status(){
  31. numproc=`ps -ef | grep catalina | grep -v "grep catalina" | wc -l`
  32. if [ $numproc -gt 0 ]; then
  33. echo "Tomcat is running..."
  34. else
  35. echo "Tomcat is stopped..."
  36. fi
  37. }
  38. restart(){
  39. stop
  40. start
  41. }
  42. # See how we were called.
  43. case "$1" in
  44. start)
  45. start
  46. ;;
  47. stop)
  48. stop
  49. ;;
  50. status)
  51. status
  52. ;;
  53. restart)
  54. restart
  55. ;;
  56. *)
  57. echo $"Usage: $0 {start|stop|status|restart}"
  58. exit 1
  59. esac

(文件不能执行,请执行该命令)给文件添加权限,
使得脚本文件可以执行,命令为: chmod 755 /etc/rc.d/init.d/tomcat

  1. 将文件加入到服务队列中 ```shell chkconfig —add tomcat
  1. 5. 查看tomcat,文件是否加入服务列表成功
  2. ```shell
  3. chkconfig --list
  1. 设置服务开机自启动 ```shell chkconfig tomcat on

```