[Script shell] Come terminare tutti i processi figli

Ragazzi mi trovo in questa situazione, in cui ho uno script che crea diverse interfacce grafiche con YAD. Adesso voglio che quando viene premuto il tasto EXIT su una qualsiasi di queste interfacce tutti i processi generati vengano terminati.

Questo è l’ albero delle chiamate:

├─antolli-search-─┬─antolli-search-───yad───2*{yad}]
                    ├─antolli-search-
                    └─yad

Come posso fare ?

Considerate che lo script fa qualcosa del genere:

generaGUI_1 () {...}
generaGUI_2 () {...}
...
verifica_tasto_premuto () {...}

generaGUI_1 | generaGUI_2 ....
verifica_tasto_premuto $?

Tutte le interfacce hanno associato al pulsante* Exit* l’ exit-code 1… quindi quando a verifica_tasto_premuto viene passato 1, tutto i processi dell’ albero devono essere terminati…

killall processo_padre

già provato… ma non termina i processi indicati, nell’ albero che ho postato, come yad… termina i processi eseguiti dallo script in back-ground ma non le interfacce di YAD…

Forse è meglio se vi mostro bene come funziona lo script…

#!/bin/bash
function gui_1 {
	local x=`yad --text="Inserisci un numero" --entry`
	while  $x -ne 0 ]
	do
		echo "aaa bbb"
		x=$(($x-1))
	done
}

function fun_1 {	
	while read input
	do
		a1=`echo "$input" | awk '{print $1}'`
		a2=`echo "$input" | awk '{print $2}'`
		echo "$a1"
		echo "$a2"
	done
}

function gui_start {
	local choice=""
	choice=`yad --title="TITLE" --center --text="WELCOME insert text" --entry`
	verify_exit $?

	if  "$choice" == "a" ]; then
		gui="gui_1"
	else
		gui="gui_1"
	fi

	"$gui" | fun_1 | yad --title="lista" --center --list --column="prima":TEXT

	verify_exit $?
}

function verify_exit {
	local code=$1
	if  $code -eq 1 -o $code -eq 252 ]; then
		killall "my-search-tool-2"
	fi
}

gui_start

Provate a premere il pulsante “Cancel” sull’ interfaccia “lista” PRIMA di inserire il numero sull’ interfaccia intermedia… e vedrete il problema :wink:

nella situazione con processi figli attivi ci fai vedere:

#  ps -Al
$ ps -Al

0 S  1002  2743     1  0  80   0 - 28022 wait   ?        00:00:00 antolli-search
1 S  1002  2748  2743  0  80   0 - 28022 pipe_w ?        00:00:00 antolli-search
1 S  1002  2749  2743  0  80   0 - 28022 pipe_w ?        00:00:00 antolli-search

immagino siano queste le linee che ti interessano :wink: si ottiene la stessa cosa con lo script che ho postato sopra :wink:

ti posto anche l’ output di pstree nella situazione di sopra:

├─antolli-search-─┬─antolli-search-───yad───3*{yad}]
        │                 ├─antolli-search-
        │                 └─yad

