【问题标题】:How to write a script file for centos to restart tomcat every day at midnight如何编写centos每天半夜重启tomcat的脚本文件
【发布时间】:2016-11-12 03:04:31
【问题描述】:

我需要每天@midnight 重新启动 tomcat。 我不知道用centos写脚本。

基本上是在寻找一个脚本,该脚本将每 24 小时对 /tomcat/bin/ 中的文件执行以下命令:

  • ./shutdown.sh
  • ./startup.sh

【问题讨论】:

    标签: linux shell centos tomcat7


    【解决方案1】:

    如果将 tomcat 作为服务安装,您应该能够在每晚午夜使用 cronjob 调用这些脚本。这里有一个关于如何使用 crontab 的教程:https://help.ubuntu.com/community/CronHowto

    这个答案有一个午夜 crontab 的例子:How to write a cron that will run a script every day at midnight?

    如果服务不起作用,您可以使用

        00 00 * * * bash -c '/tomcat/bin/shutdown.sh && /tomcat/bin/startup.sh'
    

    我没有地方可以尝试,所以这是我能给你的最好的。

    【讨论】:

    • 我尝试使用 22 20 * * * bash -c 'home/tomcat/apache-tomcat-7.0.69/bin/shutdown.sh && /home/tomcat/apache-tomcat-7.0 运行 crontab .69/bin/startup.sh' 但它不起作用
    【解决方案2】:

    您可以使用cron 在特定时间运行您的脚本。

    首先创建一个脚本(比如tc_script.sh)来运行这两个命令:

    #!/bin/bash
    /tomcat/bin/shutdown.sh 
    /tomcat/bin/startup.sh
    

    其次,编辑crontab 文件以在每天午夜运行此脚本。为此,在您的终端中输入crontab -e,这将打开crontab file,您可以在其中输入要在特定时间执行的命令。

    在这个文件中,添加一个新行:

    00 00 * * * /tomcat/bin/tc_script.sh
    

    crontab 文件的语法如下:

    minute hour day_of_month month day_of_week <command>
    

    所以上面一行说,每个月每天在00 分钟、00 小时内运行命令(* 表示任何/每个)。

    cron 也可以识别 @daily 关键字,因此您也可以使用它,因为它更短且更具可读性。

    @daily /tomcat/bin/tc_script.sh
    

    @daily 将使cron 每天午夜运行脚本。

    【讨论】:

    • 我创建了一个名为 scheduler.sh 的文件,当我运行它时出现此错误[root@s19238906 bin]# ./scheduler.sh -bash: ./scheduler.sh: /bin/bash^M: bad interpreter: No such file or directory
    • 您可以将其更改为#!/bin/sh。并使用chmod +x ./scheduler.sh 使脚本可执行。
    • 好的,我删除了第一行,即#!/bin/bash,脚本文件工作正常,但是如何测试它是否工作。我关闭了 tomcat 并希望调度程序现在启动它。怎么做 ? If I get date, this is what I get [root@s19238906 bin]# date Sat Jul 9 22:00:11 CEST 2016
    • 其实我的crontab文件是空的,我加了这个22 08 * * * /home/tomcat/apache-tomcat-7.0.69/bin/scheduler.sh ,对吗?但正如预期的那样,它不会启动服务器
    • 我稍微修改了脚本(删除了&amp;&amp;)。尝试类似地更改您的脚本,并尝试手动运行脚本并查看它是否正在重新启动tomcat。接下来我们可以看到关于 cron 的内容。
    猜你喜欢
    • 2011-04-28
    • 2017-10-02
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    相关资源
    最近更新 更多