Chronos, Cron.

If you are working with data, chances are that you need to do a certain set of tasks periodically; whether its your daily report, daily cleaning of the incoming database, you don’t have to waste time running them everyday. The procedure I’m about to describe is very rudimentary, there are more complex tools to achieve the task but in my opinion those tools are a big overkill to it.

Cron is a tool included in Unix-like operating systems that automatically triggers processes at a given time frame, or moment. It uses jobs that each user installs individually and then depending on the times set, it will run them accordingly.

I had a question regarding cron:

  • Q: If its automatic then who runs it?
  • A: The user that installed the cronjob.

So this is where my first example derives, in order to answer the question I made a cronjob that would log into a file the username of the cronjob “runner”. To install such cronjob you just have to run:

crontab -e

The first time it might ask you what text editor you want to set as default, nano is usually the simplest choice; so just type the number for your desired editor and press enter. Now you will be prompted with the chosen editor and the following text is by default there:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

Notice that the whole file right now is just comments, nothing actionable yet; but we can read a very useful introduction on to how to use cron. In order to install a cron we just have to add a new line to the file we are editing, the line has five slots to indicate minute, hour, day of the month and month.

The following line adds a cron that will run every minute, it’s a simple command that outputs the user name (whoami) to the file /tmp/weeeee.log.

* * * * * whoami > /tmp/weeeee.log

In the folder /tmp/ every user has permision to write, so that’s why I usually use that folder to write the output of scripts. The silly name helps the future me knowing that the file with the silly name can be safely removed.

After the change of the minute on my machine clock, we can see the new file appear. and when we run the command cat to the file,

skalas@hostname:~
> cat /tmp/weeeee.log 
skalas

so the user running the cronjob is the user that runs the crontab command, or so it seems. What happens when we use sudo to configure the cron? First we run the crontab

sudo crontab -e

And we add the same line:

* * * * * whoami > /tmp/weeeee.log

We check the file and we see who is running the cron.

skalas@felurian:~
> sudo cat /tmp/weeeee.log
root

So we know who runs the commands when we use the crontab command. Hopefully this will also serve as an introduction as to how to make your first cron jobs. You can continue by reading the instructions that appear in the crontab command, reading the manual, and thinking about what other jobs you would like to automatize.

Este posteo fue publicado originalmente en el blog del autor aquí.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s