¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

handle restarts


 

What I am hoping to do is to create a script that will run say every 5 minutes. It will check if TWS is running. If its running then it should just quit if its not running then it will launch TWS. ANy clue on how to detect if TWS is currently running. I am on windows and the task manager just shows as Java. There is no way to tell its TWS or some other java application.


 

what? I have done is that I assume that when TWS is running, its through IBC. IBS launches a shell with the title "IBC (TWS 1020) - C:\IBC\scripts\DisplayBannerAndLaunch.bat"
So I try to locate if I can find a open cmd shell with that title. If it finds it, then exit else attempt to launch TWS.

I used this script to detect if I had an open shells that matched the title


 

¿ªÔÆÌåÓý

Actually you can achieve this just using Task Scheduler, if you configure the task correctly.

?

I don¡¯t know if you¡¯ve seen the section on running under Task Scheduler in the User Guide: if not, I suggest you read it.

?

However I just realised today that this section contains some errors. I¡¯ll try to send out an update to it later tonight.

?

Basically all you need to do is to set the task to run every (week)day at a particular time, and to restart the task every 5 minutes (for a duration of say 1435 minutes); also set the task to not run if there is already an instance running. And make sure you use the /INLINE parameter to the start script. And configure AutoRestart time to a suitable time (ignore the stuff in the current User Guide description about not using AutoRestart ¨C it¡¯s wrong)..

?

Hopefully that will be enough to get you going, and if not then my revised description should help (when I¡¯ve written it!...).

?

Richard

?

?

?

From: [email protected] <[email protected]> On Behalf Of nkulki@...
Sent: 16 December 2022 16:38
To: [email protected]
Subject: Re: [ibc] handle restarts

?

what? I have done is that I assume that when TWS is running, its through IBC. IBS launches a shell with the title "IBC (TWS 1020) - C:\IBC\scripts\DisplayBannerAndLaunch.bat"
So I try to locate if I can find a open cmd shell with that title. If it finds it, then exit else attempt to launch TWS.

I used this script to detect if I had an open shells that matched the title


 

¿ªÔÆÌåÓý

Here is the revised User Guide description for running IBC under Windows Task Scheduler:

?

Scheduled Tasks (Windows only)

?

On Windows you can start IBC automatically using the Task Scheduler to run

StartTWS.bat or StartGateway.bat.

?

When you define your task, make sure that the option to 'Run only when user

is logged on' is selected. Doing this will ensure that you can see and interact

with TWS.

?

You will then need to log on to Windows before the task runs.

?

Note that you can set up Windows to log on automatically at startup: this might

be useful, for example, if your system's BIOS allows you to configure the

system to power on at a particular time. Information on how to do this is

freely available on the internet. But bear in mind that doing this can

negatively impact your system's security.

?

Task Scheduler does actually allow you to specify that your task should run

whether or not the user is logged in. However if you do this, the task is

always started in a separate user session which you cannot see and interact

with, even if you are already logged on when the task starts, or if you

subsequently log on. Therefore you are strongly advised NOT to use the option

for 'Run whether user is logged on or not'.

?

Remember also to change the task settings to prevent Windows automatically

ending it after a certain time.

?

You can set the AutoRestart time in the Lock and Exit section of the

configuration dialog: this causes TWS/Gateway to automatically shut down and

restart without requiring re-authentication at the specified time. When the

restart time is reached, TWS shuts down (and IBC with it), but this does not

end the task, because the StartTWS.bat or StartGateway.bat script continues

running to restart IBC. The restarted IBC then reloads TWS with the relevant

information needed for it to recover its previous session without re-

authentication. This sequence is then repeated each day at the same time. Thus

TWS can be kept running all week, with automated startup and a single

authentication at the start of the week. Note that this is all the same task,

since the start script run by the Task Scheduler keeps running all the time.

?

Finally on the Sunday, if the task has not been ended before then, IB will

prevent that session running any further because the session credentials expire.

At this point it is necessary to start a new task to begin the whole cycle over

again.

?

Since there is little point having TWS running after Friday evening (because

the markets are closed), you can use the ClosedownAt setting in config.ini

to tidily shut down TWS automatically after the Friday trading session has

finished.

?

Note that TWS's auto-restart mechanism does not operate if TWS is shut down

other than at the auto-restart time: for example via the File | Exit menu, or

due to power failure or a program bug. This situation can be handled by

configuring the task to run periodically (say every 10 minutes) during the week

so that if TWS crashes or is manually shut down, the task is automatically

restarted. Make sure the task is also configured to prevent a new instance if

one is already running.

?

Note also that if you set up the task to run at user logon, and you configure

your computer's BIOS to power on when power is restored after failure, and to

then log on automatically, this will ensure TWS is restarted after a power

outage. (Information about how to make your computer log on automatically is

easily available on the internet: but make sure you understand the security

implications of autologon to Windows).

?

**IMPORTANT** Make sure you use the /INLINE argument to StartTWS.bat or

StartGateway.bat when starting IBC from Task Scheduler. Otherwise IBC starts

and runs correctly, but Task Scheduler is not aware of it: in particular Task

Scheduler does not show the task as running. This prevents correct operation of

Task Scheduler features such as killing the task after a specified elapsed

time, and periodic restarts as described above will result in multiple IBC

instances being started, with unpredictable results. The reason for this is

that if `/INLINE` is not used, the start scripts create a new window to run

IBC in, and Task Scheduler is not aware of this, so the task ends as soon as

this new window has been created.

?

A sample scheduled task is included in the IBC distribution ZIP,

called Start TWS Live (daily).xml. You can import this into your Task

Scheduler if you are running Windows. After importing it, you will need to

enable it and change the user account it runs under. This task starts TWS daily

from Sunday to Thursday at 22:15, and assumes that TWS is set to autologoff

shortly before this. It restarts the task every 10 minutes. You can adjust

these times to suit your needs.

?

Richard


 

There may be a better way, but I use an autoHotKeys script to check if the IB Gateway window is open and run the script to launch IB Gateway if it¡¯s not there.

It has performed well this way for over a year.



On Fri, Dec 16, 2022 at 11:07 AM <nkulki@...> wrote:
What I am hoping to do is to create a script that will run say every 5 minutes. It will check if TWS is running. If its running then it should just quit if its not running then it will launch TWS. ANy clue on how to detect if TWS is currently running. I am on windows and the task manager just shows as Java. There is no way to tell its TWS or some other java application.