Forums

Running a script continuously in the background

I have a script which has to run continuously through an infinite loop which checks again and again for certain conditions. If I add it in scheduled task than it will be 're-executed' at scheduled times but I want it to run continuously. How to do it?

https://help.pythonanywhere.com/pages/LongRunningTasks

So if I add my task in the scheduled task and make it run, say every 1 minute. Than will the task scheduler automatically exit if it sees that the process from the previous spawn is still running? Or do I have to use that socket code?

Hi, I tried that code. Just to test it, I ran it from one bash console. It acquired the lock(obviously) and continued running as expected. But than again I ran it from another console and it ran again. How can it be? It should fail to acquire the lock right?

Or does PA consoles run on 'virtual' servers which don't share sockets so that's the reason sockets are failing to not acquire locks?

That's almost exactly right; consoles can run on any one of a cluster of console servers, so there's no guarantee that the lock will work for them. But scheduled tasks are guaranteed to run on the same server for each user, so it does work with them.