app启动&退出脚本实现

#__author: Administrator
#date: 2018/6/5
#usr/bin/python
#encoding:utf-8
import os
import time
#app类
class App(object):
    def __init__(self):
        self.content=''
        self.startTime=0
    #启动App
    def LaunchApp(self):
        cmd='adb shell am start -W -n com.android.settings/.MainSettings'
        self.content=os.popen(cmd)
    #停止App
    def StopApp(self):
        cmd='adb shell am force-stop com.android.settings'
        os.popen(cmd)
    #获取启动时间
    def GetLaunchedTime(self):
        for line in self.content.readlines():
            if 'ThisTime' in line:
                self.startTime=line.split(':')[1]
                break
        return self.startTime
#控制类
class Controller(object):
    def __init__(self,count):
        self.app=App()
        self.counter=count
        self.alldata=[('timestamp','elapsedtime')]
    #单次测试过程
    def testprocess(self):
        self.app.LaunchApp()
        time.sleep(5)
        elapsedtime=self.app.GetLaunchedTime()
        self.app.StopApp()
        time.sleep(3)
        currenttime=self.getCurrentTime()
        self.alldata.append((currenttime,elapsedtime))
    #多次执行测试过程
    def run(self):
        while self.counter>0:
            self.testprocess()
            self.counter=self.counter-1
    def getCurrentTime(self):
        currentTime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
        return currentTime
    def SaveDataToCSV(self):
        csvfile=open('startTime','w')
        for i in self.alldata:
            csvfile.write(str(i)+'\n')
        csvfile.close()
if __name__=='__main__':
    controller = Controller(2)
    controller.run()
    controller.SaveDataToCSV()


在Windows系统中,可以通过PowerShell脚本实现限制某个进程重复启动的需求。这通常依赖于检查当前运行的进程是否已经存在,如果存在则跳过启动逻辑,否则继续执行程序。 ### 使用 `Get-Process` 检查进程是否存在 PowerShell 提供了 `Get-Process` 命令,可以用于列出当前运行的进程。通过结合 `Where-Object` 或 `Select-Object`,可以筛选出特定的进程名称。如果发现该进程已经在运行,则可以退出脚本,避免重复启动。 以下是一个示例脚本: ```powershell # 检查进程是否已经运行 $processName = "notepad" $existingProcess = Get-Process -Name $processName -ErrorAction SilentlyContinue if ($existingProcess) { Write-Host "程序已经在运行,退出脚本" exit } # 如果未运行,则启动程序 Start-Process -FilePath "notepad.exe" ``` 上述脚本中,`Get-Process` 命令用于检查指定名称的进程是否正在运行。如果找到匹配项,说明程序已经在运行,脚本退出;否则继续执行启动程序的命令。 ### 使用临时文件标识进程状态 另一种方法是利用文件系统来标识程序是否已经启动脚本启动程序前检查某个特定的临时文件是否存在,如果存在则退出,否则创建该文件并启动程序。 ```powershell $lockFile = "C:\Temp\YourApp.lock" # 检查锁文件是否存在 if (Test-Path $lockFile) { Write-Host "程序已经在运行,退出脚本" exit } # 创建锁文件 New-Item -Path $lockFile -ItemType File -Force | Out-Null # 启动程序 Start-Process -FilePath "C:\Path\To\YourApp.exe" # 添加清理锁文件的逻辑(可选) # Remove-Item -Path $lockFile -Force ``` 此方法适用于需要在脚本层面实现进程单实例控制的场景。需要注意的是,锁文件可能因程序异常退出而残留,因此建议在程序正常退出时清理该文件,或在脚本中加入自动清理机制。 ### 使用注册表键值标识程序运行状态 还可以通过注册表键值来标记程序是否正在运行。PowerShell 脚本可以使用 `Set-ItemProperty` 和 `Get-ItemProperty` 命令操作注册表,实现进程的唯一性控制。 ```powershell $regKey = "HKCU:\Software\YourApp" $regValue = "Running" # 检查注册表键是否存在 if (Get-ItemProperty -Path $regKey -Name $regValue -ErrorAction SilentlyContinue) { Write-Host "程序已经在运行,退出脚本" exit } # 创建注册表键值 Set-ItemProperty -Path $regKey -Name $regValue -Value 1 # 启动程序 Start-Process -FilePath "C:\Path\To\YourApp.exe" # 程序退出后删除注册表键值(可选) # Remove-ItemProperty -Path $regKey -Name $regValue ``` 此方法适用于需要持久化记录程序运行状态的场景,且适合与系统集成较深的应用。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值