Search code examples
linuxshellcentostomcat7

How to write a script file for centos to restart tomcat every day at midnight


I am in the need of restarting tomcat everyday @midnight. I have no idea in writing scripts in centos .

Basically looking for a script that will execute following commands every 24 hr for the files located in /tomcat/bin/ :

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

Solution

  • If tomcat is installed as a service, you should be able to use a cronjob call those scripts at midnight every night. There's a tutorial on how to use crontab here: https://help.ubuntu.com/community/CronHowto

    This answer has an example for midnight crontab: How to write a cron that will run a script every day at midnight?

    If the service doesn't work, you could use

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

    I don't have somewhere to try it, so that's the best I can give you.