5

I am trying to configure an EC2 Windows 2008 server instance to automatically start a batch file after the instance is restarted (to connect to Jenkins). I have placed a shortcut to the batch file in the Startup folder in the Windows menu.

The problem I am seeing is that the instance does not start the batch file until I actually login to the instance with remote desktop. As soon as I log in to the instance, the file is executed.

How do I make sure that the batch file is started as soon as the operative system is loaded?

djonsson
  • 603
  • 5
  • 14

1 Answers1

3

I figured it out. The trick was to automatically log in after the instance is restarted:

  1. Open the Start Menu, type netplwiz in the search box, and press Enter.
  2. Untick Users must enter a user name and password to use this computer
  3. Enter the windows password in the confirmation dialogue

The instance will now run the items in the Startup folder in Windows.

If this is not configured on the instance, the login will actually happen when you remote desktop into the instance.

djonsson
  • 603
  • 5
  • 14
  • 3
    This is a really bad idea, especially on a Cloud machine - if you ever poke an accidentally-imprecise hole in your Security Group, expect to be compromised with rapidity. Undo this change ASAP and instead create a Scheduled Task to run 'At Startup' which points to your batch file and runs with sufficient permissions (and no more) for it to do its' thing. – Eight-Bit Guru Feb 17 '14 at 16:35
  • @Eight-BitGuru This did not work for me. I tried many configurations of task scheduler and for some it says 'Running' but the task clearly is not running (as I would see a command prompt and task manager would show CPU usage). – stevec Jul 10 '19 at 15:20
  • @user5783745 If you have set the task to run 'At Startup', it runs before any user window session is created - therefore when you log-in and create a window session, it is not the 'System' session that the task is using, and you won't see any GUI activity from it. You might see it in Task Manager if it runs long enough to still be active by the time you are logged-in. – Eight-Bit Guru Jul 10 '19 at 15:54
  • @Eight-BitGuru the talk scheduler asked for my (administrator) credentials. Therefore when I login as administrator I should see it running no? – stevec Jul 10 '19 at 16:02
  • @user5783745 No. The credentials grant permission to do things, they don't confer window session accessibility. The task will be running in the 'System' session and will not be visible in any window session you create when you log-in. If you need to see output from your task, either write it to a logfile or (if you just need 'started' and 'stopped' alerts) into the Event Log. – Eight-Bit Guru Jul 10 '19 at 20:50
  • @Eight-BitGuru great knowledge. Thanks – stevec Jul 10 '19 at 21:48
  • @Eight-BitGuru I tried to log the output as you recommend. It works when I run the task myself. But when it runs using task scheduler (the task says 'Running') but there is no logged output at all. There should be if it is truely running. I am logging the output to `C:\Users\Administrator\Desktop\Output.txt` (and using [this](https://stackoverflow.com/questions/1215260/how-to-redirect-the-output-of-a-powershell-to-a-file-during-its-execution) method – stevec Jul 11 '19 at 01:35
  • I can see similar issues [here](https://serverfault.com/questions/848968/task-scheduler-scheduled-task-at-startup-not-working) – stevec Jul 11 '19 at 01:41
  • I am going to try [this](https://stackoverflow.com/questions/20575257/how-do-i-run-a-powershell-script-when-the-computer-starts) method. Wish me luck – stevec Jul 11 '19 at 02:33
  • @Eight-BitGuru I finally got it working using [this](https://stackoverflow.com/questions/20575257/how-do-i-run-a-powershell-script-when-the-computer-starts) method and suggestions [here](https://stackoverflow.com/questions/56998570/how-to-make-windows-ec2-user-data-script-run-again-on-startup) and [here](https://stackoverflow.com/questions/26158411/amazon-ec2-custom-ami-not-running-bootstrap-user-data) to help me figure out why the powershell script wasn't running. One last question. If I have automated a task, and I RDP into the instance, I can't see it running. So how could I cancel it? – stevec Jul 12 '19 at 07:31