定时自动部署和更新hexo

    1. #!/bin/bash
    2. function startServer()
    3. {
    4. cd /opt/blog
    5. nohup hexo server -p 80 >/dev/null 2>&1 &
    6. echo "start server sucess !!! PID ::: "
    7. getPID
    8. }
    9. function stopServer()
    10. {
    11. ps -ef|grep hexo |grep -v grep | grep -v cron |grep -v blog | grep -v /bin/bash | grep -v /bin/sh | awk '{print $2}' | xargs kill -9 >> /dev/null
    12. echo "stop hexo success !!! "
    13. }
    14. function getPID()
    15. {
    16. ps -ef | grep hexo | grep -v grep | grep -v cron | grep -v blog | grep -v /bin/bash | grep -v /bin/sh | awk '{print $2}'
    17. echo ""
    18. }
    19. function autoUpdate()
    20. {
    21. chkHexo=`ps -ef|grep hexo | grep -v cron | grep -v grep |grep -v blog | grep -v /bin/sh | grep -v /bin/bash |awk '{print $2}'`
    22. if [[ -z $chkHexo ]];then
    23. echo "hexo server is stop !!! "
    24. else
    25. stopServer
    26. fi
    27. killall nginx
    28. echo ""
    29. cd /opt/blog
    30. rm -rf db.json
    31. hexo clean
    32. hexo generate
    33. gulp
    34. nginx
    35. }
    36. function autoDeploy()
    37. {
    38. autoUpdate
    39. cd /opt/blog
    40. set timeout 200000
    41. /usr/bin/expect <<-EOF
    42. spawn hexo deploy
    43. expect "Username"
    44. send "$gitUn\r"
    45. expect "Password"
    46. send "$gitPwd\r"
    47. interact
    48. expect eof
    49. EOF
    50. startServer
    51. }
    52. . /etc/profile
    53. autoDeploy
    54. echo ""
    55. echo -n "-------------- auto deploy "
    56. echo `date +"%F %H:%M:%S"`
    57. echo ""
    58. echo ""

    配置定时任务

    1. [root@hostname ~]crontab -e
    2. 添加如下一行:
    3. 30 23 * * * root `sh /opt/blog/hexo-deploy.sh >> /tmp/test.txt`