Outils pour utilisateurs

Outils du site


blog

Script shell interactif avec boîtes de dialogue (dialog boxes) - Whiptail

Voir :

Voir aussi :

  • dialog

Whiptail est inclus par défaut dans Debian

Exemple de script

dialogBoxes.sh

#! /bin/bash
 
set -o nounset
 
WHIPTAIL='env LANG=C whiptail'
 
#exec 2> /tmp/dlg-log-"$(date +%Y%m%d%H%M)".err
 
menu1()
{
	$WHIPTAIL --title "NODE" --menu "Choose a hostname" 25 78 16 \
	srv-1 "" \
	srv-2 "" \
	MAIN_MENU "" \
			2>/tmp/dlg-choix
}
 
menu2()
{	
	$WHIPTAIL --title "APP ON/OFF" --menu "On / Off" 25 78 16 \
	1_App_On "" \
	2_App_Off "" \
	MAIN_MENU "" \
			2>/tmp/dlg-choix
}
 
main_menu()
{	
	$WHIPTAIL --title "MAIN MENU" --menu "Select an option" 25 78 16 \
	1_Node "" \
	2_App_Activation "" \
			2>/tmp/dlg-choix
}
 
sortir()
{
	rm -f /tmp/dlg-choix
 	echo -e "\\n\\tBye !"
	exit 0
}
 
 
test_menu_option()
{
CHOIX=$(cat /tmp/dlg-choix)
rm -f /tmp/dlg-choix
 
case $CHOIX in
1_Node)
	menu1
   ;;
2_App_Activation)
	menu2
   ;;
1_App_On)
	echo "App_On ${HOST_SRV:-x}"
	sortir
   ;;
2_App_Off)
	echo "App_Off ${HOST_SRV:-x}"
	sortir
   ;;
MAIN_MENU)
	main_menu
   ;;
srv-1)
	HOST_SRV=1
	menu2
   ;;
srv-2)
	HOST_SRV=2
	menu2
   ;;
"")
	echo "Cancelled by user"
	sortir
   ;;
*)
	echo -e "Option unknown : \"${CHOIX}\""
	exit 2
   ;;
esac
}
 
main_menu
 
while true
do
	test_menu_option
done

Dialog

Exemples

(pv -n image.iso | dd of=/dev/sdb bs=1M && sync) 2>&1 | dialog --gauge "la commande dd est en cours d'exécution, merci de patienter..." 10 70 0
2025/03/24 15:06

Script shell commande timeout

Voir aussi :

A titre d'exemple. Idéalement il faut gérer le timeout avec Ansible

Exemple de code Ansible

    - name: command ldap_search
      become: true
      ansible.builtin.command: |
        /usr/bin/timeout --kill-after=15 10 /usr/bin/ldapsearch -LLL -D cn={{ lookup('env', 'LDAP_USER') }} -b o=unixauth -y /root/.ansible/tmp-files/.ldap.txt uid={{ CDO_ACCOUNT_USER | quote }} dn
      changed_when: false
      check_mode: false
      register: ldapsearch
      environment:
        LANG: C
      failed_when:
        - ldapsearch.rc != 0       # OK
        - ldapsearch.rc != 124     # Timeout. SIGTERM
        - ldapsearch.rc != 137     # Timeout. SIGKILL

    - name: fail when timeout - SIGTERM
      fail:
        msg: "Error_Command_Timeout. Process arrêté par SIGTERM."
      when: ldapsearch.rc == 124

    - name: fail when timeout - SIGKILL
      fail:
        msg: "Error_Command_Timeout. Process arrêté par SIGKILL."
      when: ldapsearch.rc == 137

    - name: fail if ACME user
      fail:
        msg: |
          Error_ElementAlreadyUsed. Nom de compte utilisateur déjà utilisé par ACME {{ CDO_ACCOUNT_USER }}.
      when:
        - ldapsearch.stdout_lines |select('regex', '^dn:') |list is regex(',o=unixauth')
        - not allow_ldap_acme

Concernant Ansible voir aussi https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_async.html

async pool timeout

play1.yml

#!/usr/bin/ansible-playbook
 
---

- name: test
  hosts: localhost
  gather_facts: false
 
  # TIMEOUT_MAX = async	                # when poll != 0
  # TIMEOUT_MAX = retries x delay	# when poll == 0

  tasks:
    - name: Command
      shell: |
        sleep 5
        date >> /tmp/date.log
      poll: 0           # When pool is a positive value, the playbook will still block on the task until it either completes, fails or timeout
      async: 2          # if poll != 0 : fail when task is longer than async value
      register: cmd_sleep

    - name: DEBUG 10
      debug:
        msg: "Command en cours d'execution"

    - name: Async_status
      async_status:
        jid: "{{ cmd_sleep.ansible_job_id }}"
      register: job_result
      until: job_result.finished
      retries: 3        # MAX TIMEOUT VAR
      delay: 2          # MAX TIMEOUT VAR

    - name: DEBUG 20
      debug:
        msg: "Execution terminée"
2025/03/24 15:06

Lister la table des process dans un fichier txt avec top

top -n 1 -b > /tmp/processtable.txt
top -b -n 1 -o '%CPU' |sed 1,7d
 
getCpu="$(top -b -n2 -d10 | awk '/PID USER/ {getline;printf "Process: %s, cpu: %s %\n",$12,$9}' | tail -1)"
typeset -i cpu="$(echo $getCpu | awk -F"," '{print $2}'|  awk -F":" '{print $2}' | awk '{print $1}')"
2025/03/24 15:06

PyContracts

Voir :

Voir aussi :

  • icontract
  • mypy

Voir programmation par contrat en générale :

pipenv install PyContracts
# coding: utf-8
 
from contracts import contract
 
#contracts.disable_all()
 
@contract
def div(a, b):
    """
    Simple division
    :type a: int,>0
    :type b: int,>0
    :rtype: float,>0
    """
    return a / b
2025/03/24 15:06

TODO Pourquoi le Logiciel Libre

Richard Stallman, Créative Commun, Partage, License, Copyleft, GNU, GPL

Voir :

2025/03/24 15:06
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki