Archive

Posts Tagged ‘Ubuntu’

Cron jobs – how to create and use them

September 25th, 2011 No comments

Hello to all in this article I’ll talk about cron jobs, so that it serves and how we can use them in everyday life.

Often in our daily lives, we need to control or create some routines that we can not always do them simply using a desktop interface or sever-side language.

Let’s imagine the following situation: Once a week, we need to create the XYZ system backup, for security reasons. This backup is always at night, because it is a quieter period in the day. Unfortunately there are not always available resources to run this routine at night, or we simply forget to run this routine.

To solve this problem, we can create a cron job, once a week it runs a command on the server and create a backup file.

Here’s what wiki says:

Cron is a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.

Ok, so let’s create a cron job to do this weekly backup of our system.

The first thing we need to do is create the cron file, in a Linux terminal or putty (windows), use the following command line:

crontab-e

This command will create the cron job file, its original content is the following:

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

#

# m h  dom mon dow   command

 

To simplify this first time, we will divide the cron job into two parts:

# 1 – When we want the cron run the routine

# 2 – What is the command to be executed.

So here goes, to set the date and time the cron will run, we should use the following parameters:

1 – Minute: (0-59)

2 – Hour: (0-23)

3 – Day of the week: (1 – 31)

4 – Month (1-12)

5 – Day of the week: (0-7) Sunday (0 and 7)

Asterisk ( * )

The asterisk indicates that the cron expression will match for all values of the field; e.g., using an asterisk in the 4th field (month) would indicate every month.

Now let’s check the line command we want the cron job to run in server, it can be a simply call in the server or a call to a sh file passing parameters. For now let me show a simple call in a line command:

tar -zcf /home/ibraim/mytarfile.tar.gz /home/ibraim/mysistemfolder/

This line command will zip (tar), into mytarfile.tar.gz file all items in “/home/ibraim/mysistemfolder/” folder.

0 23 * * 2 tar -zcf /home/ibraim/mytarfile.tar.gz /home/ibraim/mysistemfolder/

So this cron job will execute the tar command every Monday at 23:00, every month.

Categories: Linux Tags: ,

Manipulando arrays em php Parte 8 – funções

January 14th, 2011 No comments

Olá a todos, continuando com os posts sobre arrays, gostaria de falar sobre algumas funções ultilizadas para a
manipulação de arrays como : ordenação, intersecção, acesso e outras.

in_array

Esta função checa se um valor existe em um array

Descrição

bool in_array ( mixed $needle , array $haystack [, bool $strict ] )

Procura em haystack pelo valor needle.

Parâmetros

Parâmetro Descrição
needle O valor procurado. Nota: Se needle for uma string, a comparação é feita diferenciando caracteres maiúsculos e minúsculos.
haystack O array.
strict Se o terceiro parâmetro opcional strict for passado como TRUE então in_array() também fará uma checagem de tipos de needle em haystack.

Valor Retornado

Retorna TRUE se needle é encontrado no array, FALSE caso contrário.

Histórico

Versão Descrição
4.2.0 needle pode agora ser um array.

Exemplos

Exemplo #1 Exemplo da in_array()

				$os = array("Mac", "NT", "Irix", "Linux");
				if (in_array("Irix", $os)) {
					echo "Tem Irix";
				}
				if (in_array("mac", $os)) {
					echo "Tem mac";
				}

A segunda condicional falha pois in_array() diferencia letras minúsculas e maiúsculas. Então, a saída seria:

Tem Irix
Exemplo #2 in_array() com checagem de tipos

				$a = array('1.10', 12.4, 1.13);

				if (in_array('12.4', $a, true)) {
					echo "'12.4' encontrado com checagem de tipo\n";
				}
				if (in_array(1.13, $a, true)) {
					echo "1.13 encontrado com checagem de tipo\n";
				}

O exemplo acima irá imprimir:

1.13 encontrado com checagem de tipo
Exemplo #3 Exemplo de in_array() passando um array para needle

				$a = array(array('p', 'h'), array('p', 'r'), 'o');

				if (in_array(array('p', 'h'), $a)) {
					echo "'ph' foi encontrado\n";
				}
				if (in_array(array('f', 'i'), $a)) {
					echo "'fi' foi encontrado\n";
				}
				if (in_array('o', $a)) {
					echo "'o' foi encontrado\n";
				}

O exemplo acima irá imprimir:


'ph' foi encontrado

'o' foi encontrado

Referencias:

Documentação PHP: http://php.net

Categories: PHP - Geral Tags: , , ,