Come elencare i lavori Cron in Linux

Affinché Linux esegua automaticamente attività che altrimenti dovresti eseguire tu stesso, come l’esecuzione di script e l’esecuzione di comandi specifici, utilizziamo uno strumento chiamato cronicamente. Sui sistemi Linux, cronicamente utility è il modo preferito per automatizzare l’esecuzione di script a determinati intervalli di tempo.
In questo articolo, tratteremo come visualizzare/elencare i lavori pianificati nell’elenco crontab.
Con la maggior parte dei Cron (es Vixie-Cron – Debian/Ubuntu predefinito, cronicamente – Fedora predefinito, Solaris Cron …) ottenere l’elenco dei lavori cron pianificati per l’utente corrente tramite:
$ crontab -l
o per un altro utente tramite
# crontab -u username -l
In alternativa, puoi cercare i file di spool. Di solito sono salvati sotto /var/spool/cronad esempio per vcron, la seguente directory /var/spool/cron/crontabs contiene tutti i crontab configurati di tutti gli utenti, ad eccezione dell’utente root, che può anche configurare i lavori tramite il crontab a livello di sistema, che si trova in:
/etc/crontab
Per visualizzarlo, esegui il seguente comando less:
less /etc/crontab
Esempio di struttura crontab:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- 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
Sul mio computer appare così:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Insieme a amico (predefinito su Fedora/CentOS), esiste anche una directory di configurazione in stile cron.d per i lavori cron di sistema:
/etc/cron.d
Come sempre, la directory cron.d semplifica la gestione delle voci di configurazione che fanno parte di pacchetti diversi.
Per comodità, la maggior parte delle distribuzioni fornisce anche directory in cui gli script collegati/memorizzati vengono eseguiti periodicamente, ad esempio:
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
Come elencare i lavori Cron ogni ora
Per elencare i lavori cron orari, eseguire il comando seguente
$ ls -la /etc/cron.hourly/
total 12
drwxr-xr-x 2 root root 4096 Apr 24 20:44 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
Come elencare i lavori Cron giornalieri
Possiamo elencare i cron job giornalieri con il seguente comando:
$ ls -la /etc/cron.daily/
total 72
drwxr-xr-x 2 root root 4096 Apr 24 20:46 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
-rwxr-xr-x 1 root root 376 Apr 4 2014 apport
-rwxr-xr-x 1 root root 15481 Apr 10 2014 apt
-rwxr-xr-x 1 root root 314 Feb 18 2014 aptitude
-rwxr-xr-x 1 root root 355 Jun 4 2013 bsdmainutils
-rwxr-xr-x 1 root root 256 Mar 7 2014 dpkg
-rwxr-xr-x 1 root root 372 Jan 22 2014 logrotate
-rwxr-xr-x 1 root root 1261 Sep 23 2014 man-db
-rwxr-xr-x 1 root root 435 Jun 20 2013 mlocate
-rwxr-xr-x 1 root root 249 Feb 17 2014 passwd
-rwxr-xr-x 1 root root 2417 May 13 2013 popularity-contest
-rwxr-xr-x 1 root root 214 Mar 27 2017 update-notifier-common
-rwxr-xr-x 1 root root 328 Jul 18 2014 upstart
Come elencare i Cron Job settimanali
Per elencare i cron job settimanali. Corri sotto comando
$ ls -la /etc/cron.weekly/
total 28
drwxr-xr-x 2 root root 4096 Apr 24 20:46 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
-rwxr-xr-x 1 root root 730 Feb 23 2014 apt-xapian-index
-rwxr-xr-x 1 root root 427 Apr 16 2014 fstrim
-rwxr-xr-x 1 root root 771 Sep 23 2014 man-db
-rwxr-xr-x 1 root root 211 Mar 27 2017 update-notifier-common
Come elencare lavori Cron mensili
Questo elencherà i cron job mensili
$ ls -la /etc/cron.monthly/
total 12
drwxr-xr-x 2 root root 4096 Apr 24 20:44 .
drwxr-xr-x 96 root root 4096 May 19 17:12 ..
-rw-r--r-- 1 root root 102 Feb 9 2013 .placeholder
Vedi Cronjobs specifici del software
Possiamo dare un’occhiata a uno specifico cron job con il gatto comando:
$ cd /etc/cron.daily/
$ ls -l
total 60
-rwxr-xr-x 1 root root 376 Apr 4 2014 apport
-rwxr-xr-x 1 root root 15481 Apr 10 2014 apt
-rwxr-xr-x 1 root root 314 Feb 18 2014 aptitude
-rwxr-xr-x 1 root root 355 Jun 4 2013 bsdmainutils
-rwxr-xr-x 1 root root 256 Mar 7 2014 dpkg
-rwxr-xr-x 1 root root 372 Jan 22 2014 logrotate
-rwxr-xr-x 1 root root 1261 Sep 23 2014 man-db
-rwxr-xr-x 1 root root 435 Jun 20 2013 mlocate
-rwxr-xr-x 1 root root 249 Feb 17 2014 passwd
-rwxr-xr-x 1 root root 2417 May 13 2013 popularity-contest
-rwxr-xr-x 1 root root 214 Mar 27 2017 update-notifier-common
-rwxr-xr-x 1 root root 328 Jul 18 2014 upstart
$ cat update-notifier-common
#!/bin/sh
set -e
[ -x /usr/lib/update-notifier/package-data-downloader ] || exit 0
# Try to rerun any package data downloads that failed at package install time.
/usr/lib/update-notifier/package-data-downloader
Inoltre, puoi avere lavori “a”. (/var/spool/at/*)anacronistico (/etc/anacrontab e /var/spool/anacron/*)
L’esecuzione corretta e tempestiva di tali script è gestita dalle voci run-parts nel crontab di sistema o da anachron.
Insieme a systemd (su Fedora, CentOS 7, ecc.) l’esecuzione periodica del lavoro può essere ulteriormente configurata tramite unità timer. I timer di sistema abilitati possono essere visualizzati da:
$ systemctl list-timers
Ho visto tutte le directory predefinite in cui puoi trovare lavori cron sulla maggior parte dei sistemi Linux. In caso di domande o commenti, si prega di pubblicare di seguito nella sezione dei commenti.