vedere tutto, grazie.

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0     1     0  0  80   0 - 16547 epoll_ ?        00:00:01 systemd
1 S     0     2     0  0  80   0 -     0 kthrea ?        00:00:00 kthreadd
1 S     0     3     2  0  80   0 -     0 run_ks ?        00:00:00 ksoftirqd/0
1 S     0     5     2  0  60 -20 -     0 worker ?        00:00:00 kworker/0:0H
1 S     0     7     2  0  60 -20 -     0 worker ?        00:00:00 kworker/u:0H
1 S     0     8     2  0 -40   - -     0 cpu_st ?        00:00:00 migration/0
1 S     0     9     2  0 -40   - -     0 watchd ?        00:00:00 watchdog/0
1 S     0    10     2  0 -40   - -     0 cpu_st ?        00:00:01 migration/1
1 S     0    12     2  0  60 -20 -     0 worker ?        00:00:00 kworker/1:0H
1 S     0    13     2  0  80   0 -     0 run_ks ?        00:00:00 ksoftirqd/1
1 S     0    14     2  0 -40   - -     0 watchd ?        00:00:00 watchdog/1
1 S     0    15     2  0  60 -20 -     0 rescue ?        00:00:00 cpuset
1 S     0    16     2  0  60 -20 -     0 rescue ?        00:00:00 khelper
5 S     0    17     2  0  80   0 -     0 devtmp ?        00:00:00 kdevtmpfs
1 S     0    18     2  0  60 -20 -     0 rescue ?        00:00:00 netns
1 S     0    19     2  0  80   0 -     0 bdi_fo ?        00:00:00 bdi-default
1 S     0    20     2  0  60 -20 -     0 rescue ?        00:00:00 kintegrityd
1 S     0    21     2  0  60 -20 -     0 rescue ?        00:00:00 kblockd
1 S     0    22     2  0  60 -20 -     0 rescue ?        00:00:00 ata_sff
1 S     0    23     2  0  80   0 -     0 hub_th ?        00:00:00 khubd
1 S     0    24     2  0  60 -20 -     0 rescue ?        00:00:00 md
1 S     0    28     2  0  80   0 -     0 kswapd ?        00:00:00 kswapd0
1 S     0    29     2  0  85   5 -     0 ksm_sc ?        00:00:00 ksmd
1 S     0    30     2  0  99  19 -     0 khugep ?        00:00:00 khugepaged
1 S     0    31     2  0  80   0 -     0 fsnoti ?        00:00:00 fsnotify_mark
1 S     0    32     2  0  60 -20 -     0 rescue ?        00:00:00 crypto
1 S     0    40     2  0  60 -20 -     0 rescue ?        00:00:00 kthrotld
1 S     0    43     2  0  80   0 -     0 scsi_e ?        00:00:00 scsi_eh_0
1 S     0    44     2  0  80   0 -     0 scsi_e ?        00:00:00 scsi_eh_1
1 S     0    45     2  0  80   0 -     0 scsi_e ?        00:00:00 scsi_eh_2
1 S     0    46     2  0  80   0 -     0 scsi_e ?        00:00:00 scsi_eh_3
1 S     0    47     2  0  80   0 -     0 scsi_e ?        00:00:00 scsi_eh_4
1 S     0    48     2  0  80   0 -     0 scsi_e ?        00:00:00 scsi_eh_5
1 S     0    50     2  0  80   0 -     0 worker ?        00:00:00 kworker/u:3
1 S     0    51     2  0  80   0 -     0 worker ?        00:00:00 kworker/u:4
1 S     0    54     2  0  60 -20 -     0 rescue ?        00:00:00 kpsmoused
1 S     0    55     2  0  60 -20 -     0 rescue ?        00:00:00 deferwq
1 S     0    58     2  0  80   0 -     0 worker ?        00:00:02 kworker/0:2
1 S     0    60     2  0  60 -20 -     0 worker ?        00:00:00 kworker/0:1H
1 S     0   256     2  0  60 -20 -     0 worker ?        00:00:00 kworker/1:1H
1 S     0   288     2  0  80   0 -     0 kjourn ?        00:00:00 jbd2/sda1-8
1 S     0   289     2  0  60 -20 -     0 rescue ?        00:00:00 ext4-dio-unwri
4 S     0   317     1  0  80   0 -  7430 epoll_ ?        00:00:00 udevd
4 S     0   318     1  0  80   0 -  8037 epoll_ ?        00:00:00 systemd-journa
1 S     0   319     2  0  80   0 -     0 kaudit ?        00:00:00 kauditd
1 S     0   335     2  0  80   0 -     0 bdi_wr ?        00:00:00 flush-8:0
1 S     0   455     2  0  60 -20 -     0 rescue ?        00:00:00 led_workqueue
1 S     0   461     2  0  60 -20 -     0 rescue ?        00:00:00 cfg80211
1 S     0   495     2  0  60 -20 -     0 rescue ?        00:00:00 hd-audio0
1 S     0   529     2  0  60 -20 -     0 rescue ?        00:00:00 iwlwifi
1 S     0   571     2  0  80   0 -     0 kjourn ?        00:00:00 jbd2/sda3-8
1 S     0   572     2  0  60 -20 -     0 rescue ?        00:00:00 ext4-dio-unwri
4 S     0   593     1  0  80   0 -  4872 hrtime ?        00:00:00 smartd
4 S     0   595     1  0  80   0 - 29598 hrtime ?        00:00:00 crond
4 S     0   607     1  0  80   0 - 87509 poll_s ?        00:00:00 NetworkManager
0 S     0   608     1  0  80   0 - 35289 poll_s ?        00:00:00 abrtd
0 S     0   610     1  0  80   0 - 34761 inotif ?        00:00:00 abrt-watch-log
0 S     0   615     1  0  80   0 - 34761 inotif ?        00:00:00 abrt-watch-log
1 S     0   616     1  0  80   0 -  1072 poll_s ?        00:00:00 acpid
4 S     0   620     1  0  80   0 -  7069 epoll_ ?        00:00:00 systemd-logind
0 S     0   622     1  0  80   0 -  2891 inotif ?        00:00:00 system-setup-k
4 S    70   624     1  0  80   0 -  7000 poll_s ?        00:00:00 avahi-daemon
4 S     0   625     1  0  80   0 - 62882 poll_s ?        00:00:00 rsyslogd
4 S     0   640     1  0  80   0 -  5297 pause  ?        00:00:00 atd
4 S     0   643     1  0  80   0 - 26043 poll_s ?        00:00:00 lxdm-binary
4 S    81   649     1  0  80   0 -  7657 poll_s ?        00:00:00 dbus-daemon
4 S     0   654     1  0  80   0 -  1738 poll_s ?        00:00:00 mcelog
1 S    70   682   624  0  80   0 -  6970 unix_s ?        00:00:00 avahi-daemon
5 S     0   693   317  0  80   0 -  7428 epoll_ ?        00:00:00 udevd
5 S     0   694   317  0  80   0 -  7428 epoll_ ?        00:00:00 udevd
4 S     0   723     1  0  80   0 - 47910 poll_s ?        00:00:00 polkitd
4 S     0   728   643  4  80   0 - 38934 poll_s tty1     00:01:42 X
4 S     0   792     1  0  80   0 -  5263 poll_s ?        00:00:00 bluetoothd
4 S     0   793     1  0  80   0 - 20776 poll_s ?        00:00:00 modem-manager
4 S     0   810     1  0  80   0 - 12488 poll_s ?        00:00:00 wpa_supplicant
4 S     0   937     1  0  80   0 - 259175 poll_s ?       00:00:00 console-kit-da
4 S  1002  1002   643  0  80   0 - 23763 poll_s ?        00:00:00 lxsession
1 S  1002  1012     1  0  80   0 -  5054 poll_s ?        00:00:00 dbus-launch
1 S  1002  1013     1  0  80   0 -  7714 poll_s ?        00:00:00 dbus-daemon
0 S  1002  1078     1  0  80   0 - 79278 poll_s ?        00:00:00 imsettings-dae
0 S  1002  1081     1  0  80   0 - 39838 poll_s ?        00:00:00 gvfsd
0 S  1002  1169  1002  0  80   0 - 49786 poll_s ?        00:00:01 openbox
0 S  1002  1172  1002  0  80   0 - 154298 poll_s ?       00:00:05 lxpanel
0 S  1002  1173  1002  0  80   0 - 162602 poll_s ?       00:00:00 pcmanfm
0 S  1002  1174  1002  0  80   0 - 39419 poll_s ?        00:00:00 xscreensaver
4 S     0  1175   607  0  80   0 - 24095 poll_s ?        00:00:00 dhclient
0 S  1002  1181  1002  0  80   0 - 28022 wait   ?        00:00:00 status-monitor
1 S  1002  1183     1  0  80   0 - 128510 poll_s ?       00:00:00 xfce4-power-ma
0 S  1002  1186     1  0  80   0 - 10246 poll_s ?        00:00:00 xfconfd
0 S  1002  1188     1  0  80   0 - 61047 poll_s ?        00:00:00 gvfs-udisks2-v
0 S     0  1242     1  0  80   0 - 86263 poll_s ?        00:00:00 udisksd
0 S     0  1243     1  0  80   0 - 56050 poll_s ?        00:00:00 upowerd
0 S  1002  1252     1  0  80   0 - 104221 poll_s ?       00:00:05 parcellite
5 S    32  1273     1  0  80   0 -  4790 poll_s ?        00:00:00 rpcbind
0 S  1002  1282     1  0  80   0 - 54184 poll_s ?        00:00:00 abrt-applet
0 S  1002  1289     1  0  80   0 - 105240 poll_s ?       00:00:00 zeitgeist-data
1 S  1002  1372     1  0  69 -11 - 117174 poll_s ?       00:00:00 pulseaudio
4 S   172  1373     1  0  81   1 - 41140 poll_s ?        00:00:00 rtkit-daemon
0 S  1002  1381     1  0  80   0 - 87594 poll_s ?        00:00:00 zeitgeist-daem
0 S  1002  1385     1  0  80   0 - 222246 poll_s ?       00:00:00 nm-applet
0 S  1002  1400     1  0  80   0 - 56431 poll_s ?        00:00:00 seapplet
0 S  1002  1402     1  0  80   0 - 70516 poll_s ?        00:00:00 lxpolkit
0 S  1002  1407     1  0  80   0 - 53638 poll_s ?        00:00:00 zeitgeist-fts
0 S  1002  1420     1  0  80   0 - 36538 poll_s ?        00:00:00 gconfd-2
0 S  1002  1422     1  0  80   0 - 63907 poll_s ?        00:00:00 dconf-service
0 S  1002  1426  1407  0  80   0 - 26716 unix_s ?        00:00:00 cat
1 S  1002  1433     1  0  80   0 - 12976 poll_s ?        00:00:00 menu-cached
0 R  1002  1452  1169  0  80   0 - 125453 -     ?        00:00:02 lxterminal
0 S  1002  1453  1452  0  80   0 -  2103 unix_s ?        00:00:00 gnome-pty-help
0 S  1002  1454  1452  0  80   0 - 28813 n_tty_ pts/0    00:00:00 bash
0 S  1002  1529     1  3  80   0 - 291199 poll_s ?       00:01:02 firefox
0 S  1002  1550     1  0  80   0 - 253226 poll_s ?       00:00:08 thunderbird
0 S  1002  1583     1  0  80   0 - 66301 poll_s ?        00:00:00 at-spi-bus-lau
0 S  1002  1666  1529  1  80   0 - 106593 poll_s ?       00:00:30 plugin-contain
0 S  1002  1749     1  1  80   0 - 395225 poll_s ?       00:00:29 geany
0 S  1002  1750  1749  0  80   0 -  2103 unix_s ?        00:00:00 gnome-pty-help
0 S  1002  1751  1749  0  80   0 - 28813 n_tty_ pts/1    00:00:00 bash
1 S     0  2200     2  0  80   0 -     0 worker ?        00:00:00 kworker/1:3
1 S     0  2351     2  0  80   0 -     0 worker ?        00:00:00 kworker/1:0
1 S     0  2612     2  0  80   0 -     0 worker ?        00:00:00 kworker/0:0
1 S     0  2666     2  0  80   0 -     0 worker ?        00:00:00 kworker/u:0
1 S     0  2898     2  0  80   0 -     0 worker ?        00:00:00 kworker/0:1
0 S  1002  2996     1  0  80   0 - 28022 wait   ?        00:00:00 antolli-search
1 S  1002  3001  2996  0  80   0 - 28022 pipe_w ?        00:00:00 antolli-search
1 S  1002  3002  2996  0  80   0 - 28022 pipe_w ?        00:00:00 antolli-search
0 S  1002  3003  2996  0  80   0 - 104389 poll_s ?       00:00:00 yad
0 S  1002  3004  3001  0  80   0 - 199955 poll_s ?       00:00:00 yad
1 S     0  3086     2  0  80   0 -     0 worker ?        00:00:00 kworker/1:1
1 S     0  3087     2  0  80   0 -     0 worker ?        00:00:00 kworker/1:2
0 S  1002  3106  1181  0  80   0 - 26711 hrtime ?        00:00:00 sleep
0 S  1002  3108  1452  1  80   0 - 28813 wait   pts/2    00:00:00 bash
0 R  1002  3164  3108  0  80   0 - 28409 -      pts/2    00:00:00 ps

