Typically, the objective with a Foxtrot project is to start up, perform a set of actions either once or according to specific number of records (a loop), and then close down to make room for the next job in line. Therefore, you will typically implement a Close Me action at the end of the script and in your error task to make sure to close down the Foxtrot or FoxBot application when the project is complete.
Restarting Foxtrot or FoxBot is relevant if you have a project that should run for a very long time or 24/7. For example, if you have a script overviewing an email inbox to wait for an incoming email and perform the process when it arrives. In this case, you need it to be ready as soon as the email comes in, therefore, you will implement a forever loop in the project to have it run 24/7 and only stop in certain scenarios. In this type of project, it is best practice and highly recommended to restart Foxtrot or FoxBot once in a while to avoid the machine executing the project to, for example, run out of memory. It is always good to completely close down Foxtrot or FoxBot once in a while and reopen and -run the project. This is something you can implement in the script itself and decide how often it should be executed.
At the end of the article, you can download a project file that showcases a best practice of how to restart Foxtrot and FoxBot. Simply put, you should implement a Write File and Open File to dynamically restart the project.
IMPORTANT: You should also implement the restart logic in an error task to make sure to restart the project or go to a label where it restarts in the event of an error. Otherwise, you risk that the script will never reach the point of restarting. Alternatively, you could schedule a bat file to run every X minutes that starts the project if it is not already running. This way, you make sure that the project is always running.
IMPORTANT: If you are working with a project that is supposed to run 24/7, it is important to consider implementing a scheduled job that starts the project every X minutes if Foxtrot/FoxBot is not running. For more details on how this is achieved, please see this article where it is also explained how you can make a bat file that only opens Foxtrot/FoxBot if it is not already running. This is important because if the unexpected problem of Foxtrot/FoxBot crashing occurs, there will be nothing rebooting the project unless you have a scheduled job to start Foxtrot/FoxBot if it is not running. Of course, you will have to implement some logic in the beginning of the project to properly handle a scenario where the previous execution left applications opened, etc.
The recommended actions
In the beginning of the project
We recommend creating a variable in the beginning of the script storing the current date and time of the moment of the project starting. You will be able to use this variable to take the decision at some point to restart the project after X minutes/hours.
The actions to restart the project
These are the recommended actions to use to actually perform the restart of the project.
Generating the bat file
In the Write File action, this is where you generate the bat file to perform the restart of the project.
Note, the rem lines are simply note lines. To understand the fourth line of the bat file, read this article.
rem This first line allows the project to complete before closing it
rem This second line kills the process
taskkill /F /FI "USERNAME eq %username%" /IM [*APP_FILE]
rem This third line waits for a second before reopening the project
rem This fourth line reopens the project and runs it
start "[*APP_NAME]" "[*APP_PATH]" /Open [*PROJECT_PATH] /Run
When to restart?
After setting up the actions, make sure to test them, you need to decide where and how often you restart the project. Since the restart process only takes 5-10 seconds, depending on how fast your machine is capable of opening the project, a general rule of thumb is to restart the process every 1-2 hours. In the project at the end of the article, it is illustrated how you can place the restart actions at the end of the project and then inside of the forever loop create an If-statement based on the number of minutes that the project has been running for.
Of course, you can set up whatever logic suits your needs. But, it is important to restart the project a sufficient number of times during a day to avoid execution issues.