Configure the startup of the Python script in Windows

Source: Internet
Author: User
ArticleDirectory
    • I. Implementation of Start Menu startup items
    • Ii. Boot script
    • 3. Call the script through a service

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 (PythonCode):

ImportTimefout= Open ('E: \ 1.txt','W')WhileTrue: TMP='% D-% 02d-% 02d % 02d: % 02d: % 02d \ r \ n'% Time. localtime () [0: 6]PrintTMP 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 ):
  Import   timefout  = open ( '  E: \ 1.txt  ',  '  W   ' ) TMP  =  '  % d-% 02d-% 02d % 02d: % 02d: % 02d \ r \ n   ' % time. localtime () []   Print   tmpfout. 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 ):

 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;

 
Servicestartshell.exe c: \ python27 \ python.exe E: \ test1.py/install

C. Uninstall the service;

 
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.