Deprecated: Creation of dynamic property Timber\Site::$pingback is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Site.php on line 180

Deprecated: Creation of dynamic property Timber\User::$name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/User.php on line 118

Deprecated: Creation of dynamic property Timber\Post::$has_disclaimer is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_lock is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_last is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_meta_box_present is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_send_notification is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_modify_title_and_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_heading is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_category is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_blz_affiliation_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_content_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_speciali is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskw is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_metadesc is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_linkdex is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_content_score is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskeywords is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_keywordsynonyms is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_estimated-reading-time-minutes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_thumbnail_id is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$response_body is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_indexnow_last_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_a45894eb190bcfd724fea62f13a14935 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_oembed_time_a45894eb190bcfd724fea62f13a14935 is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$newsletter_subject is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_newsletter_subject is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$newsletter_summary is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_newsletter_summary is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_date_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$ping_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_password is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$to_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$pinged is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_content_filtered is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$guid is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$menu_order is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_mime_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_count is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$filter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71
Crontab Linux: guida completa alla pianificazione automatica di comandi e script | IlSoftware.it

Crontab Linux: guida completa alla pianificazione automatica di comandi e script

Nel mondo Linux, la pianificazione delle attività ricorrenti è affidata a cron e al file crontab. Spieghiamo come funzionano questi strumenti, come creare e modificare le pianificazioni.

Nel mondo Linux e Unix-like, l’automazione dei processi è una pratica fondamentale per amministratori di sistema, sviluppatori e team DevOps. Tra gli strumenti più affidabili e diffusi per la pianificazione delle attività troviamo cron, e in particolare il suo file di configurazione per utente, crontab. Ogni utente del sistema può avere il proprio crontab personale e l’amministratore può configurare un crontab di sistema per gestire operazioni globali.

Pianificazione delle attività su Linux: cosa sono cron e crontab

cron è un demone (daemon) di sistema che esegue automaticamente comandi o script a intervalli regolari, definiti nel tempo. crontab (abbreviazione di “cron table”) è il file che contiene le istruzioni per cron: una lista di comandi con la relativa pianificazione. Ogni riga di un file crontab ha la seguente struttura:

MIN HOUR DOM MON DOW COMMAND

Campo Significato Valori ammessi
MIN Minuto 0–59
HOUR Ora 0–23
DOM Giorno del mese 1–31
MON Mese 1–12 oppure nomi (jan–dec)
DOW Giorno della settimana 0–7 (0 e 7 = domenica)
COMMAND Comando da eseguire Qualsiasi comando di shell

Come usare crontab

Per verificare l’esistenza di una tabella cron, quindi di qualche pianificazione per l’utente in uso, basta semplicemente digitare quanto segue:

crontab -l

Il comando seguente, invece, consente di andare a modificare il file di configurazione per l’utente corrente. In questo modo si può creare o modificare la crontab aggiungendo o rimuovendo le attività da svolgere:

crontab -e

Di default, il sistema chiede quale editor di testo utilizzare per intervenire sul file di configurazione: il consiglio è quello di optare per nano che è uno degli strumenti più semplici da usare in assoluto. All’interno dell’editor nano, la combinazione di tasti CTRL+O permette di salvare il file mentre CTRL+X consente di uscire dal programma.

Per modificare il crontab di sistema da amministratore (root), consigliamo di usare il seguente comando esplicito:

crontab -e -u root

In alternativa, si può aprire direttamente il file /etc/crontab:

sudo nano /etc/crontab

In questo file la sintassi è leggermente diversa: ogni riga prevede anche il riferimento allo specifico utente che esegue il comando (notare la presenza di USER):

MIN HOUR DOM MON DOW USER COMMAND

Attenzione a modificare direttamente il file di configurazione perché anche semplici errori di sintassi possono compromettere l’esecuzione delle operazioni pianificate.

Esempi di base

# Ogni giorno alle 2:00 esegue lo script di backup
0 2 * * * /usr/local/bin/backup.sh

# Ogni 10 minuti esegue uno script per il controllo connessione
*/10 * * * * /home/user/check-conn.sh

# Esegue uno script Python dal lunedì al venerdì alle 8:00
0 8 * * 1-5 /usr/bin/python3 /home/user/script.py

Inserendo quanto segue nel file /etc/crontab, il sistema esegue lo script di backup indicato ogni giorno alle 2:30 di notte utilizzando l’account root:

30 2 * * * root /usr/local/bin/backup.sh

Caratteri speciali nei campi temporali

Simbolo Significato
* Qualsiasi valore valido nel campo
, Lista di valori (es. 1,2,5)
- Intervallo (es. 1-5)
/ Passo (es. */15 = ogni 15 unità di tempo nel campo)

Scorciatoie per pianificazioni comuni con crontab

Sintassi Descrizione
@reboot All’avvio del sistema
@yearly Una volta all’anno (equivale a 0 0 1 1 *)
@monthly Una volta al mese
@weekly Una volta alla settimana
@daily Una volta al giorno
@hourly Una volta all’ora

Ad esempio, la sintassi @reboot /home/user/startup.sh permette di avviare lo script indicato a ogni avvio del sistema in uso.

Note finali

Per usare cron e crontab con soddisfazione, è indispensabile assicurarsi che l’utente abbia i permessi richiesti per avviare script e comandi. In particolare, è necessario che gli script richiamati abbiano i permessi di esecuzione (chmod +x script.sh). Inoltre, è indispensabile usare percorsi assoluti per riferirsi a comandi e file.

In generale, è fondamentale effettuare dei test manuali per verificare che tutto funzioni prima di passare alla configurazione della crontab.

Ancora, va osservato che in molte distribuzioni Linux, cron logga le esecuzioni in /var/log/syslog o /var/log/cron. È quindi possibile sfruttare il comando grep CRON /var/log/syslog per raccogliere informazioni sulle attività via via poste in esecuzione.

Ti consigliamo anche

Link copiato negli appunti