【发布时间】:2016-11-12 03:04:31
【问题描述】:
我需要每天@midnight 重新启动 tomcat。 我不知道用centos写脚本。
基本上是在寻找一个脚本,该脚本将每 24 小时对 /tomcat/bin/ 中的文件执行以下命令:
- ./shutdown.sh
- ./startup.sh
【问题讨论】:
标签: linux shell centos tomcat7
我需要每天@midnight 重新启动 tomcat。 我不知道用centos写脚本。
基本上是在寻找一个脚本,该脚本将每 24 小时对 /tomcat/bin/ 中的文件执行以下命令:
【问题讨论】:
标签: linux shell centos tomcat7
如果将 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'
我没有地方可以尝试,所以这是我能给你的最好的。
【讨论】:
您可以使用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 每天午夜运行脚本。
【讨论】:
[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
22 08 * * * /home/tomcat/apache-tomcat-7.0.69/bin/scheduler.sh ,对吗?但正如预期的那样,它不会启动服务器
&&)。尝试类似地更改您的脚本,并尝试手动运行脚本并查看它是否正在重新启动tomcat。接下来我们可以看到关于 cron 的内容。