Archive

Articles taggués ‘commandes’

How to Run Cron Every 5 Minutes, Seconds, Hours, Days, Months

16/03/2019 Comments off

Source: thegeekstuff.com

Question: How do I execute certain shell script at a specific intervals in Linux using cron job? Provide examples using different time periods.

Answer: Crontab can be used to schedule a job that runs on certain internal. The example here show how to execute a backup.sh shell script using different intervals.

Also, don’t forget to read our previous crontab article that contains 15 practical examples, and also explains about @monthly, @daily, .. tags that you can use in your crontab.

1. Execute a cron job every 5 Minutes

The first field is for Minutes. If you specify * in this field, it runs every minutes. If you specify */5 in the 1st field, it runs every 5 minutes as shown below.

*/5 * * * * /home/ramesh/backup.sh

Note: In the same way, use */10 for every 10 minutes, */15 for every 15 minutes, */30 for every 30 minutes, etc.

2. Execute a cron job every 5 Hours

The second field is for hours. If you specify * in this field, it runs every hour. If you specify */5 in the 2nd field, it runs every 5 hours as shown below.

0 */5 * * * /home/ramesh/backup.sh

Note: In the same way, use */2 for every 2 hours, */3 for every 3 hours, */4 for every 4 hours, etc.

3. Execute a job every 5 Seconds

Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses ‘sleep 5′ command in it.

Lire la suite…

Categories: Système Tags: , , ,

Using Bash Arrays with Examples

13/03/2019 Comments off

bash-scripting-32-638Arrays can be a useful tool when coding your bash scripts.  The simplest way that I can define an array is to state that an array is a variable for a multi-instance dataset.

For example, a variable is used when there is a single value from a dataset like the IP Address of a server.  However, an array can be used to store all of the IP Addresses in your server room.

Speaking of IP Addresses and bash arrays, my last article (Detect and Block WordPress Brute Force Login Attacks) includes a script which is an example of how an array can be used in bash scripting.

Because arrays can be so useful in bash scripting, I thought that I would put together the following article detailing ways of Using Bash Arrays with Examples.

Initializing Bash Arrays or Assigning Values to Arrays

For arrays to be useful, we need to be able to assign values to them.  We assign values to an array by listing the array along with its instance number as shown below.  This method will assign each instance of the array one by one.

#!/bin/bash
myarray[0]=Hello
myarray[1]=World,
myarray[3]=Happy
myarray[4]=Friday

# Display all instances of the array
echo ${myarray[*]}

We can see above that in addition to being able to assign the values one by one, we can reference all array instances with an asterisk (*).  Another way to display all instances of the array is to use the following “echo ${myarray[@]}”

We run the script and get:

$ ./arrays.sh
Hello World, Happy Friday

We can also retrieve individual instances of an array by specifying the individual array instance number.  We modify the above script slightly to retrieve a couple of the instances.

#!/bin/bash
myarray[0]=Hello
myarray[1]=World,
myarray[3]=Happy
myarray[4]=Friday

# Display all instances of the array
echo ${myarray[0]} ${myarray[4]}

We run the script again and we get:

$ ./arrays.sh
Hello Friday

Lire la suite…

Linux Users and Sudo

11/03/2019 Comments off

Introduction

users sudoBefore we proceed, it would be best to cover some basic user administration topics that will be very useful in later chapters. Adding Users

One of the most important activities in administering a Linux box is the addition of users. Here you’ll find some simple examples to provide a foundation for future chapters. It is not intended to be comprehensive, but is a good memory refresher. You can use the command man useradd to get the help pages on adding users with the useradd command or the man usermod to become more familiar with modifying users with the usermod command.

Who Is the Super User?

The super user with unrestricted access to all system resources and files in Linux is the user named root. This user has a user ID, of 0 which is universally identified by Linux applications as belonging to a user with supreme privileges. You will need to log in as user root to add new users to your Linux server.

Debian Note: When installing Ubuntu Linux systems, you are prompted to create a primary user that is not root. A root user is created but no password is set, so you initially cannot log in as this user. The primary user can become the root user using the sudo su - command that will be discussed later.

How To Add Users

Adding users takes some planning; read through these steps below before starting:

1) Arrange your list of users into groups by function. In this example there are three groups « parents« , « children » and « soho« .

Parents    Children     Soho
Paul       Alice        Accounts
Jane       Derek        Sales

2) Add the Linux groups to your server:

[root@bigboy tmp]# groupadd parents
[root@bigboy tmp]# groupadd children
[root@bigboy tmp]# groupadd soho

3) Add the Linux users and assign them to their respective groups

[root@bigboy tmp]# useradd -g parents paul
[root@bigboy tmp]# useradd -g parents jane
[root@bigboy tmp]# useradd -g children derek
[root@bigboy tmp]# useradd -g children alice
[root@bigboy tmp]# useradd -g soho accounts
[root@bigboy tmp]# useradd -g soho sales

If you don’t specify the group with the -g, RedHat/Fedora Linux creates a group with the same name as the user you just created; this is also known as the User Private Group Scheme. When each new user first logs in, they are prompted for their new permanent password.

4) Each user’s personal directory is placed in the /home directory. The directory name will be the same as their user name.

[root@bigboy tmp]# ll /home
drwxr-xr-x 2 root root 12288 Jul 24 20:04 lost found
drwx------ 2 accounts soho 1024 Jul 24 20:33 accounts
drwx------ 2 alice children 1024 Jul 24 20:33 alice
drwx------ 2 derek children 1024 Jul 24 20:33 derek
drwx------ 2 jane parents 1024 Jul 24 20:33 jane
drwx------ 2 paul parents 1024 Jul 24 20:33 paul
drwx------ 2 sales soho 1024 Jul 24 20:33 sales
[root@bigboy tmp]#

