Skip to content

Commit

Permalink
+ cancel crontab (-c), bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mojave-pku committed Oct 10, 2020
1 parent f6adc96 commit 410cbca
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
24 changes: 23 additions & 1 deletion cron.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import env_check
from crontab import CronTab
import os
import sys
import getopt


def set_crontab():
Expand All @@ -18,6 +20,26 @@ def set_crontab():
job.enable()
user_cron.write()

def reset_crontab():
user_cron = CronTab(user=True)
script_path = os.path.join(os.getcwd(), 'main.py')
count = 0
for job in user_cron:
if job.command == f'python3 {script_path}':
user_cron.remove(job)
user_cron.write()
count += 1

print(f'成功清除{count}项定时任务~')

if __name__ == '__main__':
set_crontab()
opts, args = getopt.getopt(sys.argv[1:], "c")
is_reset = False
for cmd, arg in opts:
if cmd == "-c":
is_reset = True

if is_reset:
reset_crontab()
else:
set_crontab()
2 changes: 1 addition & 1 deletion env_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def env_check():
try:
import selenium
except ImportError:
raise ImportError("没有找到selenium包,请用pip安装一下吧~ pip3 install selenium")
raise ImportError("没有找到selenium包,请用pip安装一下吧~ pip3 install --user selenium")

if not os.path.exists('config.ini'):
raise ValueError("请先在config.sample.ini文件中填入个人信息,并将它改名为config.ini")
Expand Down
19 changes: 17 additions & 2 deletions macAutoRun.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#!/bin/bash

chmod +x phantomjs/bin/phantomjs
pip3 install python-crontab
python3 cron.py
pip3 install --user python-crontab
is_reset=0
while getopts c opt
do
case "$1" in
-c)
is_reset=1
esac
shift
done
if [ $is_reset == 1 ]
then
python3 cron.py -c
else
python3 cron.py
fi

0 comments on commit 410cbca

Please sign in to comment.