Crontab every seconds

Welcome again!

Today I will post a quick tip, something useful with a “clever” solution. Crontab every X seconds, let’s see the script.

Your script
Just modify the run function of what you want to do it every $SECLAUNCH seconds. This will be lauched once, even if the run functions needs more than $SECLAUNCH seconds. If it take less than, the function run will be launched at $SECLAUNCH – timeelapsed.

#!/bin/bash
# USING runScriptEverySeconds.sh
# Load defaults config
. /scripts/config # Loading defaults configurations
 
##########################
# Seconds between launch #
##########################
SECLAUNCH=5
     
###################
# FUNCTION TO RUN #
###################
     
function run {
   runYourScriptEvery5seconds.sh
}
     
. /scripts/runScriptEverySecond.sh #Script who make it possible running every seconds

Here is the script you have to set as /scripts/runScriptEverySecond.sh

#!/bin/bash

########### SCRIPT RUN COMMAND EVERY $SECLAUNCH SECONDS #################
#									#
# This scripts will launch function run() every SECLAUNCH seconds. 	#
# If the scripts need more than SECLAUNCH will be launched as soon 	#
# as the last ends.							#
#									#
# 			-- IMPORTANT --					#
#									#
# User needs to add .defaultScripts/runScriptEverySeconds.sh at the 	#
# end and define SECLAUNCH and run funcion.				#
#									#
#########################################################################

##########################
# Seconds between launch #
##########################
# SECLAUNCH=5

#######################
# FUNCTION TO DEFINE  #
#######################

# function run {
# 	date
# 	sleep $(($RANDOM%5+1))
# }



###############################################
#		DO NOT EDIT		      #
###############################################

DATE=`date +%s`

# Launch command

for (( ; ; ))
do
	NOW=`date +%s`
	RES=$(($NOW - $DATE))
	#echo $RES $SECLAUNCH
	if [ $RES -ge $SECLAUNCH ]
	then
		#echo "INSIDE"
		DATE=`date +%s`
		run
	else
		sleep 1
	fi
done

If you have questions, don’t worry! ask me here or at twitter @becloudin

Stay cloud!

Google Maps div grey?

 

Hi again!

This will be a very short shoot of a recent problem I have been in a project. The problem was related with Google Maps (gmaps V3).

The problem was in the div of the map, I get part of it in grey. But all the functionalities work, I get my pointers, I can zoom, move the map, but I still getting grey zones.

This picture will help you to understand if you have similar problem.

The most amazing think was that all the code that generate the map, and the position of the map were OK! I tried the same code outside where is has to be placed and everything works. My first thought was the CSS. I have been using complex CSS styles, but nothing worked.

The solution, of course, StackOverflow has the solution. The problem was that the div where GMaps is placed, in this case, in the beginning of the stage the div was hidden, it causes gmaps scripts doesn’t get correct sizes and prints locations in grey.

How to solve? In Stackoverflow they try to use fixed position, in my case, not the best solution. My solution, generate gmaps at the same time that the user try to show the hidden part of the page where where the map is it placed. The bad think, user will have to wait until Google returns all the data of the map, just a second. Strange problem, with difficult problem source, but with easy solution 😉

Stay cloud!

Vacaciones, timelapse.

Hola de nuevo,

Este post es a modo de reencuentro. Después de casi un mes sin escribir, algo tenía que poner en el blog, algo interesante, algo que a la gente siempre le gusta y que raramente comparte, su vida privada. Bueno, creo que eso era así hasta la aparición de Facebook, Twitter y tantas otras redes sociales.

En este caso, estas vacaciones he estado probando una nueva técnica fotográfica, el timelapse. Con mayor o menor fortuna, pero lo he intentado. Sin duda hay que practicar, pero por algo se empieza. ¡Espero que os guste!

 

Como bonustrack, os dejo el comando para codificar todas las imagenes en un vídeo via FFMPEG. Cuando controle un poco más el tema ya haré un post sobre How to make timelapse. Pero eso será en próximos capítulos.

ffmpeg -f image2 -r 24 -vcodec libx264 -i img_%04d.JPG ~/Desktop/video5.24fps.mov

 

 