Lire la suite…

tmux & screen cheat-sheet

08/03/2019 Comments off

screen and tmux

A comparison of the features (or more-so just a table of notes for accessing some of those features) for GNU screen and BSD-licensed tmux.

The formatting here is simple enough to understand (I would hope). ^ means ctrl+, so ^x is ctrl+x. M- means meta (generally left-alt or escape)+, so M-x is left-alt+x

It should be noted that this is no where near a full feature-set of either group. This – being a cheat-sheet – is just to point out the most very basic features to get you on the road.

Trust the developers and manpage writers more than me. This document is originally from 2009 when tmux was still new – since then both of these programs have had many updates and features added (not all of which have been dutifully noted here).

Actiontmuxscreen
start a new sessiontmux OR
tmux new OR
tmux new-session
screen
re-attach a detached sessiontmux attach OR
tmux attach-session
screen -r
re-attach an attached session (detaching it from elsewhere)tmux attach -d OR
tmux attach-session -d
screen -dr
re-attach an attached session (keeping it attached elsewhere)tmux attach OR
tmux attach-session
screen -x
detach from currently attached session^b d OR
^b :detach
^a ^d OR
^a :detach
rename-window to newname^b , <newname> OR
^b :rename-window <newn>
^a A <newname>
list windows^b w^a w
list windows in chooseable menu ^a « 
go to window #^b #^a #
go to last-active window^b l^a ^a
go to next window^b n^a n
go to previous window^b p^a p
see keybindings^b ?^a ?
list sessions^b s OR
tmux ls OR
tmux list-sessions
screen -ls
toggle visual bell ^a ^g
create another window^b c^a c
exit current shell/window^d^d
split window/pane horizontally^b « ^a S
split window/pane vertically^b %^a |
switch to other pane^b o^a <tab>
kill the current pane^b x OR (logout/^D) 
collapse the current pane/split (but leave processes running) ^a X
close other panes except the current one^b ! 
cycle location of panes^b ^o 
swap current pane with previous^b { 
swap current pane with next^b } 
show time^b t 
show numeric values of panes^b q 
toggle zoom-state of current pane (maximize/return current pane^b z 
break the current pane out of its window (to form new window)^b ! 

Source: dayid.org

Tmux (terminal multiplexer)

07/03/2019 Comments off

TmuxTmux, à l’instar de Screen, est un multiplexeur de terminaux, outil permettant d’exploiter plusieurs terminaux au sein d’un seul et même affichage.

Installation

Tmux n’est pas installé par défaut. Pour l’installer à l’aide d’un utilitaire graphique il suffit d’Installer le paquets tmux.
Par l’installer avec apt-get depuis un terminal, il suffit de saisir la commande suivante :

sudo apt-get install tmux

Utilisation de tmux

Depuis le tableau de bord (dash), un terminal ou encore une console saisissez la commande suivante :

tmux

Les principaux raccourcis

Tmux fait appel à l’ensemble de touches <Ctrl> <b> là ou screen fait appel à <Ctrl> <a>.

Les raccourcis et fonctions étant proches voire identiques à ceux de Screen, pour mieux les comprendre, reportez-vous à la page Screen.
 

Raccourcis de base

  • <Ctrl> <b> suivi de <c> : Créer un nouveau terminal dans la session tmux active
  • <Ctrl> <b> suivi de <n> : Switcher entre les différents terminaux de la session
  • <Ctrl> <b> suivi de <X> : Choisir un terminal spécifique (ou X est le numéro du terminal)
  • <Ctrl> <b> suivi de <d> : Se détacher de la session tmux (lancer ‘tmux a’ pour s’y rattacher)
  • <Ctrl> <b> suivi de <,> : Permet de renommer un terminal
  • <Ctrl> <b> suivi de <w> : Affiche la liste des terminaux disponibles
  • <Ctrl> <b> suivi de <t> : Afficher l’heure dans un terminal

Commandes dans un Split

  • <Ctrl> <b> suivi de <« > : Split vertical du terminal courant en deux ouverture d’un terminal dans le nouveau panel
  • <Ctrl> <b> suivi de <%> : Split horizontal du terminal courant en deux ouverture d’un terminal dans le nouveau panel
  • <Ctrl> <b> suivi de <o> : Switcher entre les terminaux splittés
  • <Ctrl> <b> suivi de <espace> : Changer l’organisation visuelle des terminaux splittés
  • <Ctrl> <b> suivi de <Alt> (flèches directionnelles) : Reduire, agrandir fenêtre du split
  • <Ctrl> <b> suivi de <!> : Convertir un split en terminal seul
  • <Ctrl> <b> suivi de <q> : Afficher les numéros des terminaux splittés
  • <Ctrl> <b> puis saisissez :join : permet de joindre un terminal seul dans un split

Par exemple, après avoir tapé le combo <Ctrl> <b> si vous saisissez

:join -v -s 3.0 -p 50

Où :

  • -h ou -v : pour horizontalement ou verticalement
  • -s 3.0 : terminal 3 et volet 0 (volet si écran splitté)
  • -p 50 : occupation à 50% de la fenêtre

Ici donc vous ajouterez verticalement, un terminal numéroté 3 et qui prendra 50% de l’espace total.

Lire la suite…