Cómo listar trabajos cron en Linux

Para que Linux realice automáticamente tareas que de otro modo tendría que hacer usted mismo, como ejecutar scripts y ejecutar comandos específicos, utilizamos una herramienta llamada crónicamente. En los sistemas Linux, crónicamente La utilidad es la forma preferida de automatizar la ejecución de scripts en ciertos intervalos de tiempo.
En este artículo, cubriremos cómo ver/enumerar trabajos programados en la lista crontab.
Con la mayoría de Crons (p. ej. Vixie-Cron – por defecto Debian/Ubuntu, crónicamente – Fedora por defecto, Cron Solaris …) obtener la lista de trabajos cron programados para el usuario actual mediante:
$ crontab -l
o para otro usuario a través de
# crontab -u username -l
Alternativamente, puede buscar archivos de spool. Por lo general, se guardan en /var/spool/cronpor ejemplo para vcron, el siguiente directorio /var/spool/cron/crontabs contiene todos los crontabs configurados de todos los usuarios, excepto el usuario raíz, que también puede configurar trabajos a través del crontab de todo el sistema, que se encuentra en:
/etc/crontab
Para verlo, ejecute el siguiente comando menos:
less /etc/crontab
Ejemplo de estructura 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 camarada (predeterminado en Fedora/CentOS), también hay un directorio de configuración de estilo cron.d para trabajos cron del sistema:
/etc/cron.d
Como siempre, el directorio cron.d facilita 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 los scripts vinculados/almacenados se ejecutan periódicamente, por ejemplo:
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
Cómo enumerar trabajos de 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 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 listar trabajos semanales de Cron
Para listar trabajos cron semanales. Ejecutar bajo 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 mensuales de Cron
Esto mostrará una lista de los 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 gato dominio:
$ 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, puedes tener trabajos «en». (/var/carrete/en/*)anacrónico (/etc/anacrontab y /var/spool/anacron/*)
La ejecución correcta y oportuna de esos scripts es manejada por entradas de partes de ejecución en el crontab del sistema o por anachron.
Con sistemad (en Fedora, CentOS 7, etc.) la ejecución periódica de trabajos se puede configurar aún más a través de unidades de temporizador. Los temporizadores del sistema habilitados se pueden mostrar mediante:
$ systemctl list-timers
He 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.