README-usage-for-service.bash 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ## USAGE
  2. # --- 配置自启
  3. echo "RUN: $(date)" \
  4. && username="user" \
  5. && servername="yancheng" \
  6. && filepath="/usr/lib/systemd/system/${servername}.service" \
  7. && projectdir="/home/${username}/repositories/repositories/sri-project.yancheng.master/yancheng-edge" \
  8. && sudo rm -rf ${filepath} \
  9. && sudo touch ${filepath} \
  10. && sudo chmod 777 ${filepath} \
  11. && sudo chmod 777 ${projectdir}/run.sh \
  12. && text='
  13. [Unit]
  14. Description={{servername}} Auto-start Service
  15. After=network.target
  16. [Service]
  17. Type=exec
  18. WorkingDirectory={{projectdir}}
  19. ExecStart=/bin/bash -c "./run.sh"
  20. Restart=always
  21. User={{username}}
  22. Group={{username}}
  23. [Install]
  24. WantedBy=multi-user.target
  25. ' \
  26. && sudo echo "${text}" | sudo tee ${filepath} \
  27. && sudo sed -i '1d' ${filepath} \
  28. && sudo sed -i "s|{{username}}|${username}|g" ${filepath} \
  29. && sudo sed -i "s|{{servername}}|${servername}|g" ${filepath} \
  30. && sudo sed -i "s|{{projectdir}}|${projectdir}|g" ${filepath} \
  31. && sudo systemctl daemon-reload \
  32. && sudo systemctl start yancheng.service \
  33. && sudo systemctl enable yancheng.service \
  34. && echo "END: $(date)"
  35. # --- 自启服务操作
  36. sudo systemctl list-units --type=service | grep yancheng
  37. sudo systemctl status yancheng.service
  38. sudo systemctl stop yancheng.service
  39. sudo systemctl start yancheng.service
  40. sudo systemctl restart yancheng.service
  41. sudo systemctl enable yancheng.service
  42. sudo journalctl -u yancheng.service -f