In part 4 of this article we explored a method for transfering a password from one file to another. Now we are going to use Apache Ant to stop a Liferay server. What are the steps needed to accomplish this goal? First we need to determine if the Liferay server is running. No need to stop it if it isn’t running. Next we need to run the shutdown command. The shutdown command works most of the time. The build file will need to account for those times when it doesn’t. Because I will be using this on a Linux server we will start with the pgrep command.
command line on server
The above pgrep command shows that Liferay is running. Using the above results we can construct the following target.
target to detect running Liferay server
Using the exec task we send the pgrep command to the OS and store the result in the “result” property. Then using the condition task we can verify the result contains what it should and set the isLiferayRunning property to true. Otherwise we set it to false. Now the next target can use it to stop the Liferay instance, or not.
target to stop Liferay if running
The above target depends on our previous target. Which means the checkIfLiferayIsRunning target runs first and then returns to the stopLiferayIfRunning target. However the “if” attribute of the target will prevent it from being run if the “isLiferayRunning” property is not true. This property was set inside the checkIfLiferayIsRunning target. We still need to monitor and make sure it shuts down properly. We can do this by expanding on the target to monitor a log file.
target to stop Liferay if running
The additions verify that the log file is available and monitors it waiting for Apache Tomcat to report that the HTTP protocol handler has been destroyed. This log message lets us know that Tomcat has fully shutdown. What would happen if this message didn’t appear in the log? Eventually the waitfor tasks will timeout. One option would be to follow the waitfor tasks with a fail task to stop Ant from continuing. In this case if Liferay fails to shutdown properly I want to kill the process.
target to kill Liferay process.
The killLiferayIfRunning target is very similar to our stopLiferayIfRunning task. It has the same dependencies and if property. One difference is the pgrep command used only returns the PID of the Liferay process. Another is instead of trying to properly shutdown Liferay we issue a kill command to the OS to terminate the process. In the following build file we introduce a new task. Antcall is used to run targets. The following brings what we have discussed together.
target to kill Liferay process.
Using the antcall task we run the stopLiferayIfRunning target followed by running killLiferayIfRunning target three times. Both targets check to see if Liferay is running. If it is not running, they do nothing. The next part in this article will cover starting a Liferay server and monitoring for a successful startup.