ITPub博客

首页 > 应用开发 > Python > Python脚本用于定时关闭网易云音乐PC端

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

原创 Python 作者:juyxii 时间:2020-09-12 10:18:42 0 删除 编辑

       背景:昨晚发现网易云音乐的PC客户端没有定时关闭的功能,可以使用Python编写一个简单的脚本,用于定时关闭这样的计划任务。经过改良后,可以对此做一些有用的扩展,用于日常运维中。

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

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

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

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

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

  脚本运行原理:

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

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

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

  此脚本涉及的知识:

  获取系统语言默认编码

  枚举和kill 进程

  获取当前用户的用户名

  实现倒计时功能

  实现计划任务功能

  Python多线程执行

  运行环境与使用方法:

  Python 2.7

  psutil

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

  其中,在脚本的__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


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69934488/viewspace-2718997/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-08-28

  • 博文量
    11
  • 访问量
    9810