Using Task Scheduler When Internet is Off?

Gauri Kshirsagar 1 Reputation point
2023-01-07T01:29:18.963+00:00

I have a python script that requires the use of internet to work (it's supposed to send an email/text).

I want to use Task Scheduler to schedule it to run daily at a specific time. I have already set a condition to run if a network connection is available. However, when the computer is off - it will execute, but return the following error: The process terminated unexpectedly - so, in the end, my program will just not execute for that day.

Is there a way to specify that if running the program at a specific time returns a "terminated unexpectedly error" then Task Scheduler should wait to detect an internet connection and then run the program again?

Or - is there another way for me to execute this program daily, any other way that Task Scheduler can do this, or using a different program entirely, or somehow finding a way to keep my internet on even when the laptop is shut off?

Thanks so much!

Windows for business | Windows Server | User experience | Other
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-01-07T03:14:28.693+00:00

    Add the Python equivalent of Powershell's Test-NetConnection to your script and sleep for a few seconds and retest.

    if (!(Test-NetConnection www.microsoft.com -CommonTCPPort HTTP).TcpTestSucceeded) {  
          start-sleep -seconds 60  
    }  
    

    If the network is not available, the script will just keep trying (sleeping) and when the PC gets back online, it will continue to the next statement and send the email.

    I am not familiar with Python, but I found this with a simple search.

    https://pytutorial.com/python-check-internet-connection

    0 comments No comments

  2. Limitless Technology 44,751 Reputation points
    2023-01-10T08:18:13.187+00:00

    Hello there,

    You can use Task Scheduler to configure your python script to run again if it terminates unexpectedly. Here's how:

    1. Open Task Scheduler and locate the task that runs your python script.
    2. Right-click the task and select Properties.
    3. Go to the Actions tab.
    4. In the Actions list, click the action that runs your script.
    5. In the Properties dialog, click the Conditions tab.
    6. Under the Restart if the task fails section, select "1" from the Restart every dropdown. This will cause the task to be restarted if it fails.
    7. Click OK to save the changes.

    Alternatively, you could use a different program to schedule your python script to run daily. For example, you could use cron on Linux or launchd on Mac to schedule your script to run at a specific time.

    As for keeping your internet connection active when your laptop is shut off, it depends on the type of internet connection you have. If you have a broadband connection (e.g. cable, DSL), you could try using a modem with a built-in router that supports "always on" mode. This would allow you to keep your internet connection active even when your laptop is shut off. If you have a different type of internet connection, such as a mobile hotspot, you may need to find a way to keep the device that provides the internet connection powered on.

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.