Comentarios, sugerencias, serán bienvenidas!

Arbre Empathique Brest – Disable Failsafe Grub on Ubuntu Server

Hi everybody!

Today I will talk a something it happend me at my job. First I will explain in what I was working, later I will explain the problem and the easy solution.

If you follow me at LinkedIn you will know that I work for ADmira Digital Networks, one of the best digital signage companies. This weeks I’ve been working for a project called Arbre Empathique. This project consist in a very big iron tree placed in Brest with hydrophonic plants, cameras, screens, wifi and sensors. The cameras provide real time video to the screens, player takes images from the cameras, data from sensors sensors and shows different content at the same time everything at realtime!. The Wi-Fi was the method to connect people with the tree to get more information from it. If you want to know more, check the ADmira post or just ask ;).

The Wi-Fi was the problem, well, the ubuntu server that is behind the access point. As you probably don’t know, the digital signage  hardware and software has to be prepared to be placed in the worst sites you could ever imagine, and I can promise Brest is one of the worsts I’ve ever see. You have to be prepared to get energy fails, cold, warm, windy, raining, and everything has to work. That’s the reality of digital signage, it’s quite different from a normal software company, but you have to be prepared for the worst.

The problem was the ubuntu server 12.04, it works really great, but it held in Grub window waiting for a keyboard keystroke choosing a boot option. That couldn’t be a problem, if you get your servers in your office, but when your server is located in more than 1200Km, yes, that’s a problem.

The solution, change the Grub script adding a timeout when recordfail gets 1. Let’s see what was there, and what I’ve done it.

/etc/grub.d/00_header

make_timeout ()
{
    cat << EOF
if [ "\${recordfail}" = 1 ]; then
  set timeout=-1 # This tells Grub to wait the keystroke choose
else
  set timeout=${2}
fi
EOF
}

Here the solution, change the -1 to 10.

make_timeout ()
{
    cat << EOF
if [ "\${recordfail}" = 1 ]; then
  set timeout=10 # Here we can just wait 10 seconds 
else
  set timeout=${2}
fi
EOF
}

Remember to run update-grub as root and you’ll never be stucked again.

Just with that change our ubuntu will work although any kind of energy failure happens. Of course, hardware fails and you have to be prepared! That’s digital signage 😉

To finish you can see the video from Cloud9 the architects of that project.

 

Stay in the cloud!

Éxito = Proyecto + Comunicarlo + Optimismo

Hola de nuevo,

Este será el primer post en español y con él decidiré una nueva manera de llevar este blog. A partir de ahora, todos los posts que no tengan relación con el mundo de la informática (emprendeduría, economía, curiosidades…) los escribiré en español. La razón, los sentimientos, creencias y habilidades linguísticas mejoran cuando uno utiliza su lengua materna. Para nutrir el conocimiento y resolver dudas técnicas, uno debe buscar en inglés y probablemente encuentre la solución y puede que la encuentre aquí.

Después de este pequeño inciso, vamos a lo que realmente importa.

Estos días, semanas, meses de dura crisis cuesta mucho ver gente con optimismo, gente con ganas de hacer cosas. Podríamos entenderlo, no tienen trabajo, no saben qué hacer y papá estado tiene los mismos problemas (cómo para confiar en él para que te guie…). Este país necesita algo que lo saque de donde está y ese algo son sus personas. Ni rescates, ni bancos, ni la tía de la lejía.

Hoy mismo viendo la televisión (324.cat – Valor Afegit) aparecía un head hunter hablando sobre como selecciona a altos directivos. Personas altamente cualificadas, con un nivel de vida muy elevado pero muchos de ellos en la misma situación que el resto, en el paro. Muchas empresas han cerrado, y las que siguen, no pueden permitirse los sueldos de antaño.

Para ellos, el head hunter aconsejaba que tuvieran un plan B y saberse adaptar a la situación ( más trabajo y menos sueldo). Este plan B podía consistir en tener algo qué hacer. Un pequeño estudio, una pequeña tienda, algo con lo que salir cada día y no dejar de hacer funcionar nuestro cerebro. Para los que no disponían de un plan B, y sobre todos para aquellos más acomodados a su antigua posición en la empresa, la solución era no estar en casa, sino salir, pensar, idear…

