How to make a standalone script using Global.asa

Asked

Viewed 438 times

7

I’m looking to run a script that records information into the database every 24 hours. I’m not the server administrator, so I don’t have access to Scheduled Tasks, I read in some articles that I can use Global.asa for every time the site is visited (using Session_onstart), count 24 hours and run the script.

I tried to apply this form but it didn’t work; Follows the code in classic Asp:

<!-- #include file="etc/conexao.asp" -->

<script language="vbscript" runat="server">

Sub Session_OnStart
   'Runs on application start or after 15 minutes
   If Application("LastRun") = "" Or DateDiff(n, Application("LastRun"), Now()) > 15 Then

    set RS = server.CreateObject ("ADODB.Recordset")
    set RS2 = server.CreateObject ("ADODB.Recordset")

'exemplo de query que irá roda:
    RS2.Open "SELECT numero from PI_TESTES WHERE ID = 5",conn,1
    numero = RS2("numero")

    numero = numero + 2

    RS.Open "UPDATE PI_TESTES SET numero = '"&numero&"' "+_ 
    "WHERE ID = '5' ",conn,1

       Application("LastRun") = Now()
   End If
End Sub


</SCRIPT>

If anyone has any other solution, I am also willing to adopt.

  • Does it involve a web system? Can I propose a solution that uses your hosted site.

  • It’s intranet, I can’t use "external solutions/services" @Maiconcarraro :)

3 answers

3


The problem of using the Global.asa is that it does not guarantee that your script will run every time, if it is not constantly in use the IIS can stop the process after a while then it is not guaranteed that you will run the script.

If using Windows, you can use the task scheduler or by cmd the command schtasks.

Example:

schtasks /Create /SC DAILY /ST 10:00:00 /ET 10:10 /K /TN rotina /TR "'C:\Program Files\Internet Explorer\iexplore.exe' \"http://ip_intranet/aplicacao/rotina.asp""
  • /Create - Create the task.
  • /SC - Frequency that will perform the task, in case DAILY = Daily
  • /ST - Time to run the task in case 10h.
  • /ET - Time to finish the task, in case 10h10.
  • /K - Completes the task according to the ET.
  • /TN - Task name, in case 'routine'.
  • /TR - Command to be executed, in case it opens the browser of IExplorer and open the url "http://ip_intranet/aplicacao/rotina.asp"

What’s the trick here?

You create this page rotina.asp doing all the SELECT/UPDATE that it needs. So every time it performs the task it will open the page, making a request as long as it is on the same network intranet and open the rotina.asp.

  • 1

    Thank you. I contacted the server administrator and he ran the routine. Actually the global.asa is very buggy - All the experiments I tested on it didn’t work.

3

If it were ASP.Net it would be easier to use Quartz.net but in classic Asp it is more complicated. I suggest to you three "gambiarras" that you can analyze and choose the one that best suits you.

First:

  1. Schedule your personal machine to run the Asp page or vbs that does the service daily.

The problem here is that you have to be allowed to schedule the task and in case your machine gets shut down for some reason or gets off the network will not run.

Second:

  1. Create a free account on newrelic
  2. Add a monitor to Synthetics (Synthetics.newrelic.com) that runs every 24 hours calling your routine page.

This solution only works on sites that are public. That is, they can be accessed over the Internet, not working on intranets, and if your server is out at the scheduled time may not run.

Third:

  1. Ask the support team to schedule the server’s Recycle to be done every day at a certain time
  2. Put your code in the global application_start.asa

This solution will always run on the first access after Recycle, so the time may vary. Another problem is that it can run more than once a day and you will have to control it via code. But despite varying the time and can run more than once a day, this solution is the one that ensures that your code will run every day. If it is a public site you can create a monitor in newrelic, running every 1 minute by accessing any page of the site, ensuring that once it is done your code will run. And break, you will have an uptime/downtime report and your system performance.

Sorry to give weird solutions but classic Asp is complicated. I hope I helped.

  • Thanks for the response and dedication, unfortunately I can not use an external service because the site is on the intranet. But I will save your answer for future use!

2

I believe that a better solution would be to use a Scheduler in the event Application_Start of your application. This Scheduler will be fired only once and run every 24 hours.

The Quartz is a good example of a good tool for this.

You can also implement a scheduler, but it would be "reinventing the wheel", however a very interesting exercise.

  • Thank you for indicating the service!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.