Commande python pour pilotage machine linux
Bonjour,
Je cherche à comprendre pourquoi je n'arrive pas à lancer une commande python depuis un serveur nodejs. Je précise le schéma :
J'ai un serveur nodejs qui propose une petite interface avec des boutons qui permettent d'activer des commandes python.
Les commandes python ont toutes été testées manuellement et elles fonctionnent toutes. Ces commandes sont :
Code:
1 2
| os.system('sshpass -p\'gc\' ssh root@10.0.20.11 "reboot -f" &')
os.system('sshpass -p\'gc\' ssh root@10.2.10.11 "poweroff now" &') |
Les codes sont tous bien paramétrés en terme de droits ( chmod +x code.py )
Le code nodejs semble bien paramétré car certains code python s'exécutent bien. Voici un extrait du code nodejs :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| if(name=="reboot"){
if (state_reboot ==0 & state_stop ==0 & state_start ==0 & state_cleanup ==0){
spawn('python', ['/home/radxa/server_alert/public/reboot.py']); //ici on lance le script reboot
console.log("reboot : commande lancée")
state_reboot=1;
setTimeout(()=>{
state_reboot=0
io.emit('etatreboot',state_reboot);
}, 25000)
}
io.emit('etatreboot',state_reboot);
} |
Par ailleurs lorsque j'appuie sur le bouton (donc depuis nodejs), et en parallèle j'ai le htop qui tourne, je vois bien mon script qui apparaît dans la liste des taches, par contre les commandes contenues dans ce script os.system('sshpass ... ne se lancent pas --> pas visibles dans le htop, et la je ne comprends pas pourquoi.
Je ne sais pas si j'ai été assez explicite.
Merci pour votre aide.
commande python pour pilotage machine linux
N'y aurait-t'il un moyen de définir sous quel utilisateur se lance l'application nodejs ? a partir de systemctl ? je ne sais pas.
Merci.
Commande python pour pilotage machine linux
bonjour,
en recherchant, j'ai trouvé run truc intéressant : les cibles par défaut.
Mes différentes applications (services) sont lancées automatiquement au démarrage de la carte par systemctl. Il s'avère que quand je tape la commande : systemctl get-default cela me retourne "graphical.target" .
Est-ce que cela peut impacter l'exécution du service nodejs ?
J'ai donc redéfini la cible en tapant : sudo systemctel set-default multi-user.target
On verra ce que cela donne.
Si vous avez des idées, remarques, je suis preneur.
Merci.
commande python pour pilotage machine linux
Bonjour,
Merci pour la réponse, je suppose effectivement que c'est un problème de droit .. mais je me pose aussi une autre question :
Lorsqu'une carte (raspberry ou autre) démarre, qu'un serveur se lance automatiquement, puis qu'un utilisateur se connecte sur une page du serveur depuis un PC distant (pour exécuter des codes pythons), sous quel utilisateur sont exécutés les codes python ?
-> dans ce cas, aucun utilisateur n'est connecté directement sur la carte
-> existe-t'il un utilisateur par défaut dans ce cas ?
-> est-ce que le PC distant devient l'utilisateur ?
J'avoue ne vraiment pas connaitre cette gestion d'utilisateur, si vous avez des explications je suis preneur.
Merci beaucoup.
commande python pour pilotage machine linux
Merci pour ce retour.
Je viens de lancer "journactl" sur une de mes cartes. Ce que cela retourne :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Queued start job for default target Main User Target.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Created slice User Application Slice.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Reached target Paths.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Reached target Timers.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Starting D-Bus User Message Bus Socket.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Listening on GnuPG network certificate management daemon.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Listening on GnuPG cryptographic agent and passphrase cache (access for web browsers).
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Listening on GnuPG cryptographic agent and passphrase cache (restricted).
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Listening on GnuPG cryptographic agent (ssh-agent emulation).
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Listening on GnuPG cryptographic agent and passphrase cache.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Listening on debconf communication socket.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Listening on Sound System.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Listening on D-Bus User Message Bus Socket.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Reached target Sockets.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Reached target Basic System.
Jun 26 14:07:01 rock-pi-4b systemd[70228]: Starting Sound Service...
Jun 26 14:07:02 rock-pi-4b systemd[70228]: Started D-Bus User Message Bus.
Jun 26 14:07:02 rock-pi-4b systemd[70228]: Started Sound Service.
Jun 26 14:07:02 rock-pi-4b systemd[70228]: Reached target Main User Target.
Jun 26 14:07:02 rock-pi-4b systemd[70228]: Startup finished in 939ms. |
on observe que l'utilisateur est l'utilisateur par défaut : "default target Main User Target"
or je n'ai pas clairement identifié de user pour mon service "nodejs.service" lancé par systemctl au lancement de la carte. Voir la déclaration du service :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
[Unit]
Description=nodejs
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
ExecStart= /home/radxa/server_alert/autostart_indexjs.sh
Restart=always
[Install]
WantedBy=multi-user.target |
Par ailleurs on y retrouve dans le répertoire /etc/systemd/system les services :
bluetooth.target.wants dbus-org.freedesktop.timesync1.service graphical.target.wants prometheus.service sshd.service
dbus-fi.w1.wpa_supplicant1.service default.target.wants multi-user.target.wants pushgateway.service sysinit.target.wants
dbus-org.bluez.service display-manager.service network-online.target.wants rkisp_3A.service timers.target.wants
dbus-org.freedesktop.nm-dispatcher.service getty.target.wants pm2-radxa.service samba-ad-dc.service
ne faudrait-il pas remplacer : " WantedBy=multi-user.target " présent dans le nodejs.service par " WantedBy=default.target.wants " ??
Merci.
commande python pour pilotage machine linux
En executant la commande journalctl -u nodejs on observe :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
-- Boot b80dfeb894e94c2ea912d75fcd9b32bb --
juin 23 16:37:09 raspberrypi systemd[1]: Started nodejs.
juin 23 16:37:13 raspberrypi autostart_indexjs.sh[493]: Alert Server Connected to port 5000
juin 23 16:38:00 raspberrypi autostart_indexjs.sh[493]: Connecté au client $(socket.id)
juin 23 16:38:02 raspberrypi autostart_indexjs.sh[493]: déconnecté
juin 23 16:38:13 raspberrypi systemd[1]: Stopping nodejs...
juin 23 16:38:13 raspberrypi systemd[1]: nodejs.service: Succeeded.
juin 23 16:38:13 raspberrypi systemd[1]: Stopped nodejs.
juin 23 16:38:13 raspberrypi systemd[1]: nodejs.service: Consumed 1.307s CPU time.
-- Boot 527ccba8a4104f8aa29b28c61a09b978 --
juin 23 16:38:18 raspberrypi systemd[1]: Started nodejs.
juin 23 16:38:22 raspberrypi autostart_indexjs.sh[495]: Alert Server Connected to port 5000
juin 23 16:54:15 raspberrypi autostart_indexjs.sh[495]: Connecté au client $(socket.id)
juin 23 16:56:50 raspberrypi autostart_indexjs.sh[495]: Connecté au client $(socket.id)
juin 23 16:57:05 raspberrypi autostart_indexjs.sh[495]: déconnecté
juin 23 17:10:46 raspberrypi autostart_indexjs.sh[495]: déconnecté
juin 23 17:10:58 raspberrypi systemd[1]: Stopping nodejs...
juin 23 17:10:58 raspberrypi systemd[1]: nodejs.service: Succeeded.
juin 23 17:10:58 raspberrypi systemd[1]: Stopped nodejs.
juin 23 17:10:58 raspberrypi systemd[1]: nodejs.service: Consumed 1.468s CPU time.
-- Boot 67a1fbe45f4d4b98836d4296601c2d81 --
juin 23 17:11:03 raspberrypi systemd[1]: Started nodejs.
juin 23 17:11:07 raspberrypi autostart_indexjs.sh[490]: Alert Server Connected to port 5000
juin 23 17:52:51 raspberrypi autostart_indexjs.sh[490]: Connecté au client $(socket.id)
juin 23 17:53:24 raspberrypi autostart_indexjs.sh[490]: reboot : commande lancée
juin 23 17:57:42 raspberrypi autostart_indexjs.sh[490]: déconnecté
lines 147-204/204 (END) |
Je ne sais pas trop comment interpreter ça, mais ce qui est certain c'est que nodejs est lancé par un script bash (autostart_indexjs.sh) accessible dans le répertoire de l'utilisateur pi : /home/pi/server_alert/autostart_indexjs.sh.
Effectivement lorsque je lance les scripts en manuel, en lançant directement le nodejs comme ça : node index.js alors les scripts python s'executent bien. Mais pas lorsque ça se lance via autostart_indexjs.sh.
le script bash fait seulement :
Code:
1 2 3
|
cd /home/pi/server_alert
node index.js |
y a t'il moyen de specifier un utilisateur (root par exemple) pour le lancement de index.js depuis le script bash ?
Merci pour l'aide.
commande python pour pilotage machine linux
Merci pour l'aide,
ça ne fonctionne toujours pas. Je vais tenter par un autre moyen, je posterai un message plus tard si j'y arrive.