How to configure python scripts for startup in Windows

Source: Internet
Author: User
This article mainly introduces three methods for configuring a python script for Windows to start up. This article describes how to implement a Start menu, a boot script, and how to call the script through a service, for more information, see test environment: windows Server 2003 R2.

I. implementation of Start Menu startup items

You must log on to the console to perform this operation.

Test script (python code ):

The code is as follows:


Import time
Fout = open ('E: \ 1.txt ', 'w ')
While True:
Tmp = '% d-% 02d-% 02d % 02d: % 02d: % 02d \ r \ n' % time. localtime () [0: 6]
Print tmp
Fout. write (tmp)
Fout. flush ()
Time. sleep (5)

1. general operations

1.1 create a shortcut;

1.2 add the created shortcut to the Start menu;

1.3 boot verification;

2. hide the startup of the command line window

The above operation method has a command line window, which is not practical in some cases. we can remove the command line window in the following two ways.

2.1 Change the python script file extension to ". pyw"

Other operations are similar to the above process and will not be described here.

2.2 start with a script such as vbs

The vbs code is as follows:

Set ws = CreateObject ("Wscript. Shell ")
Ws. run "cmd/c E: \ test1.py", vbhide
The path is configured according to the actual situation. other operations are similar.

If you do not want to use shortcuts, you can directly add the script to the startup item.

Appendix:

Configure automatic account logon

To enable the Start menu, you must ensure that you log on to the system. here is a method for automatic account logon.

A. type "Rundll32 netplwiz. dll, UsersRunDll" in the running box ";

B. open the user account interface and remove the check box before "to use the local machine, the user must enter the user name and password". press "OK" and enter the user name and password for automatic logon;

II. boot script


Cycle is not supported. it is best to configure the timeout time.
Test Code (python ):

The code is as follows:


Import time
Fout = open ('E: \ 1.txt ', 'w ')
Tmp = '% d-% 02d-% 02d % 02d: % 02d: % 02d \ r \ n' % time. localtime () [0: 6]
Print tmp
Fout. write (tmp)
Fout. close ()

The procedure is as follows:

A. enter gpedit. msc in the running state to open the group policy editor;
B. select "computer configuration" => "Windows settings" => "script" => "start;

C. select a script;

D. configure the maximum waiting time for the script, path: "Computer Configuration" => "management template" => "system" => "script" => "maximum waiting time for group policy scripts ";

3. call the script through a service

A. write a script to start serviceStartShell. the code is as follows (only the code of main function is listed here ):

The code is as follows:


Int main (int argc, char * argv [])
{
Init ();
DwThreadID = GetCurrentThreadId ();
SERVICE_TABLE_ENTRY st [] =
{
{SzServiceName, (LPSERVICE_MAIN_FUNCTION) ServiceMain },
{NULL, NULL}
};
// Printf ("argc = % d \ n", argc );
If (4 = argc) & 0 = stricmp (argv [3], "/install "))
{
Install (argv [1], argv [2]);
WriteReg (argv [1], argv [2]);
}
Else if (2 = argc) & 0 = stricmp (argv [1], "/uninstall "))
{
Uninstall ();
}
Else
{
If (! StartServiceCtrlDispatcher (st ))
{
// Printf ("Register Service Main Function Error! ");
}
}
Return 0;
}


B. service installation;

The code is as follows:


ServiceStartShell.exe C: \ Python27 \ python.exe e: \ test1.py/install


C. uninstall the service;

The code is as follows:


ServiceStartShell.exe/uninstall

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.