2

I am running an AWS Windows 2012 EC2 instance that has to run 24/7. On this instance, I run a Python 3.6 scraper script and to prevent me from having to regularly check up on the server whether the file is running, I have a .bat file in the shell:startup folder of my instance, that automatically restarts it on a daily base. The .bat file works as it will run the Python script and set a timer to restart/reboot the instance after (t=86400). The .bat file runs on the EC2 instance itself.

However, what the file does not do is run automatically after the reboot. I now first have to remote connect to the server before the .bat file will run. What I want it to do is run without me having to first remote connect into the server. How can I achieve this?

I use the following code in my .bat file. Located on my EC2 instance.

@ECHO OFF
START CMD /K (
        CD C:/Users/Administrator/Documents/
        python scraper.py
)
START CMD /K SHUTDOWN -t 86400 -r -f

I have tried looking into using AWS' Automations and other schedule based methods but couldn't get that to work.

MrDuk
  • 16,578
  • 18
  • 74
  • 133
Oliver
  • 81
  • 2
  • 11
  • 1
    I'm not sure if I understand your issue, does the batch file not contain a command which first connects to the server? or does it have the command, but fail to execute it successfully? Perhaps editing your question complete with the content of your batch file may help! – Compo Feb 02 '18 at 13:19
  • Thanks, changed it. The batch file is located on the server and simply does not run automatically after rebooting. I want the script to reboot the EC2 instance and then automatically run the batch file after the reboot. Now it will only reboot, but after the reboot the file does not automatically run. It will only run when I remote connect into the EC2 instance again. – Oliver Feb 02 '18 at 13:28
  • Did you try setting up a CloudWatch event every 24 hours (I think?) to reset your server? That way you could send a 'reboot' command to the EC2 instance without the need for that script. – Y. Hernandez Feb 02 '18 at 13:55
  • Good idea, but the point is that I need the `.bat` file to run the Python script and set the timer after the reboot – Oliver Feb 02 '18 at 13:57

2 Answers2

0

If you want to use something native to Windows Server 2012, look at Schtasks -- this is more or less the Windows equivalent of cron.

MrDuk
  • 16,578
  • 18
  • 74
  • 133
0

I found the answer to my question by using Task Scheduler and looking at the following article: Run a batch file with Windows task scheduler

An important note here is that for my batch file to run I had to create a task that started CMD and run the batch file from there. Asking Task Scheduler to run the batch file directly doesn't work on Windows Server 2012. I ran the task with the following details:

  • Administrator account
  • "Run whether user is logged on or not"
  • "Run with the highest privileges"
  • "Start on system start-up"
  • Action: Start a program -> CMD
  • Add arguments (optional): /c start "" "C:\Users\Administrator\Desktop\file.bat"

More information on how to do this can be found in this answer: https://stackoverflow.com/a/27055435/7736676

Oliver
  • 81
  • 2
  • 11