Pourquoi 3 processus et non 1 je veux Comprendre...:)

Résolu/Fermé
magouero Messages postés 242 Date d'inscription lundi 9 juillet 2007 Statut Membre Dernière intervention 2 avril 2022 - 24 mars 2022 à 20:18
mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 - 4 avril 2022 à 04:15
Bonjour,


j'aimerais comprendre pourquoi , quand je fais
ps -ef
, je vois ~3 fois le process lancé alors que j'en lance un et que , effectivement, un seul tourne, comme souhaité.
Si vous pouvez éclairer ma lanterne...

Dans la crontab j'appelle un fichier en faisant
bash testpresencebthdirect


celui-ci contient:
if [ `ps -ef | grep -c bthdirect.py` == "1" ];
then
    `nohup python3 bthdirect.py >/dev/null 2>&1`
    echo `date`," relancebthdirect">> errbthdirect.txt
fi


qui cherche un programme appelé "bthdirect.py" et s'il ne le trouve pas, en lance un.

ce qui suit le "echo" n'a pas d'importance.

Et donc la cde
ps -ef | grep test
me renvoie

pi         779   778  0 mars20 ?       00:00:00 /bin/sh -c bash testpresencebthdirect
pi         780   779  0 mars20 ?       00:00:00 bash testpresencebthdirect
pi         784   780  0 mars20 ?       00:00:00 bash testpresencebthdirect


La 1ere ok, c'est le cron qui bosse, bien,
La 2e ok, c'est mon process qui a été lancé, re-bien,
Mais la 3e ? Pourquoi ?

Merci d'avance.
contexte: Linux raspberrypi 5.10.63-v7+
A voir également:

2 réponses

mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 7 749
25 mars 2022 à 15:05
Bonjour,
  • Peux-tu reporter cette information à partir de la commande
    ps faux
    , cela pourrait aider à comprendre ?
  • Observes-tu le même comportement si tu commentes
    nohup
    ?
  • Pourquoi dans ton script
    testpresencebthdirect
    la ligne 3 est entre backquotes (ça ne sert à rien puisque tu n'injectes pas le résultat de cette commande dans une variable) ?


Ici, si on regarde ton code :
  • Le premier est l'appel fait par la
    crontab
    ;
  • Le second est le programme python effectué par la
    contrab
    ;
  • Le troisième est effectivement bizarre mais peut être dû à
    nohup
    .


Peux-tu également reporter ta
crontab
, juste au cas où ?

Note que pour faire ce que tu fais, il serait plus propre d'écrire un vrai démon en python, voir par exemple ce lien. Cela t'éviterait d'avoir à faire un appel à
nohup
et
ps
.

Bonne chance
0
magouero Messages postés 242 Date d'inscription lundi 9 juillet 2007 Statut Membre Dernière intervention 2 avril 2022 66
25 mars 2022 à 19:10
Bonjour,
ps faux =>
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       778  0.0  0.2  12188  2660 ?        S    mars20   0:00  \_ /usr/sbin/CRON -f
pi         779  0.0  0.0   1976   424 ?        Ss   mars20   0:00      \_ /bin/sh -c bash testpresencebthdirect
pi         780  0.0  0.2   7772  2724 ?        S    mars20   0:00          \_ bash testpresencebthdirect
pi         784  0.0  0.2   7772  1924 ?        S    mars20   0:00              \_ bash testpresencebthdirect
pi         785  0.2  2.6  33460 25088 ?        S    mars20  19:25                  \_ python3 bthdirect.py


Si je vire nohup le ps faux donne le même résultat.

Pourquoi backquotes ? J'avais un souci a un moment, et le moyen de lui faire comprendre que c'est une commande a été celui là.

La crontab -l => */2 * * * * bash testpresencebthdirect

Démon: pourquoi pas. Je regarde ce point.
Néanmoins j'aimerais bien comprendre car cela a l'air normal.
0
mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 7 749
28 mars 2022 à 15:23
Bonjour,

J'ai essayé de reproduire ce que tu as fait :

toto.sh

#!/bin/bash
if [ `ps -ef | grep -c toto.py` == "1" ];
then
    echo "toto.py"
    python3 toto.py >/dev/null 2>&1
fi


toto.py

#!/usr/bin/env python3
from time import sleep
while True:
    print("plop")
    sleep(5)


ps faux

mando       2964  0.0  0.0  10540  6176 pts/4    Ss   15:17   0:00              |       \_ /bin/bash
mando       3712  0.0  0.0   6868  3164 pts/4    S+   15:21   0:00              |           \_ bash toto.sh
mando       3716  0.8  0.1  16732  9676 pts/4    S+   15:21   0:00              |               \_ python3 toto.py


Comme tu le vois, dans mon cas tout apparaît normalement... Est-ce que si tu fais exactement ce que j'ai fait, tu observes le même comportement ?

Bonne chance
0
magouero Messages postés 242 Date d'inscription lundi 9 juillet 2007 Statut Membre Dernière intervention 2 avril 2022 66
2 avril 2022 à 11:42
Bonjour,
après création des 2 fichiers toto .sh et .py , quand je lance en faisant bash toto.sh
j'ai:
pi       25568  0.0  0.4  14452  4416 ?        S    10:36   0:00      \_ sshd: pi@pts/0
pi       25569  0.0  0.4   8760  3824 pts/0    Ss   10:36   0:00          \_ -bash
pi       25670  0.0  0.2   7772  2800 pts/0    S    10:40   0:00              \_ bash toto.sh
pi       25674  0.0  0.5  13820  5604 pts/0    S    10:40   0:00              |   \_ python3 toto.py
pi       25702  0.0  0.3  11112  2856 pts/0    R+   10:41   0:00              \_ ps faux



et quand je lance depuis crontab
root     26245  0.0  0.2  12188  2664 ?        S    10:57   0:00  \_ /usr/sbin/CRON -f
pi       26246  0.0  0.0   1976   416 ?        Ss   10:57   0:00      \_ /bin/sh -c bash toto.sh
pi       26247  0.0  0.2   7772  2696 ?        S    10:57   0:00          \_ bash toto.sh
pi       26251  0.0  0.6  13820  5756 ?        S    10:57   0:00              \_ python3 toto.py


Le pb disparait quand je vire les backquote autour de
`nohup python3 bthdirect.py >/dev/null 2>&1`

j'ai:
root     26676  0.0  0.2  12188  2660 ?        S    11:10   0:00  \_ /usr/sbin/CRON -f
pi       26678  0.0  0.0   1976   396 ?        Ss   11:10   0:00      \_ /bin/sh -c bash testpresencebthdirect
pi       26679  0.0  0.2   7772  2524 ?        S    11:10   0:00          \_ bash testpresencebthdirect
pi       26687  0.8  1.3  21136 12896 ?        S    11:10   0:00              \_ python3 bthdirect.py
0
magouero Messages postés 242 Date d'inscription lundi 9 juillet 2007 Statut Membre Dernière intervention 2 avril 2022 66 > magouero Messages postés 242 Date d'inscription lundi 9 juillet 2007 Statut Membre Dernière intervention 2 avril 2022
Modifié le 2 avril 2022 à 13:49
C'est parce que le fait d'utiliser des
` 
engendre la création d'un sous-shell. C'est de la substitution de commande "command substitution" comme $(gnagna)

Maintenant je sais.
1
mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 7 749 > magouero Messages postés 242 Date d'inscription lundi 9 juillet 2007 Statut Membre Dernière intervention 2 avril 2022
4 avril 2022 à 04:15
Merci pour l'information, tout s'explique :-)
0