:::: MENU ::::

Home

  • Nov 07 / 2016
  • 0
Database

PostgreSQL – Check value is in array

With PostgreSQL, if you have a field containing an Array and you want to get all the results containing this value in the array, you can use the following command.

Here, we are looking for all the results in the table my_table containing my_value in the field my_array.

SELECT * FROM my_table WHERE 'my_value'=ANY(my_array) ORDER BY id;

   id  |        type        |             title             |             my_array                                                            
-------+--------------------+-------------------------------+------------------------------------
 93646 | this_is_my_type    | this_is_my_title              | {my_1,my_2,my_3,my_value,my_4,my_5}
 94184 | this_is_my_type    | this_is_my_title2             | {my_3,my_value,my_4,my_5}
 98240 | this_is_my_type    | this_is_my_title3             | {my_1,my_2,my_3,my_value}             


  • Oct 28 / 2016
  • 0
Linux

Add a confirmation dialog with bash

If you have a bash script doing some critical actions, it can be useful to add a confirmation dialog to double check with the user that he really wants to perform the action.

Here is a very simple script allowing this kind of checking:

#!/bin/bash
echo "Welcome to your favourite script!"
read -r -p "Are you sure you want to execute it? [y/N] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
  echo "Execution in progress..."
  # Write your action here
else
  echo "Action canceled."
  exit
fi
echo "End of script."

Output will look like:

  • Case ‘yes’:
    $ bash script_yesno.sh 
    Welcome to your favourite script!
    Are you sure you want to execute it? [y/N] y
    Execution in progress...
    End of script.
    
  • Case ‘no’:
    $ bash script_yesno.sh 
    Welcome to your favourite script!
    Are you sure you want to execute it? [y/N] N
    Action canceled.
    

It’s up to you customizing it depending on your needs and your feelings!

  • Oct 19 / 2016
  • 0
Linux

Apache Tomcat – Get logs directly cleaned with rotation

Tomcat allows you to rotate easily your log files with AccessLogValve but it can be more difficult to get cleaning in logs directories automatically.

For that, you can easily use some smart find commands combined with actions:

  1. Get logs older than 3 days compressed so they can be smaller but still accessible
    find /var/log/tomcat/ -mtime +3 -regex ".*\.\(log\|txt\)$" -exec gzip "{}" \;
  2. Get logs older than 60 days deleted permanently
    find /var/log/tomcat/ -mtime +60 -name "*.gz" -exec rm "{}" \;

You will probably have to adjust the logs path depending on your needs, and you can also modify the retention time for each action.
Once the command looks correct with what you’re expecting, you can set them up as cron so it can be executed automatically every day.

  • Oct 14 / 2016
  • 0
Linux

Generate a random file with specific size

It’s possible to easily generate a random file with specific size filled differently depending on the needs:

  • Fill a 200MB file with zeroes
    dd if=/dev/zero of=/tmp/file.out bs=1024KB count=200
    
  • Fill a 200MB file with random values (longer than zeroes – depending on processor)
    dd if=/dev/random of=/tmp/file2.out bs=1024KB count=200

  • Sep 27 / 2016
  • 0
Linux

Escape sequences for SSH (when you’re stuck)

You can get stuck with your ssh session when your network is changing or when there’s a network disruption. You will face a lock leading to the unability to stop your session, even by doing a CTRL-C.

That’s why there’s some escape sequences you can use in that case (especially the first one when you’re stuck):

  ~.  - terminate connection (and any multiplexed sessions)
  ~B  - send a BREAK to the remote system
  ~C  - open a command line
  ~R  - Request rekey (SSH protocol 2 only)
  ~^Z - suspend ssh
  ~#  - list forwarded connections
  ~&  - background ssh (when waiting for connections to terminate)
  ~?  - this message
  ~~  - send the escape character by typing it twice

Note that escapes are only recognized immediately after newline.

  • Sep 16 / 2016
  • 0
Linux

Renew a GPG key when you get “GPG error – KEYEXPIRED”

Getting this error when trying to update packages on your linux distro using apt?

$apt-get update
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://apt.puppetlabs.com jessie Release: The following signatures were invalid: KEYEXPIRED 1468001658

This just means that the GPG key is expired and that you need to renew it.

You can list expired keys and get the ID by using this command:

$apt-key list | grep expired
pub   4096R/4BD6EC30 2010-07-10 [expired: 2016-07-08]

Here, we can see that ID “4BD6EC30” is expired and is leading to the issue.

Let’s now update this key:

$apt-key adv --recv-keys --keyserver keys.gnupg.net 4BD6EC30

And you can now perform the update properly, you won’t get error anymore.

Question ? Contact