From Fedora Project Wiki

(justification for Requires: crontabs)
No edit summary
Line 1: Line 1:
== Fedora Cron Job Files ==
== Fedora Cron Job Files ==


This document describes the guidelines for packaging cron job file(s), in Fedora.
This document describes the guidelines for packaging cron job file in Fedora.


For the purposes of these guidelines, a cron job file is defined as an script (e.g., shell scripts or Perl scripts).
For the purposes of these guidelines, a cron job file is defined as a script (e.g., a shell script or a Perl script).


If your package requires regularly scheduled tasks to operate properly and does not ship systemd unit file, you can use those files to set that up.
If your package requires regularly scheduled tasks to operate properly and does not ship a systemd unit file, you can use a cron job to run those tasks.


== Cron Job Files on the filesystem ==
== Cron Job Files on the filesystem ==

Revision as of 16:48, 27 March 2013

Fedora Cron Job Files

This document describes the guidelines for packaging cron job file in Fedora.

For the purposes of these guidelines, a cron job file is defined as a script (e.g., a shell script or a Perl script).

If your package requires regularly scheduled tasks to operate properly and does not ship a systemd unit file, you can use a cron job to run those tasks.

Cron Job Files on the filesystem

Packages with cron job files must be placed respectfully into one or more of the following directories /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly depending on the intended interval they should run.

If a certain cron job has to be executed at some frequency or at a specific time interval other then the above, that cron job file should be placed in /etc/cron.d directory followed by an crontab entry at the required interval in the /etc/crontab file.

All files installed in any of these directories must be scripts and must be treated as configuration files so that they can easily be modified by the local system administrator.

Cron Job file

An typical cron job file is just an script like

#!/bin/sh
# My cron job script
# set -x

echo "This is my simple cron job script"

exit 0

Example of cron job definition run at ever other hour specified in /etc/crontab

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

0 */2 * * * root /etc/cron.d/example

Cron job file names

The file name of a cron job file should match the name of the package from which it comes.

If a package supplies multiple cron job files files in the same directory, the file names should all start with the name of the package by a hyphen (-) and a suitable suffix.

A cron job file name cannot include any period or plus characters as this will cause cron to ignore the file. Underscores should be used instead.

Cron Job Files Packaging

Cron job file(s) in packages must be marked as %config(noreplace), and their filename(s) should match the name of the package.

Packages with cron job files must have an explicit Requires: crontabs. Since crontabs requires /etc/cron.d and all cron daemon packages create (and own) that directory, crontabs serves as a virtual requires for cron daemon functionality.

Example of cron job packaging

Name:
.....
Source1: %{name}.cron
Requires: crontabs

.....

%install 
.....
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/cron.daily
%{__install} -p -D -m 0750 %{SOURCE1} $RPM_BUILD_ROOT%{_sysconfdir}/cron.daily/%{name}

%files
%config(noreplace) %{_sysconfdir}/cron.daily/%{name}