Cómo enumerar trabajos de Cron en Linux

Para que Linux realice automáticamente tareas que de otro modo tendría que realizar usted mismo, como ejecutar scripts y ejecutar comandos específicos, usamos una herramienta llamada cron. En los sistemas Linux, la utilidad cron es la forma preferida de automatizar la ejecución de scripts en intervalos de tiempo específicos.

En este artículo, cubriremos cómo ver / listar los trabajos programados en la lista crontab.

Con la mayoría de Crons (por ejemplo, Vixie-Cron – Debian / Ubuntu predeterminado, Cronie – Fedora predeterminado, Solaris Cron …) obtiene la lista de trabajos cron programados para el usuario actual a través de:

$ crontab -l

o para otro usuario a través de

# crontab -u username -l

Alternativamente, puede buscar los archivos de cola. Por lo general, se guardan en / var / spool / cron, por ejemplo, para vcron, el siguiente directorio / var / spool / cron / crontabs contiene todas las crontabs configuradas de todos los usuarios, excepto el usuario root que también puede configurar trabajos a través de el crontab de todo el sistema, que se encuentra en:

/etc/crontab

Para verlo, ejecute el siguiente comando less:

less /etc/crontab

Estructura de ejemplo del 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

En mi computadora se ve así:

# /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 )

Con cronie (predeterminado en Fedora/ CentOS), también hay un directorio de configuración de estilo cron.d para los trabajos cron del sistema:

/etc/cron.d

Como siempre, el directorio cron.d simplifica el mantenimiento de las entradas de configuración que forman parte de diferentes paquetes.

Para mayor comodidad, la mayoría de las distribuciones también proporcionan directorios donde se ejecutan periódicamente scripts enlazados / almacenados, por ejemplo:

/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

Cómo enumerar trabajos cron por hora

Para enumerar los trabajos cron por hora, ejecute el siguiente comando

$ 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

Cómo enumerar los trabajos diarios de Cron

Podemos listar los trabajos cron diarios con el siguiente 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

Cómo enumerar los trabajos cronológicos semanales

Para enumerar trabajos cron semanales. Ejecutar debajo del 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

Cómo enumerar trabajos cron mensuales

Esto mostrará una lista de trabajos cron mensuales

$ 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

Ver Cronjobs específicos del software

Podemos echar un vistazo a un trabajo cron específico con el comando cat:

$ 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

Además de eso, puede tener trabajos “at” (/ var / spool / at / *), anacron (/ etc / anacrontab y / var / spool / anacron / *)

La ejecución correcta y oportuna de esos scripts se gestiona mediante entradas de ejecución de piezas en el crontab del sistema o mediante anacron.

Con systemd (en Fedora, CentOS 7, etc.) la ejecución periódica de trabajos se puede configurar adicionalmente a través de unidades de temporizador. Los temporizadores del sistema habilitados se pueden mostrar a través de:

$ systemctl list-timers

Hemos visto todos los directorios predeterminados donde puede encontrar trabajos cron en la mayoría de los sistemas Linux. Si tiene alguna pregunta o comentario, publíquelo a continuación en la sección de comentarios.

Leer también:

  • Cómo verificar los registros cronológicos en Ubuntu 18.04
  • Cómo programar un trabajo en Linux usando Cron, Anacron y en los comandos
  • Cómo configurar trabajos de Cron usando el archivo Crontab de Linux