---
title: "开机隐藏cmd运行程序"
author: "dongin"
date: "2007-12-03T16:14:02Z"
tags: []
slug: "cmd"
draft: false
lastmod: "2007-12-03T16:14:02Z"
id: 145622
url: "https://openexperience.iteye.com/blog/145622"
---

如果有个程序运行的时候不想让它显示cmd界面，比如我正在做的一个网吧项目就需要这样。下面举个小例子来测试一下：  
先建一个python文件test.py，代码如下：

python 代码

1. if \_\_name\__ == "\_\_main\_\_":
2. import time
3. while True:
4. file = open('test.txt', 'a')
5. file.write('run in background\\n')
6. file.close()
7. time.sleep(1)
8. print "it's running..."

接着写个批处理文件(test.bat），内容如下：

python 代码

1. python test.py

如果运行这个批处理文件，可以看见在cmd中一直打印"it's running..."，  
最后关键部分就是写一个test.vbs文件，内容如下:

python 代码

1. Set ws = CreateObject("Wscript.Shell")
2. ws.run "cmd /c test.bat",vbhide

双击test.vbs后test.py就会在后台执行而部显示cmd，可以从test.txt或进程中看出test.py正在运行。最后要做的就是把test.vbs拖到 windows的‘’启动“力即可。