Quick Tip - Restarting ColdFusion at an interval

This post is more than 2 years old.

Had a few quick emails with a reader today I thought I'd share. First, let's start with this question:

Is it possible to have Coldfusion server programmatically restart every X days?
The short answer is absolutely yes - just use your operating system's native scheduling capability. To be clear, I'm not talking about the ColdFusion Scheduler (not sure you want to use ColdFusion to restart itself), but the tools provided by the OS itself. On Unix based systems you have a CRON system that you can edit with a simple text file to set up a scheduled task. Write a quick shell script to run the command line to restart ColdFusion and you are all set. I haven't worked with CRON in a long time. I remember it being weird but once I got past the syntax it worked ok.

On the flip side, Windows has support for this too. You can use the Tool Scheduler which apparently got a little bit of an update in Windows 7:

After sharing this with the reader, he replied with:

Of course ideally I should really figure out why my server is going down every 3-4 days :P but right now pretty backed up and would rather reboot in middle of night than the random hours it's choosing now.

Couldn't agree more. It is an unfortunate fact of life that band aids tend to stick around forever. So if you are using this technique to get around some server issue, you definitely want to ensure you take the time to find out why.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Ed posted on 10/8/2010 at 10:07 PM

Is there anything wrong in restarting CF from itself? i used this approach many times and it worked fine. Did it both doing a call to jrunx.kernel.JRun and also as CFEXECUTE to a BAT file that do NET STOP/START.

Comment 2 by Raymond Camden posted on 10/8/2010 at 10:13 PM

If it works - sure. I'd just get the willies.

Comment 3 by Bruno Soares posted on 10/8/2010 at 11:07 PM

The question is why keep restarting the server? solve the problem itself.

Comment 4 by Tyler Clendenin posted on 10/8/2010 at 11:43 PM

I recommend not relying on restarting cf from itself, because if you need a restart, it is unlikely that the page you are running your restart from will respond anyway.

Comment 5 by Raymond Camden posted on 10/9/2010 at 12:34 AM

I don't think that is a question Bruno - the reader knows he needs to solve the problem. This was just a temporary solution.

Comment 6 by Jim Priest posted on 10/9/2010 at 12:47 AM

I've blogged about cron a bit and just recently on automating startup/shutdown of services - http://thecrumb.com/2010/09...

Need to finish that series of posts :)

I agree with the others - I'd figure out why it needs restarting first. Also remember if you are using any kind of framework, caching etc there will be a penalty on the first hit to your application(s) - so you may want to automate a web request with wget or something similar to get all your apps up and running after you have restarted things.

Comment 7 by JC posted on 10/9/2010 at 6:47 AM

I know exactly how this reader feels. I have an Intranet that worked fine with CF8. Once I upgraded, I've discovered that memory leaks are occurring and I have to restart the services every few days. I need to learn how to monitor pages in order to discover which code is causing the server to leak now. I would love to hear if anyone else is experiencing this and the methods they use to find the "bad" code. Definitely a CF9 thing for me.

Comment 8 by Raymond Camden posted on 10/9/2010 at 6:51 AM

Have you tried running varScoper?

Comment 9 by JC posted on 10/9/2010 at 6:23 PM

@Ray - I just downloaded varScoper this morning. It's finding lot of unscoped variables in my components. Do unscoped variables really cause all these memory leaks? I'm green when it comes to setting local variables as opposed to global. Have you discussed how to use local variables to help with memory leaks? Thanks!

Comment 10 by Raymond Camden posted on 10/9/2010 at 7:02 PM

Under load it can cause all kinds of problems. I'd recommend fixing them asap. Shouldn't take long.

Comment 11 by JC posted on 10/10/2010 at 7:08 PM

So far, so good. However, I have a dynamic variable that's getting set inside a loop. So I expected the following code to work, but it throws an error.

<cfset var "TEMP_FIDCOUNT_#FLOOP#" = '' />

Is there a correct syntax for the cfset statement above or am I trying to do something that's not possible?

Comment 12 by Raymond Camden posted on 10/10/2010 at 7:12 PM

In CF8, var statements must come first. In CF9, they can be anywhere in the method. However, you want to change your syntax to make use of the local scope. The local scope is the same as the var scope.

<cfset local["TEMP_FIDCOUNT_#FLOOP#"] = "">

Comment 13 by JC posted on 10/10/2010 at 7:56 PM

As always, you rock. ;-) Thanks!

Comment 14 by Josh posted on 10/24/2010 at 8:37 PM

Was reading through the comments, and a question came to mind. I have an app that used to run on CF8 and now lives on a CF9 box. When it was on CF8, I made use of the <cfset var local = StructNew()> trick in most of my components. Now that it's running on CF9, am I screwing myself by creating that faux-local struct?

Comment 15 by Raymond Camden posted on 10/24/2010 at 8:39 PM

Because so many people made use of var local, Adobe tried to make it work anyway. That's what I've heard. So if you are having an issue, it may not be that.