Crontab: Scheduling tasks

In this post we will see how to schedule task in unix systems with the use of crontab.

Frequently we need to run processes in the background in out web servers like generate thumbnails, execute mantenance MySQl queries, etc. Unix systems have a great tool named cron. This tool allows us to run processes automatically in regular intervals. Also, we can create backups, synchronize files and a much more. To do all these stuff we have crontab.

The crontab command is used in Unix systems to schedule other programs to be executed periodicaly. To see what crontabs are currently running in our system, we can run:

user@unix:~$ crontab -l

To edit the crontabs we can run:

user@unix:~$ crontab -e

This command will open our editor by default to allow us to edit the crontab. To write our crontabs we can use this format:

user@unix:~$ * * * * * php /var/www/hasheado/script.php

Explaining the format

As we can see in the above command there are 5 asterisks. The asterisk represent different date and time parts:

  1. minutes (from 0 to 59)
  2. hour (from 0 to 23)
  3. day of month (from 1 to 31)
  4. month(from 1 to 12)
  5. day of the week (from 0 to 6) (0 = Sunday)

Executing a command every minute

If we use an asterisk in all parts:

user@unix:~$ * * * * * php /var/www/hasheado/script.php

This will execute the script /var/www/hasheado/script.php:

  1. every minute
  2. every hour
  3. every day of the month
  4. every month
  5. every day of the week

Well that is, I hope it’s helpful, crontab is so powerful.

Unix basic commands

This is a recompilation of helpful commands for Unix systems.


List directory content

To list the content of a specific directory we can use the ‘ls’ command. The syntax is ls [options] [directory] being the ‘-l’ a really helpful option, which shows the directory content with details:

user@unix:~$ ls -l
drwxr-xr-x 2 user user 4096 2010-03-10 00:03:45 Desktop
drwxr-xr-x 2 user user 4096 2010-01-19 22:33:05 Documents


The pipe “|” command

This command allows us to pipe the outputs and inputs of other processes. The concatenation of pipe commands is very helpful and powerful, and it is really used on Unix systems. An example is:

user@unix:~$ cat fichero1 fichero2 | grep palabra | sort | uniq


The tee command

tee is a command using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipe.

user@unix:~$ sh | tee deploy.txt



To create symbolic links we can use the ‘ln’ command. For instance:

user@unix:~$ ln -s /path/to/link [new_alias]

With the above command we create a file named ‘new_alias’ which points out to the ‘/path/to/link’.


Show the last lines of a file

The command ‘tail’ shows the last lines of a file. For instance:

user@unix:~$ tail -f -n 10 file.txt

Shows the last 10 lines of the file.txt file.


Show the command documentation

A really helpful command is the ‘man [command]’ command which shows the documentation of the given command , for instance:

user@unix:~$ man tail

It will show the documentation for the tail command.


Flush DNS cache

If we need to flush our DNS cache we could run:

user@unix:~$ sudo /etc/init.d/dns-clean restart


Count files and/or folders

Sometimes we need to kwno how many files and/or folders we have in a specific path, to do that we could run:

user@unix:~$ ls -lR | grep ^d | wc -l


Check disk usage

If we want to check the usage of our hard disk we should run:

user@unix:~$ du -h -a /dir | grep "[0-9]G\b"


Count files in folder

Note that it doesn’t count hidden files and assumes that file names don’t contain newline characters.

user@unix:~$ ls | wc -l