cron - CSTBox crontab management¶
Lightweight tools for working with the CSTBox private crontab.
In order to avoid cluttering the host system, CSTBox cron settings are gathered in a package related crontab stored in /etc/cron.d, as described in cron man page. This way, we can deactivate all CSTBox cron tasks when shutting it down, without loosing their definitions, and then be able to reactivate them back again when restarting the framework.
-
class
pycstbox.cron.
CronItem
(line=None, system_crontab=True)¶ An item of the crontab, be it a job specification or anything else.
Constructor.
Parameters: - line (str) – (optional) a line to be parsed. If not provided, all fields are initialized to an empty value. It item belongs to a system crontab, the user field is defaulted to the current user id
- system_crontab (bool) – if True (default), cron items use the system crontab format, including the user field. If false, no user field is included in the cron items.
-
parse
(line)¶ Parses a string supposed to be a crontab line.
A basic analyze of the line is performed to decide if it can be a job specification or something else. It is quite rudimentary and not bullet proof in any way, since a comment line containing a number of words equal to the job definition field count is taken for a job.
Maybe some more in-depth analyze could be done by checking if what is supposed to be the time specs fields are valid ones, but we don’t need so much sophistication for the moment.
Parameters: line (str) – the line to be parsed
-
render
()¶ Returns the crontab record corresponding to the item definition.
The result is always a string, which can be empty if no attribute of the item is set.
-
class
pycstbox.cron.
CronTab
(path='/etc/cstbox/crontab', system_crontab=True)¶ The crontab.
Despite its declaration the class does not inherit from CronTabHooks with the usual semantics. This must be seen here as a mixin mechanism, allowing to easily override the hooks by providing an ad-hoc version of
CronTabHooks
. This is the reason why object has been left in the super-classes list, although not required from Python language strict point of view.Constructor.
Parameters: - path (str) – an optional path from which the crontab is loaded, if it exists. default: CSTBox crontab path
- system_crontab (bool) – if True (default), cron items use the system crontab format, including the user field. If false, no user field is included in the cron items.
-
read
(path)¶ Loads and parses the content of a crontab file.
Parameters: path (str) – the path of the file Raises: any IO error dealing with file access, including file not found
-
write
(path=None)¶ Writes the crontab to disk.
Parameters: path (str) – the path of the file to be written. If not provided, the path defined at instantiation time is used. If none of these are defined, an exception is generated. If not provided at creation time, the path passed here will be stored in the instance.
-
add
(item)¶ Adds an item to the crontab.
Passing None is allowed and will add an empty CronItem.
Parameters: item (CronItem) – the item to be added Raises: ValueError if the passed item is not None and is not of same type (system or user) as the crontab itself
-
remove
(item)¶ Removes a given item from the crontab.
-
iterjobs
()¶ Returns an iterator on the cron items representing jobs.
-
find_comment
(comment)¶ Return a list of cron items having a specific comment.
-
find_command
(command)¶ Return a list of cron items using a command.
-
render
()¶ Renders the whole crontab as a list of strings.