service_linux.sh 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/bin/bash
  2. # Copyright (c) http://www.o2oa.net/
  3. current_dir="$(
  4. cd "$(dirname "$0")"
  5. pwd
  6. )"
  7. cd ${current_dir}
  8. if [ -z "$1" ]; then
  9. echo "usage: ./service_linux.sh name [start.sh default is start_linux.sh]"
  10. exit
  11. fi
  12. name="$1"
  13. scriptName="start_linux.sh"
  14. if [ -z "$2" ]; then
  15. echo "use ${current_dir}/${scriptName} as start script."
  16. else
  17. scriptName="$2"
  18. fi
  19. if [ ! -f ${current_dir}/${scriptName} ]; then
  20. echo "start script ${current_dir}/${scriptName} not exist."
  21. exit
  22. fi
  23. servicePath="/etc/systemd/system/${name}.service"
  24. echo "[Unit]" >${servicePath}
  25. echo "Description=o2server name:${name}" >>${servicePath}
  26. echo "Wants=network-online.target" >>${servicePath}
  27. echo "After=network.target" >>${servicePath}
  28. echo "[Service]" >>${servicePath}
  29. echo "Type=simple" >>${servicePath}
  30. echo "StandardOutput=null" >>${servicePath}
  31. echo "StandardError=null" >>${servicePath}
  32. echo "ExecStart=${current_dir}/${scriptName}" >>${servicePath}
  33. echo "ExecReload=${current_dir}/restart_linux.sh" >>${servicePath}
  34. echo "ExecStop=${current_dir}/stop_linux.sh" >>${servicePath}
  35. echo "[Install]" >>${servicePath}
  36. echo "WantedBy=multi-user.target" >>${servicePath}