Bash: How do I get the command history in a screen session?

16/12/2023 Categories: Système Tags: , , Comments off

If I start a screen session with screen -dmS name, how would I access the command history of that screen session with a script?

Using the , the last executed command appears, even in screen.

ANSWER:

I use the default bash shell on my system and so might not work with other shells.

this is what I have in my ~/.screenrc file so that each new screen window gets its own command history:

Default Screen Windows With Own Command History

To open a set of default screen windows, each with their own command history file, you could add the following to the ~/.screenrc file:

screen -t "window 0" 0 bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'
screen -t "window 1" 1 bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'
screen -t "window 2" bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'

Ensure New Windows Get Their Own Command History

The default screen settings mean that you create a new window using Ctrl+a c or Ctrl+a Ctrl+c. However, with just the above in your ~/.screenrc file, these will use the default ~/.bash_history file. To fix this, we will overwrite the key bindings for creating new windows. Add this to your ~/.screenrc file:

bind c screen bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'
bind ^C screen bash -ic 'HISTFILE=~/.bash_history.${WINDOW} bash'

Now whenever you create a new screen window, it’s actually launching a bash shell, setting the HISTFILE environmental variable to something that includes the current screen window’s number ($WINDOW).

Command history files will be shared between screen sessions with the same window numbers.

Write Commands to $HISTFILE on Execution

As is normal bash behavior, the history is only written to the $HISTFILE file by upon exiting the shell/screen window. However, if you want commands to be written to the history files after the command is executed, and thus available immediately to other screen sessions with the same window number, you could add something like this to your ~/.bashrc file:

export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"
Categories: Système Tags: , ,

Scripts shell de sauvegarde

15/12/2023 Categories: Système Tags: , , , Comments off

Une des façons les plus simples de sauvegarder un système utilise un script shell. Par exemple, un script peut être utilisé pour configurer les répertoires à sauvegarder et transmettre ces répertoires comme arguments à l’utilitaire tar, ce qui crée un fichier d’archive. Le fichier d’archive peut ensuite être déplacé ou copié dans un autre emplacement. L’archive peut également être créée sur un système de fichiers distant tel qu’un montage NFS.

L’utilitaire tar crée un fichier d’archive de plusieurs fichiers ou répertoires. tar peut également filtrer les fichiers par le biais des utilitaires de compression, réduisant ainsi la taille du fichier d’archive.

Categories: Système Tags: , , ,

Learn Bash: Remove Commands From Your History

15/12/2023 Categories: Système Tags: , , , Comments off

Occasionally I type a password or other sensitive information into a shell prompt. Using bash history, the command can be removed.

# say we start with an empty bash command history
bash-3.2$ history
 1 history
# enter a command that requires a password
bash-3.2$ sudo rm -i some_file
Password:
# accidentally ^C and type your password
# into the prompt and hit enter
bash-3.2$ secret_password
bash: secret_password: command not found
# your password is now there for all to
# see in your bash history
bash-3.2$ history
 1 history
 2 sudo rm -i some_file
 3 secret_password
 4 history
# first option to fix it, delete the numbered entry from
# history and write to your ~/.bash_history file
bash-3.2$ history -d 3
bash-3.2$ history -w
# entry 3 will be removed entirely from your command history
bash-3.2$ history
 1 history
 2 sudo rm -i some_file
 3 history
 4 history -d 3
 5 history -w
 6 history
# the second option is to clear the entire history
# and write the changes to disk
bash-3.2$ history -c
bash-3.2$ history -w
# it's now pretty obvious that your history has been
# scrubbed clean, but at least your password is history!
bash-3.2$ history
 1 history -w
 2 history
Categories: Système Tags: , , ,

Dumper une base MySQL avec horodatage dans le nom du fichier

14/12/2023 Categories: Système Tags: , , , Comments off

J’ai un projet qui utilise la base de données MySQL. Je souhaite sauvegarder la base de données tous les jours, donc j’utilise ceci:

mysqldump -h host -u user -p database de mots de passe> 'location.sql'

Je souhaite que les fichiers soient nommés avec l’horodatage, c’est-à-dire:

Aujourd’hui, le fichier sera nommé quelque chose-07-05-2014 08-00-00
Demain sera quelque chose-08-05-2014 08-00-00

Comment ajouter un timestamp formaté avec le nom de fichier exporté?

Réponse:

Une solution serait:

mysqldump -h host -u user -p password database > quelque chose-$(date +%d-%m-%Y %H %M %S).sql

Pour un horodatée qui permette le tri correct des fichiers, il fait changer l’ordre des paramètres et utiliser:

%Y-%m-%d

de manière à trier sur année, mois puis jour. Ne rien changer pour les hh:mm:ss puisque le tri se fair naturellement dans cas.

Pour automatiser ce dump, il faut insérer cette commande dans le crontab (du root ou de l’utilisateur):

0 */8 * * * root /usr/bin/mysqldump -h host -u user -p password database > quelque chose-$(date +%d-%m-%Y %H %M %S).sql

pour que la commande s’exécute toutes les 3 heures (24h ÷ 8).

Categories: Système Tags: , , ,

Administration Linux Avancée : commandes utiles

13/12/2023 Categories: Système Tags: , Comments off

I► Dans cet article, nous allons aborder les commandes d’administration Linux avancé :

=> mkfifo et script permettent d’initialiser un fichier de type pipe et de récupérer à distance les commandes saisies – exemple :

Un user saisit mkfifo NOmDuFichier (tube), puis démarre le script pour enregistrer les commandes dans ce tube : script -f tube

Un second user (par SSH ou en local) saisit cat tube : le script se met en marche , tout ce qui est saisi par le 1er user est visualisé par le second user :

Le premier user peut mettre fin au scripr en saisissant CTRL+D

=> logger permet d’écrire directement dans le fichier de log principal  :

=> Write permet d’écrire directement sur le terminal d’un utilisateur connecté :

=> WALL permet d’écrire sur tous les terminaux ouverts 

wall “fermez vos pc, c’est les vacances !!!”

 

Source: Michel Bocciolesi

Categories: Système Tags: ,