Supervisor: A Process Control System

For these days I had the possibility to work with a great tool in my job. This tool allows us to monitor and control a number of processes on Unix operating systems.

As their website says: “Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.“.

Supervisor is written in Python and based on a configuration file (supervisord.conf) the daemon (supervisord) keeps monitoring for defined processes are running, and if the process is killed by whatever cause the supervisord start it again.

A configuration file looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[supervisord]
logfile=/var/log/supervisord.log

[unix_http_server]
file=/tmp/supervisor.sock
username=supervisor
password=mysecret

[rpcinterface:supervisor]
supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface

[inet_http_server]
port=*:5858
username=supervisor
password=mysecret

// Programs
[program:email_sender]
command=/usr/bin/php email_sender.php --arg1=a --arg2=b
user=www-data
autorestart=true
stderr_logfile=/var/log/email_sender_%(process_num)1d.err.log
stdout_logfile=/var/log/email_sender_%(process_num)1d.out.log
numprocs=2
process_name=%(program_name)s_%(process_num)02d

And a web app shows us a status of processes:

supervisor-screenshot

For more information you can read the documentation.

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.

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:

1
user@unix:~$ crontab -l

To edit the crontabs we can run:

1
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:

1
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:

1
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:

1
2
3
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:

1
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.

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

 

Symlinks

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

1
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:

1
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:

1
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:

1
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:

1
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:

1
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.

1
user@unix:~$ ls | wc -l