I'm responsbile for a project environment made up of 3 components which is occasionally dying. Here is a short raw overview of environment
- Apache Reverse Proxy (entry door to app server)
- Tomcat Server with an Application enabling web access
- A Java Standalone application using SQLite database
The Tomcat and Java Standalone application is running on top of Windows 2008 RC2 Standard, the overall environment is becoming inacessible periodically and in order to solve that the customer decided to implement a daily Windows server reboot in my opinion this is very bad approach as it is much better to just set an auto reboot of each of components using few tiny batch scripts and Windows Taskmgr, however as the customer is king and decided to implement the reboot its their own thing.
However even fter the daily server reboot was set once a week or so the application was becoming inaccessible and a Tomcat server restart was necessery as a fix.
Finally as a work-around to the issue, I've proposed the logical thing to automatically restart Tomcat once a day early in morning, here is how Tomcat auto Restart was implemented on the Win server:
1. Check out the name of running Tomcat service
First thing is to use the sc command to find out the Tomcat application name:
C:UsersGeorgi>sc query state= all| findstr "Tomcat"
DISPLAY_NAME: Apache Tomcat Tomcat7_r2c
2. Create bat script to stop and start Tomcat service
Press keyboard Win-button + R, start notepad type inside:
sc stop Tomcat7_r2c && sc start Tomcat7_r2c
Don't be confused from screenshot that I have Tomcat7_MyApp instead of Tomcat7_r2c, but I made screenshot in hurry for another app.
Save the file, somewhere (preferrably) in application folder/bin/ it is best to save it once with bat extension MyApp-Tomcat_Restart.bat and once as MyApp-Tomcat_Restart.xml (XML format file is later needed for import to Task Scheduler which understands .XMLs). The .bat file is good to have because it is useful to somtimes restart Tomcat manually by running it (in case of some sudden Tomcat Appserver occurs even though the auto-restart script).
3. Create new Task using command line (cmd.exe)
Task can be created also from command line using following syntax:
schtasks /Create [/S
[/U [/P [ ]]]]
/XML <xmlfile> /TN <taskname>
Simple way to create a new Windows task is shown in below command, it will set my Tomcat Restart script to run everyday in 05:00 early morning when no employees are using the system:
schTasks /Create /SC DAILY /TN "My Task" /TR "C:UsersGeorgiDesktopmyApp-Tomcat_Restart.bat" /ST 05:00
SUCCESS: The scheduled task "Tomcat Restart Task" has successfully been created.
4. Create / Import new Windows "Cron" job
Alternative way is to use Task Scheduler GUI frontend and create new (Basic Task) or import just created script
To run Windows Task Scheduler from comamnd line :
To import already existing .XML formatted file for Task scheduler, right click on the Task Scheduler (Local) and select Import task
Import the myApp-Tomcat_Restart.XML previously created file
Adjust settings to suit your needs, but what change atleast:
- the path to the myApp-Tomcat_Restart.bat file in Actions tab
- the Local User account with which script will be running (administrator) in General tab
After making all changes you will be prompted for server Administrator account password
5. check existing Win Cron job from command line
To see the configured (Scheduled Tasks) in command line mode with a command:
The command is Windows equivalent to UNIX / Linux's crontab, e.g.:
crontab -u root -l
6. Delete existing Windows Task Job from Command line
If you happen to need to delete just created task or any other task from command line (Assuming that you know the previously created task name), use cmd:
C:>schtasks /Delete /TN "Tomcat Restart Task"
WARNING: Are you sure you want to remove the task "Tomcat Restart Task" (Y/N)? y
SUCCESS: The scheduled task "Tomcat Restart Task" was successfully deleted.
Task completed, Tomcat will auto-restart on Windows host at your scheduled time. Feedback is mostly welcome 🙂