您现在的位置是:首页 > 文章详情

Python脚本用于定时关闭网易云音乐PC客户端

日期:2017-04-07点击:515

本文主要讲述如何使用Python在指定的秒数后关闭Windows上运行的程序(此程序以网易云音乐为例)。本文的背景是昨晚发现网易云音乐的PC客户端没有定时关闭的功能,可以使用Python编写一个简单的脚本,用于定时关闭这样的计划任务。经过改良后,可以对此做一些有用的扩展,用于日常运维中。

为什么使用Python来做这件事?

用cmd、计划任务或者批处理做这件事不可以吗?如果说忽略过程,只看结果的话,这些方式确实可能更简单也能达到目的,但是通过Python来做可以从过程和结果两个方面获得很多好处:

  1. 可以拿来练手,熟能生巧,而且很多东西不用就忘记

  2. 控制程序的行为上更加灵活,想输出什么就输出什么,想扩展功能可以扩展功能,如写入日志等

  3. 移植性和复用性比较好,还可以用到Linux和Mac OSX

脚本运行原理:

1.使用python内置模块sched实现计划任务

2.使用psutil模块实现枚举和kill进程

3.使用thread模块并行执行两个阻塞任务

此脚本涉及的知识:

  1. 获取系统语言默认编码

  2. 枚举和kill 进程

  3. 获取当前用户的用户名

  4. 实现倒计时功能

  5. 实现计划任务功能

  6. Python多线程执行

运行环境与使用方法:

  1. Python 2.7

  2. psutil

  3. 使用python直接运行此脚本即可

  4. 其中,在脚本的__main__中可以修改时间(多少秒后执行)和进程的名字

运行结果截图:

这是设置的10s后关闭网易云音乐的运行截图

wKiom1jnLsPB27zQAAJY8Y_udxY871.jpg

说明:

第一行显示当前运行时的时间;

第二行会实时显示当前时间和剩余的小时、分钟和秒数;

第3、4、5、6行表示查到到进程并杀死进程;

最后两行打印结束时的时间和退出信息;

脚本内容:

脚本可以从GitHub上的LinuxBashShellScriptForOps项目中获取,并获得更新和错误修正版本。

https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/projects/WindowsSystemOps/System/pyScheduleTask.py

脚本内容如下:

#!/usr/bin/python # encoding: utf-8 # -*- coding: utf8 -*- """ Created by PyCharm. File:               LinuxBashShellScriptForOps:pyScheduleTask.py User:               Guodong Create Date:        2017/4/6 Create Time:        22:33  """ # https://docs.python.org/2/library/sched.html import threading import sched import time import sys import locale import codecs def get_system_encoding():     """     The encoding of the default system locale but falls back to the given     fallback encoding if the encoding is unsupported by python or could     not be determined.  See tickets #10335 and #5846     """     try:         encoding = locale.getdefaultlocale()[1] or 'ascii'         codecs.lookup(encoding)     except LookupError:         encoding = 'ascii'     return encoding DEFAULT_LOCALE_ENCODING = get_system_encoding() def shutdown_NetEaseCloudMusic(name):     # define NetEaseCloudMusic process name     ProcessNameToKill = name     print     import psutil     import sys     # learn from getpass.getuser()     def getuser():         """Get the username from the environment or password database.         First try various environment variables, then the password         database.  This works on Windows as long as USERNAME is set.         """         import os         for username in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):             user = os.environ.get(username)             if user:                 return user     currentUserName = getuser()     if ProcessNameToKill in [x.name() for x in psutil.process_iter()]:         print "[I] Process \"%s\" is found!" % ProcessNameToKill     else:         print "[E] Process \"%s\" is NOT running!" % ProcessNameToKill         sys.exit(1)     for process in psutil.process_iter():         if process.name() == ProcessNameToKill:             try:                 # root user can only kill its process, but can NOT kill other users process                 if process.username().endswith(currentUserName):                     process.kill()                     print "[I] Process \"%s(pid=%s)\" is killed successfully!" % (process.name(), process.pid)             except Exception as e:                 print e def display_countdown(sec):     def countdown(secs):         """         blocking process 1         :param secs: seconds, int         :return:         """         current_time = time.strftime("%Y-%m-%d %H:%M:%S %Z").decode(DEFAULT_LOCALE_ENCODING).encode("utf-8")         print "Time current: %s" % current_time         while secs:             now = time.strftime("%Y-%m-%d %H:%M:%S %Z").decode(DEFAULT_LOCALE_ENCODING).encode("utf-8")             hours, seconds = divmod(secs, 3600)             minutes, seconds = divmod(seconds, 60)             clock_format = '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)             sys.stdout.write('\rTime now: %s Time left: %s' % (now, clock_format))             sys.stdout.flush()             time.sleep(1)             secs -= 1     # set a human readable timer here, such as display how much time left to shutdown     countdown(int(sec)) def display_scheduler(name):     """     blocking process 2     :return:     """     s = sched.scheduler(time.time, time.sleep)     s.enter(10, 1, shutdown_NetEaseCloudMusic, (name,))     s.run()     now = time.strftime("%Y-%m-%d %H:%M:%S %Z").decode(DEFAULT_LOCALE_ENCODING).encode("utf-8")     print "Time finished: %s\nGood bye!" % now if __name__ == '__main__':     seconds_to_shutdown = 10     process_name_to_shutdown = "cloudmusic.exe"     threadingPool = list()     threading_1 = threading.Thread(target=display_countdown, args=(seconds_to_shutdown,))     threading_2 = threading.Thread(target=display_scheduler, args=(process_name_to_shutdown,))     threadingPool.append(threading_1)     threadingPool.append(threading_2)     for thread in threadingPool:         thread.setDaemon(False)         thread.start()     thread.join()

tag: python计划任务,python定时任务,python sched

--end--

原文链接:https://blog.51cto.com/dgd2010/1913818
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章