antolli-search viene lanciato all’avvio ?

no no… ho creato un file .desktop in /usr/share/applications per poterlo lanciare dal menu Accessori

e se dai anche un :

killall yad

niente…

se invece di lanciarlo dall’interfaccia grafica, lo lanci dal terminale ?

stesso risultato…

Scusate ragazzi ma non mi capacito di una cosa… ho semplificato ancora di più lo script in questo modo…

function gui_start {
	local choice=""
	choice=`yad --title="TITLE" --center --text="WELCOME insert text" --entry`
	verify_exit $?
}

function verify_exit {
	local code=$1
	if  $code -eq 1 -o $code -eq 252 ]; then
		kill_all_child $MY_PID 
                #MY_PID è una variabile globale inizializzata più giù
	fi
}

function kill_all_child {
	local parent=$1
	local childs=`ps -o pid --no-headers --ppid "$parent"`

echo "mypid: $parent"
echo "childs: $childs"

	for i in $childs 
	do
		kill_all_child $i
	done
	
	kill -kill $parent
}

MY_PID=$$
gui_start

#MY_PID viene inizializzato a $$, vale a dire il pid dello script. 
In questo modo la PRIMA volta che viene chiamata verify_exit viene passato come pid a kill_all_child 
che ricorsivamente termina i processi figli dello script