De la entrevista me quedo con esta frase:

Éxito = Tener un proyecto + Saberlo comunicar + Tener optimismo

Para tener éxito, necesitas tener proyecto en el que centrarte, debes saber comunicar este proyecto para que todos entiendan qué es lo que quieres, y sobre todo,  debes ser optimista. Las personas optimistas son aquellas que delante cualquier dificultad se levantan y vuelven a andar. Pero sobre optimismo, hablaremos en otro post.

Un saludo,

Stay cloud!

What imagination can do

Hi again!

Imagination, the most important skill that everyone should have. But this society teach us how to think, what’s good and what isn’t, what we have to do and what we can’t do. And society and school kills our imagination.

Why children have hundreds of tons of imagination? Why they can be extremely innovative, why the adults can’t be like a child? Where is our imagination? Can we learn from children? We can? Sure!

This example inspire this post. It’s a short film of Caine’s Arcade. Pretty nice video, so inspiring… 😉

 

 

This kid just reinvent the weel, but their personal point of view. Isn’t great that kid has developed a completely functional fun games just with a few boxes a lot of imagination. Why we can do the same? Can we stop thinking like an adult and start thinking like a child? Could this help us to be more openminded?

There is a game, that reveals how people is focused in their own limits, and in this case in the limits of the dots. Let’s see the game.

Easy game, try to mark the 6 points with just 4 lines, easy? now do it with out lift your pen from the paper, easy too? 😉

If you need help, contact me at Twitter @becloudin and you will get the answer.

 

Stay cloud!

 

 

 

Google datacenter security

Hi!

A few days ago I watched at Youtube an interesting video of Google Datacenter security. I would like to remark the intensive vigilance to disks. If they appreciate any kind of bad performance or error, they get this hard drive to an specialized lab. There the engineers try to know what’s going on, if there is nothing to to with this drive, the destruction is their final stage. Quite interesting methods…

A few years ago, I went to a Google talk in my university (UPC – FIB), one of the most amazing thing I remember from that talk was that the most expensive bill that Google has to pay, are not the salaries nor the bonus, the most expensive bill was the energy. If I don’t remember bad, they said that more o less Google spends 1M$ in energy every month per datacenter. That’s why Google is investing a lot of money in renewable energy companies.

Take a look of the video!

 

Stay cloud!

Personal Branding

Hi again!

The work, and projects make this blog a little difficult to maintain, but if you want, there are time to do everything, the limits are just in your mind.

I have lots of things to talk about and today I will write about a non technical aspects. I will talk about Personal Branding©.

I am not an expert of personal branding, but after reading a little bid, I will try to explain what is personal branding for me. In my job, as Sysadmin and System analyst, the most important points to be improved are the confidence between team and clients. If your team trust in your work and your skills this will improve different things.

The first, you. If you feel you are doing a great work, the next time a problem appears you will have the enough courage to face up and beat it. When I was young I played basketball, once my trainer said me, the difference between the teams of A league and B league are not the height, strength, of the velocity, what really makes the difference is in the mind. The A teams know they will score, and if they fail, they will try again and again with the same confidence. The B teams, once they fail the first score they loose confidence in their own, and the next shot the most provably it that they will fail again.

Believe you can do it, is the biggest help you can have.

This video can help to believe in my words.

Next point, in next post. Stay cloud!

Log rotate

Hi Cloud!

In this article we will explain an example of how to rotate logs. Ubuntu linux includes Logrotate a powerful tool that will help us to take everything under control.

Let’s starts.

Logrotate it’s commonly used to pack and save logs for a defined period of time. For example, logs of Apache webserver could be useful for just 1 month. Once we have one month and one day, the oldest log will be automatically deleted. Other logs could be saved more time, for example auth.log. This is completely configurable and really easy to set.

Other common configurations are the compress. The most of the logs are simple text lines one beside other. This kind of file are easy to compress and save lot of space. Logrotate has the option to do it. Other option is to run a script after o before logrotate starts. This is useful for example to save logs into other devices like tapes, other disks or uploaded to cloud services.

