Difference between revisions of "Crontab"

From wikieduonline
Jump to navigation Jump to search
Tags: Mobile web edit, Mobile edit
 
(19 intermediate revisions by the same user not shown)
Line 18: Line 18:
 
== Logging ==
 
== Logging ==
 
Cron execution logs can be saved to specific file using [[rsyslog]] service.
 
Cron execution logs can be saved to specific file using [[rsyslog]] service.
* In [[Ubuntu]] uncomment in <code>/etc/rsyslog.d/50-default.conf</code> the line:
+
* In [[Ubuntu]] uncomment in <code>[[/etc/rsyslog.d/50-default.conf]]</code> the line:
:<code>#cron.* /var/log/cron.log</code>
+
:<code>#cron.* [[/var/log/cron.log]]</code>
 
: and restart rsyslog service: <code>sudo systemctl restart rsyslog</code>
 
: and restart rsyslog service: <code>sudo systemctl restart rsyslog</code>
 
* In [[Debian]] uncomment in <code>/etc/rsyslog.conf </code> the line:
 
* In [[Debian]] uncomment in <code>/etc/rsyslog.conf </code> the line:
Line 38: Line 38:
  
 
Execute every minute:
 
Execute every minute:
* * * * *  /scripts/script.sh
+
:<code>* * * * *  /path/to/your/script.sh</code>
  
 
Execute every 5 minutes:
 
Execute every 5 minutes:
*/5 * * * *
+
:<code>*/5 * * * * /path/to/your/script.sh</code>
 +
 
 +
Execute every 15 minutes:
 +
:<code>[[*/15]] * * * * /path/to/your/script.sh</code>
  
 
  45 18 * * * /scripts/script.sh
 
  45 18 * * * /scripts/script.sh
  
  @reboot /scripts/script.sh
+
  [[@reboot]] /scripts/script.sh
  
 
== Related terms ==
 
== Related terms ==
Line 51: Line 54:
 
* [[kron (Cisco IOS)]]
 
* [[kron (Cisco IOS)]]
 
* [[TeamCity triggers]]
 
* [[TeamCity triggers]]
 +
* [[GitLab scheduled pipelines]]
 
* [[Travis]] cron jobs: https://docs.travis-ci.com/user/cron-jobs/
 
* [[Travis]] cron jobs: https://docs.travis-ci.com/user/cron-jobs/
* [[Cronitor]]
+
* [[Cronitor]] service
 
* <code>[[cron.schedule]]</code> [[PostgreSQL]]
 
* <code>[[cron.schedule]]</code> [[PostgreSQL]]
 +
* <code>[[CRON_TZ]]</code>
 +
* <code>[[ScheduleExpression]]</code> in <code>[[AWS::Events::Rule]]</code>
 +
* <code>[[pg_cron]]</code>
 +
* <code>[[.timer]]</code>, <code>[[/lib/systemd/system/]]</code>
 +
* <code>[[gcloud scheduler]]</code>
 +
* [[journalctl -r]] | grep CRON
  
 
== See also ==
 
== See also ==

Latest revision as of 14:41, 19 November 2024

Overview[edit]

Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a system-wide crontab file (usually in /etc or a subdirectory of /etc) that only system administrators can edit.

Each line of a crontab file represents a job, and looks like this:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * command to execute

The syntax of each line expects a cron expression made of five fields, followed by a shell command to execute.

Logging[edit]

Cron execution logs can be saved to specific file using rsyslog service.

#cron.* /var/log/cron.log
and restart rsyslog service: sudo systemctl restart rsyslog
  • In Debian uncomment in /etc/rsyslog.conf the line:
#cron.* /var/log/cron.log

Configuration[edit]


/etc/cron.deny (echo ALL >>/etc/cron.deny)
/etc/cron.allow


/var/spool/cron/crontabs/

Examples[edit]

Execute every minute:

* * * * * /path/to/your/script.sh

Execute every 5 minutes:

*/5 * * * * /path/to/your/script.sh

Execute every 15 minutes:

*/15 * * * * /path/to/your/script.sh
45 18 * * * /scripts/script.sh
@reboot /scripts/script.sh

Related terms[edit]

See also[edit]


Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Source: wikiversity

Advertising: