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.
Archived Comments
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.
If it works - sure. I'd just get the willies.
The question is why keep restarting the server? solve the problem itself.
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.
I don't think that is a question Bruno - the reader knows he needs to solve the problem. This was just a temporary solution.
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.
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.
Have you tried running varScoper?
@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!
Under load it can cause all kinds of problems. I'd recommend fixing them asap. Shouldn't take long.
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.
<cfloop index="FLOOP" list="#VALUELIST(QYEARDATA.FACILITYID)#">
<cfset var "TEMP_FIDCOUNT_#FLOOP#" = '' />
</cfloop>
Is there a correct syntax for the cfset statement above or am I trying to do something that's not possible?
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#"] = "">
As always, you rock. ;-) Thanks!
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?
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.