4

We know the crontab command is used for scheduled tasks in Linux.

I want to write a Python script. Its function is to receive some data (these data are related to crontab setting) and execute a 'crontab' command in order to reset the content of the user's crontab file.

I know how to execute external Linux commands in Python. But when you execute the crontab command (e.g. crontab -u xxx -e), you need to interact with an editor to modify the user's crontab file. (Suppose I don't know where the file is. For new users, crontab will generate a new file anyway. And I don't execute the command as the root user).

So the question is, how can I just execute crontab in Python? Is there any way to avoid interacting with an editor to modify the user's crontab file in Python?

My OS is ubuntu 14.01.

3

5 Answers 5

11

You could use python-crontab.

Installation

sudo -H pip install python-crontab

Concepts

  • A cron is a time-based job scheduler which is related to a user.
  • A job has a command and a comment and is "attached" to a cron.
  • A job is executed by the cron it is attached to at given times.

Code examples

List system cron jobs:

from crontab import CronTab
cron = CronTab(tabfile='/etc/crontab', user=False)  # system users cron
# cron  = CronTab(user=True)  # current users cron
# cron  = CronTab(user='username')  # other users cron
for job in cron:
    print(job)

Create a new job:

job = cron.new(command='/foo/bar', comment='SomeID')

Enable / disable job:

job.enable()
job.enable(False)

Find an existing job by comment:

iter = cron.find_comment('ID or some text')

Remove Items::

cron.remove( job )
cron.remove_all('echo')
cron.remove_all(comment='foo')
cron.remove_all(time='*/2')

Clear entire cron of all jobs::

cron.remove_all()
Sign up to request clarification or add additional context in comments.

4 Comments

How to list all existing crons (without using regex or names)?
How to modify a old job? job.command='new-cmd' ?
@0xc0de I'm not sure if this functionality was added after your question, but per the documentation, you can do something like this to get all existing jobs: from crontabs import CronTabs; for cron in CronTabs(): print(repr(cron)). Another alternative is: jobs = CronTabs.all(). All jobs are added to a CronTab object which can further be used as documented per the project.
Unfortunately, the only lines other than the command line that python-crontab allows are comment lines. You cannot add line for environment variables which are often necessary for a cron job to run successfully.
4

As you want it in Python, you can do "something" like this:

import os;
...
cur_cron = os.popen('crontab -l > current_crontab.txt');
cur_cron.read();
cur_cron.close();
fopen_cron = file('current_crontab.txt', 'a');
fopen_cron.write("\n### Comment here if you like");
fopen_cron.write("\n* * * * * Put your command here");
fopen_cron.close();

Hopefully it helps.

Comments

3

With Vixie crontab, you could do something like this (obviously you could check for errors and so on):

import subprocess

cron_in = subprocess.Popen(['crontab', '-l'],
    stdout=subprocess.PIPE)
cur_crontab, _ = cron_in.communicate()
# new_crontab = do_my_magic(cur_crontab)
cron_out = subprocess.Popen(['crontab', '-'],
    stdin=subprocess.PIPE)
cron_out.communicate(input=new_crontab)

Comments

0

You could/should first dump your current crontab with crontab -l, edit it the way you want (e. g. add some lines, or modify) and then install the new one.

This usually works with crontab <filename>, but should as well work with crontab - and then piping the new contents into the process's stdin.

Comments

0

If all you want to do is reset the content of user crontab file , then just remove the crontab file (or overwrite with your default) , and reload the cron service .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.