python remove_all 后未删除 cronjob
python cronjob not deleted after remove_all
TLDR:使用 python、crontab.remove_all 似乎有效,但是当我实例化一个新的 crontab 时,我可以看到 remove_all 命令无效。
长问题:
我正在尝试在我的项目中使用 cronjobs,但在此之前我希望能够测试它们。因为我不想拥有无穷无尽的作业 运行,所以我首先需要能够删除它们。然而,它并没有像我想象的那样工作。
这是我的代码:
def test_understanding_cronjobs(self):
"""
Just trying to understand cronjobs and how to delete them.
The main point of this experiment is to have a way to clean everything
between each test.
"""
# First we instanciate a crontab object.
crontab = CronTab(user="root")
# Then we make sure that there are no other test cronjobs running.
# I know everything should be clean at the start of a test, but this is to show
# that my environment is clean.
crontab.remove_all(comment="test")
# There should be no job at all now.
self.assertEqual(len(crontab), 0)
# We create a single job, with a comment that will allow us to kown that it's a test.
# https://stackabuse.com/scheduling-jobs-with-python-crontab/
job = crontab.new(command="ls -al /tmp", comment="test")
job.minute.every(1)
crontab.write()
# At this point we should have one and only one job.
self.assertEqual(len(crontab), 1)
# Let's delete all test jobs.
crontab.remove_all(comment="test")
# There should be no job at all now.
self.assertEqual(len(crontab), 0)
# Just to be sure, let's create a new crontab instance and verify this information.
clean_crontab = CronTab(user="root")
self.assertEqual(len(clean_crontab), 0)
# But here I get : AssertionError: 1 != 0, self.assertEqual(len(clean_crontab), 0)
如果您对那里发生的事情有任何想法,我真的很想听听。
crontab
只是与系统的 cron 管理交互。如果您不 write
更改,则它们不会提交,因此实例化一个新的 crontab
对象将读取现有内容,而不是“待定”工作。
# Let's delete all test jobs.
crontab.remove_all(comment="test")
# There should be no job at all now.
self.assertEqual(crontab.__len__(), 0)
# <- changes not saved here
# Just to be sure, let's create a new crontab instance and verify this information.
clean_crontab = CronTab(user="root") # <- reads existing stuff from system, only goes up to the previous write
还有
self.assertEqual(crontab.__len__(), 0)
WTF 你不应该直接调用 dunder 方法,除非你覆盖它们。
TLDR:使用 python、crontab.remove_all 似乎有效,但是当我实例化一个新的 crontab 时,我可以看到 remove_all 命令无效。
长问题: 我正在尝试在我的项目中使用 cronjobs,但在此之前我希望能够测试它们。因为我不想拥有无穷无尽的作业 运行,所以我首先需要能够删除它们。然而,它并没有像我想象的那样工作。
这是我的代码:
def test_understanding_cronjobs(self):
"""
Just trying to understand cronjobs and how to delete them.
The main point of this experiment is to have a way to clean everything
between each test.
"""
# First we instanciate a crontab object.
crontab = CronTab(user="root")
# Then we make sure that there are no other test cronjobs running.
# I know everything should be clean at the start of a test, but this is to show
# that my environment is clean.
crontab.remove_all(comment="test")
# There should be no job at all now.
self.assertEqual(len(crontab), 0)
# We create a single job, with a comment that will allow us to kown that it's a test.
# https://stackabuse.com/scheduling-jobs-with-python-crontab/
job = crontab.new(command="ls -al /tmp", comment="test")
job.minute.every(1)
crontab.write()
# At this point we should have one and only one job.
self.assertEqual(len(crontab), 1)
# Let's delete all test jobs.
crontab.remove_all(comment="test")
# There should be no job at all now.
self.assertEqual(len(crontab), 0)
# Just to be sure, let's create a new crontab instance and verify this information.
clean_crontab = CronTab(user="root")
self.assertEqual(len(clean_crontab), 0)
# But here I get : AssertionError: 1 != 0, self.assertEqual(len(clean_crontab), 0)
如果您对那里发生的事情有任何想法,我真的很想听听。
crontab
只是与系统的 cron 管理交互。如果您不 write
更改,则它们不会提交,因此实例化一个新的 crontab
对象将读取现有内容,而不是“待定”工作。
# Let's delete all test jobs.
crontab.remove_all(comment="test")
# There should be no job at all now.
self.assertEqual(crontab.__len__(), 0)
# <- changes not saved here
# Just to be sure, let's create a new crontab instance and verify this information.
clean_crontab = CronTab(user="root") # <- reads existing stuff from system, only goes up to the previous write
还有
self.assertEqual(crontab.__len__(), 0)
WTF 你不应该直接调用 dunder 方法,除非你覆盖它们。