From Fedora Project Wiki
(Justification for crontabs requirement/fix crontabs package name)
 
(16 intermediate revisions by one other user not shown)
Line 11: Line 11:
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.
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.
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.
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.


== Logrotate file ==
== Cron Job file ==


If a package contains a logfile then it needs to also include a properly installed logrotate file.
An typical cron job file is just an script like
 
Each logrotate file must end with the filename .conf. It must be placed /etc/logrotate.d/ and must have 0644 filepermission and be owned by root.
 
Here are examples of an logrotate file.
 
=== Example minimal logrotate file ===


<pre>
<pre>
/var/log/example/*log {
#!/bin/sh
missingok # If the log file is missing, go on to the next one without issuing an error message
# My cron job script
notifempty # Don't do any rotation if the logfile is empty
# set -x
compress # Compress older files with gzip
delaycompress # Don't compress yesterdays files
}
</pre>


=== Example minimal logrotate log file with user create mode ===
echo "This is my simple cron job script"


<pre>
exit 0
/var/log/example/*log {
missingok # If the log file is missing, go on to the next one without issuing an error message
notifempty # Don't do any rotation if the logfile is empty
compress # Compress older files with gzip
  delaycompress # Don't compress yesterdays files
create 640 owner group  # Set create mode immediately after rotation
}
</pre>
</pre>


=== Example minimal logrotate file with daemon restart ===
Example of cron job definition run at ever other hour specified in /etc/crontab


<pre>
<pre>
/var/log/example/*log {
# .---------------- minute (0 - 59)
missingok # If the log file is missing, go on to the next one without issuing an error message
# |  .------------- hour (0 - 23)
notifempty # Don't do any rotation if the logfile is empty
# |  |  .---------- day of month (1 - 31)
compress # Compress older files with gzip
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
  delaycompress # Don't compress yesterdays files
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
sharedscripts # Scripts are only run once for all files in directory
# | |  |  |  |
postrotate
# *  *  *  *  * user-name  command to be executed
/usr/bin/systemctl restart example.service 2>/dev/null || true
endscript
}
</pre>


=== Example minimal logrotate file with user create mode and daemon restart ===
0 */2 * * * root /etc/cron.d/example


<pre>
/var/log/example/*log {
missingok # If the log file is missing, go on to the next one without issuing an error message
notifempty # Don't do any rotation if the logfile is empty
compress # Compress older files with gzip
  delaycompress # Don't compress yesterdays files
create 640 owner group  # Set create mode immediately after rotation
sharedscripts # Scripts are only run once for all files in directory
postrotate
/usr/bin/systemctl restart example.service 2>/dev/null || true
endscript
}
</pre>
</pre>


{{admon/tip|Debugging logrotate file|You can debug your logrotate file by running
== Cron job file names ==
<pre># logrotate -d -f /etc/logrotate.d/example.conf</pre>from the command line.}}
 
 
== Rsyslog file ==


If package wants to use rsyslog as an additional optional syslog solution it also needs to contain an rsyslog conf file.
The file name of a cron job file should match the name of the package from which it comes.


Each rsyslog file must end with the filename .conf. It must be placed /etc/rsyslog.d/ directory and must have 0644 filepermission and be owned by root.
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.


Here is an examples of rsyslog file.
{{admon/tip|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.}}
=== Example minimal rsyslog file ===
<pre>
# Log example generated log messages to file
:syslogtag, isequal, "[EXAMPLE]" /var/log/example/example.log


& ~
== Cron Job Files Packaging ==
</pre>


{{admon/tip|Debugging rsyslog file|You can debug your ryslog file by running
If an package is going to provide cron job file(s) then it must be placed into a separate $name-cron subpackage.  
<pre># rsyslogd -f /etc/rsyslog.d/example.conf -N1</pre>from the command line.}}


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


If an package is going to provide additional optional syslog support then they must be placed into a separate $name-<syslogging option> subpackage as in $name-rsyslog or $name-syslog-ng
A package containing a cron job must depend on the <code>crontabs</code> package.  Since <code>crontabs</code> requires <code>/etc/cron.d</code> and all cron daemon packages create (and own) that directory, <code>crontabs</code> serves as a virtual provide for cron daemon functionality.


=== Example of rsyslog subpackage ===
=== Example of cron job subpackage ===


<pre>
<pre>
Name:
Name:
.....
.....
Source1: %{name}.logrotate
Source1: %{name}.cron


%package example-rsyslog
%package example-cron
Summary:  Rsyslog support for %{name}
Summary:  Cron job file for %{name}
Group:    System Environment/Base
Group:    System Environment/Base
Requires: %{name} = %{version}-%{release}
Requires: %{name} = %{version}-%{release}
Requires: logrotate
Requires: crontabs
Requires: rsyslog


%description example-rsyslog
%description example-cron
Rsyslog support for %{name}
Cron job file for %{name}


%install  
%install  
mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/log/%{name}
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/cron.daily
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d
%{__install} -p -D -m 0750 %{SOURCE1} $RPM_BUILD_ROOT%{_sysconfdir}/cron.daily/%{name}
%{__install} -p -D -m 0700 %{SOURCE1} $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d/%{name}.conf
 
%files example-rsyslog
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}.conf
%dir %attr(0700,root,root) %{_localstatedir}/log/%{name}
</pre>
 
=== Example of rsyslog subpackage with additional rsyslog file ===
 
<pre>
Name:
.....
Source1: %{name}.logrotate
Source2: %{name}.rsyslog
 
%package example-rsyslog
Summary:  Rsyslog support for %{name}
Group:    System Environment/Base
Requires: %{name} = %{version}-%{release}
Requires: logrotate
Requires: rsyslog
 
%description example-rsyslog
Rsyslog support for %{name}
 
%install
mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/log/%{name}
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/rsyslog.d
%{__install} -p -D -m 0700 %{SOURCE1} $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d/%{name}.conf
%{__install} -p -D -m 0700 %{SOURCE2} $RPM_BUILD_ROOT%{_sysconfdir}/rsyslog.d/%{name}.conf
 
%files example-rsyslog
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}.conf
%config(noreplace) %{_sysconfdir}/rsyslog.d/%{name}.conf
%dir %attr(0700,root,root) %{_localstatedir}/log/%{name}
</pre>
 
=== Example of syslog-ng subpackage ===
 
<pre>
Name:
.....
Source1: %{name}.logrotate
 
%package example-syslog-ng
Summary:  Syslog-ng support for %{name}
Group:    System Environment/Base
Requires: %{name} = %{version}-%{release}
Requires: logrotate
Requires: syslog-ng
 
%description example-syslog-ng
Syslog-ng support for %{name}
 
%install
mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/log/%{name}
mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d
%{__install} -p -D -m 0700 %{SOURCE1} $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d/%{name}.conf


%files example-syslog-ng
%files example-cron
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}.conf
%config(noreplace) %{_sysconfdir}/cron.daily/%{name}
%dir %attr(0700,root,root) %{_localstatedir}/log/%{name}
</pre>
</pre>

Latest revision as of 16:44, 27 March 2013

Fedora Cron Job Files

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

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

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.

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

If an package is going to provide cron job file(s) then it must be placed into a separate $name-cron subpackage.

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

A package containing a cron job must depend on the crontabs package. Since crontabs requires /etc/cron.d and all cron daemon packages create (and own) that directory, crontabs serves as a virtual provide for cron daemon functionality.

Example of cron job subpackage

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

%package example-cron
Summary:  Cron job file for %{name}
Group:    System Environment/Base
Requires: %{name} = %{version}-%{release}
Requires: crontabs

%description example-cron
Cron job file for %{name}

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

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