如何为centos写一个脚本文件,每天午夜重启tomcat

时间:2016-07-09 19:09:17

标签: linux shell centos tomcat7

我需要每天重启tomcat @midnight。 我不知道在centos中编写脚本。

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

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

2 个答案:

答案 0 :(得分:1)

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

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

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

其次,编辑crontab文件以在每天午夜运行此脚本。要在您的termainal中键入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每天午夜运行脚本。

答案 1 :(得分: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'

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