Other options, like notify by e-mail, notify if empty, are very interesting and you could try to use the manual and get more information. Remember, man logrotate and you will find everything you need.

One important thing with I get some problems is that logrotate could save gzip logs in other directory, BUT, and that’s very important, in the SAME DEVICE. If you want to save it elsewhere, you will have to write your own script to do it. Quite easy, but you will have to know it.

Let’s see the examples:

Log rotate NGINX
/etc/logrotate.d/nginx


/usr/local/nginx/logs/access.log {
        # We run logrotate every day
        daily 
        # If the log file is missing, go on to the next one without issuing an error message. See also nomissingok.
        missingok
        # Delete logs after 7 days in store
        rotate 7
        # Compress this logs
        compress
        # Delay this compress. This will perform access.log.1 and the next one will be compressed. This helps when you have to take a look of the day before log without having to uncompress the file.
        delaycompress
        # Do not rotate the log if it is empty
        notifempty 
        # Create new log with permisions 640, user root and group adm
        create 640 root adm
        # Needed to run ownscripts
        sharedscripts
        # Run it after rotate
        postrotate
                # Script to save Nginx logs in other device
                /scripts/logNGINXRotate.sh
                # Restart Nginx
                /usr/local/nginx/sbin/restartNginx.sh
        endscript
}

Scripts to save Nginx logs to other device.
This script save the rotated logs directly to S3 using s3fs mounted directory.
/scripts/logNGINXRotate.sh

#!/bin/bash
LogDate=$(date +"%Y-%m-%d.%H:%M")

logdir='/usr/local/nginx/logs/'
logdestdir='/mnt/s3/logs/api/nginx'

for filename in `ls $logdir | grep gz`
do

	mv $logdir$filename $logdestdir/access.$LogDate.tar.gz
done;

Scripts that restarts Nginx
/usr/local/nginx/sbin/restartNginx.sh

#!/bin/bash

pid=`cat /usr/local/nginx/logs/nginx.pid`
date=`date`

echo $date" ## RESTARGING NGINX ## PID:"$pid
kill -HUP $pid

Apache
Example scripts for Apache webserver.

/var/log/apache2/*.log {
        daily
        missingok
        rotate 30
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then
                        /scripts.servers/api/logApacheRotate.sh
                        /etc/init.d/apache2 reload > /dev/null
                fi
        endscript
}

Scripts to rotate Apache webserver.
/scripts.servers/api/logApacheRotate.sh


#!/bin/bash
LogDate=$(date +"%Y-%m-%d.%H:%M")

logdir='/var/log/apache2/'
logdestdir='/mnt/s3/logs/api/apache'

for filename in `ls $logdir | grep gz`
do

	mv $logdir$filename $logdestdir/$filename.$LogDate.tar.gz
done;

The final step is to verify that everything do what you expect. With this command you can try at any time every change you do.

 logrotate --force /etc/logrotate.d/yourLogRotateFile

Stay cloud!

Generate SSH keys

Hi cloud!

Today I explain how to generate public/private keys. This is very useful for a lot of services that require something more secure than a typed password. The most common usage for private Key is the ssh connection. If we use private key we will never need to type password again, saving hundreds of hours and more than one security problem.

How we can create our new key?

 $ ssh-keygen 

Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
a1:04:aa:73:5a:8f:24:14:70:24:ca:30:37:ca:92:c1 ubuntu@ip-10-22-22-22
The key's randomart image is:
+--[ RSA 2048]----+
|Oo= .            |
|=E.o .           |
|=+.   . .        |
|o.   . . .       |
|+ +   . S        |
| B o             |
|. . .            |
|                 |
|                 |
+-----------------+

That’s it! We will have our key generated. We can find them at ~/.ssh/id_rsa.pub (Public key) and ~/.ssh/id_rsa (Private key). The private key is the file you NEVER provide to anyone. And remember to get correct permisions (600). If not, you will be noticed of incorrect permissions.

To change that permissions:

 $ chmod 600 ~/.ssh/id_rsa 

In next posts we will explain new techniques to save even more time in your normal commands.

But this will be in a few days!

Stay cloud!

Uso de cookies

Este sitio web utiliza cookies para que usted tenga la mejor experiencia de usuario. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.plugin cookies