0

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
Subhasish Dash
  • 219
  • 5
  • 13

2 Answers2

2

You can use cron to run your script at specific times.

First create a script (say tc_script.sh) to run those two commands as:

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

Second, edit the crontab file to run this script at midnight everyday. To do that type crontab -e in you termainal which will open the crontab file where you can enter commands to be executed at specific times.

In this file, add a new line as:

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

Syntax of crontab file is as follows:

minute hour day_of_month month day_of_week <command>

So the above line say, run the command in 00 minutes, 00 hours everyday every month (* means any/every).

cron also recognizes @daily keyword, so you can also use that as it is shorter and more readable.

@daily /tomcat/bin/tc_script.sh

@daily will make cron run the script at midnight everyday.

sps
  • 2,720
  • 2
  • 19
  • 38
  • I created a file called scheduler.sh and when I ran it got this error `[root@s19238906 bin]# ./scheduler.sh -bash: ./scheduler.sh: /bin/bash^M: bad interpreter: No such file or directory ` – Subhasish Dash Jul 09 '16 at 19:53
  • You can change it to `#!/bin/sh`. And also make the script executable with `chmod +x ./scheduler.sh`. – sps Jul 09 '16 at 19:54
  • okay I removed the first line i.e. `#!/bin/bash` and the script file works fine, but how to test whether it is working or not. I shutdown tomcat and want the scheduler to start it now . How to do that ? `If I get date, this is what I get [root@s19238906 bin]# date Sat Jul 9 22:00:11 CEST 2016 ` – Subhasish Dash Jul 09 '16 at 20:02
  • actually, my crontab file was empty, I added this `22 08 * * * /home/tomcat/apache-tomcat-7.0.69/bin/scheduler.sh ` , is it correct ? But as expected it doesn't start the server – Subhasish Dash Jul 09 '16 at 20:10
  • I have changed the script a little bit (removed `&&`). Try changing your script similarly, and try to run the script manually and see if it is restarting the `tomcat` or not. We can see about cron next. – sps Jul 09 '16 at 20:18
  • no that is not a problem, script file is fine probably , I tried running crontab using `22 20 * * * bash -c 'home/tomcat/apache-tomcat-7.0.69/bin/shutdown.sh && /home/tomcat/apache-tomcat-7.0.69/bin/startup.sh' ` but still it doesn't work – Subhasish Dash Jul 09 '16 at 20:22
  • Is your tomcat running now? Because shutdown will fail if it is not running. So make sure your tomcat is running and try the script manually. If the script runs manually but not with cron, then maybe your cron service is not running. You can see this http://www.cyberciti.biz/faq/howto-check-cronjob-is-running-not/ to sort that issue if cron service is not running. – sps Jul 09 '16 at 20:30
  • okay it works now , I forgot the leading / before home , now it works perfectly . So, the answer would be `22 20 * * * bash -c '/home/tomcat/apache-tomcat-7.0.69/bin/shutdown.sh && /home/tomcat/apache-tomcat-7.0.69/bin/startup.sh'` . Change it but I think I should accept the other guy, whose answer helped me. you were active and helping , many thanks and +1 for that – Subhasish Dash Jul 09 '16 at 20:44
  • Glad that your issue is solved. I guess the answer I wrote still stands. However I see that your issue was solved by answer given by Alex, so its best if you accept his answer. – sps Jul 09 '16 at 20:47
2

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.

Community
  • 1
  • 1
Alex Ives
  • 101
  • 3
  • I tried running crontab using 22 20 * * * bash -c 'home/tomcat/apache-tomcat-7.0.69/bin/shutdown.sh && /home/tomcat/apache-tomcat-7.0.69/bin/startup.sh' but it doesn't work – Subhasish Dash Jul 09 '16 at 20:23