Il punto a mio parere assurdo è questo:
appena dopo aver lanciato lo script ed essermi apparsa la prima schermata, quella che richiede l’ inserimento del testo, ho dato da terminale:

$ps -Al
...
0 S  1002  3421  3419  0  80   0 - 28022 pipe_w pts/1    00:00:00 my-search-tool
0 S  1002  3422  3421  0  80   0 - 104358 poll_s pts/1   00:00:00 yad
...

la prima riga è il mio script, la seconda è il comando che crea la schermata di YAD, che come vedete risulta figlio di my-search-tool

Adesso premo il tasto di “cancel” sulla schermata e viene correttamente effettuata la chiamata a verify_exit, che a sua volta chiama kill_all_child

Qui arrivano le cose strane, come vedete ho messo degli echo in kill_all_child, per capire che succedeva… bene questo è l’ output visualizzato sul terminale:

mypid: 3421
childs:  3425

mypid: 3425
childs: 

./my-search-tool-2: line 64: kill: (3425) - Nessun processo corrisponde
./geany_run_script.sh: line 5:  3421 Ucciso                  "./my-search-tool-2"

Come vedete alla prima chiamata kill_all_child riceve in ingresso il pid dello script 3421 che è GIUSTO se confrontato con l’ output di ps -Al di sopra… adesso ci si aspetterebbe che in child ci sia 3422… e INVECE c’è 3425 !!! che non compare nemmeno nell’ elenco di ps -Al

Quindi mi chiedo perché questa linea di codice di kill_all_child restituisce 3425 ???

childs=`ps -o pid --no-headers --ppid "$parent"`

P.S.
In una simulazione identica a questa ho provato a dare, da un altro terminale

$ ps -o pid --no-headers --ppid 3411
3412

e infatti

$ ps -Al 0 S 1002 3411 3419 0 80 0 - 28022 pipe_w pts/1 00:00:00 my-search-tool 0 S 1002 3412 3411 0 80 0 - 104358 poll_s pts/1 00:00:00 yad