Always-On Task Stops After a few Hours

Hi - one of my scripts I’m trying to run in an AlwaysOn task stops after a few hours. The script starts ok and runs for a bit but then output stops. I can run the script in a Bash console just fine with no hang up or pausing for as long as the console doesn’t reset.

I have other scripts that are able to run ok as an AlwaysOn task that are Telegram bots but the non-Telegram bot script hangs.

Not sure if this is the right place to seek help. Please let me know.

Many thanks in advance.

When it hangs, does is look like it's still running when you use the "Running processes" table at the bottom of the "Tasks" page? Also -- you mention that the output stops, so I'm guessing that you have some print statements in the script that should track what it is doing. Are you using the "flush=True" kwarg in those prints? They should look like this:

print("Some logging message", flush=True)

The extra argument tells Python to write the logging output directly to the disk, which is important because otherwise it can just buffer stuff up in memory, only writing to the disk when the buffer fills up -- which can make it look like it's hanging when it's actually just not writing the log lines.

Hi thanks for the reply. Yes in the Tasks pane it still shows “running”. The script sends messages to a database that are then picked up and sent to Telegram group as well as printing status messages to the console. I’ll update the print statements to include the flush, but it’s apparent when it stops/hangs because the messages stop being sent to the db/Telegram. What’s strange is when I run the script in the console it drums fine with no hanging. It’s on a loop that should trigger its action every few seconds.

Thanks again.

I think that adding the flush keywords, and perhaps adding some extra prints, are your best bet to tracking down what's going wrong -- I suspect that some unusual path in your code is leading it into an infinite loop or sleep for some reason.

Will give it a shot - appreciate the advice. Any ideas on why it runs ok in a console?

It's hard to tell without seeing the code. Does it always hang at the same point in your code?

Not entirely sure. I'm adding the flush = True to print statements for more real-time feedback and will hopefully have some more information based on that.

Thanks for letting us know

Just wanted to give you guys an update, the flush statements certainly helped with more real-time statements with the script and I think I found a bug that was presenting very infrequently. Correcting that seems to have resolved the issue. The script ran well overnight, so fingers crossed.

Thanks again for your quick replies.

Excellent, very glad to hear we could help!