0

I'm a brand new noob in python universe, so don't judge me too fast :-) I'm trying to force a python script to reload or restart at the beggining of a bash script.

I've tried :

pkill -f myscript.py

and

killall myscript.py

and others...

Actually, I would like to make run the same script that call .wav files after having changed those .wav files... If I don't reload the script or restart it, it keeps playing the old files.

Maybe, there is other solutions. Here is the script I want to reload (it's a button script playing music for my daughter)

#!/usr/bin/env python3

import pygame
from gpiozero import LED, Button
from signal import pause

pygame.init()

button_sounds = {Button(2): pygame.mixer.Sound("/home/pi/gpio-music-box/samples/1.wav"),
                 Button(3): pygame.mixer.Sound("/home/pi/gpio-music-box/samples/2.wav"),
                 Button(4): pygame.mixer.Sound("/home/pi/gpio-music-box/samples/3.wav"),
                 Button(17): pygame.mixer.Sound("/home/pi/gpio-music-box/samples/4.wav")}

for button, sound in button_sounds.items():
    button.when_pressed = sound.play

pause()

And here is my bash script :

#!/bin/bash

***HERE THE COMMAND I NEED !***

rm -r /home/pi/gpio-music-box/samples/*
cp -r /home/pi/gpio-music-box/comptines/* /home/pi/gpio-music-box/samples/

/home/pi/gpio-music-box/music.py

Thank you very much, and scuze my english, I'm french :-) Andy

Andy
  • 1
  • 1
  • Does this answer your question? [Pkill -f doesn't work for process killing](https://stackoverflow.com/questions/26902079/pkill-f-doesnt-work-for-process-killing) – Lety Nov 10 '20 at 12:01

2 Answers2

0

try this

#!/bin/bash

pid=$(ps auxwww | grep nameOfScript.py | grep -v grep | awk '{print $2}')

kill -9 $pid

rm -r /home/pi/gpio-music-box/samples/*
cp -r /home/pi/gpio-music-box/comptines/* /home/pi/gpio-music-box/samples/

nohup /home/pi/gpio-music-box/music.py &

Have a nice day

wagigi
  • 1
  • 1
  • What it does : `ps auxwww` -> print the process `grep nameOfScript.py` -> retrieve the lines we want `grep -v grep` -> remove grep from the selection `awk '{print $2}'` -> triming results to only show pid – wagigi Nov 10 '20 at 11:14
  • Thank wagigi, despite your command line works, it doesn't stop the processes... pid=$(ps auxwww | grep nameOfScript.py | grep -v grep | awk '{print $2}') echo $pid 4343 4364 4389 killall -9 4343 4364 4389 >> No process found Thx anyway ! – Andy Nov 10 '20 at 11:47
  • You need to use `kill` and not `killall` – wagigi Nov 10 '20 at 13:03
0

Firstly, you can reduce a lot of the "noise" from ps by using output formatting. You can then stop the need for using both grep and awk by using awk to do the searching also.

ps -eo "%p %a" | awk '/nameOfScript.py/ && $1 != PROCINFO["pid"] { print "kill -9 "$1 }'

This forces ps to only print the pid (%p) and the full command (%a). The output is then piped to awk where is searches for lines with the name of the script contained. It discounts any entries with the current process id of awk and then uses this to print a kill command with the relevant process id.

Once you have verified that the kill command displays as expected, you can use awk's system function to actually run the command through:

ps -eo "%p %a" | awk '/prometheous-things.py/ && $1 != PROCINFO["pid"] { system("kill -9 "$1) }'
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18