4

I am trying to interface my homemade Raspberry Pi-powered arcade machine with a coin acceptor, and for that I:

  • use a shell script (named gameplay) which starts the game emulator (MAME) when a coin is inserted
  • (want to) kill the MAME process(s) when the time is up (decided by an Arduino interface connected to the RPi), and go back to the loop in the shell script to (sleep and) wait for coin again

all is well except that I can't seem to kill the MAME process.

enter image description here

Please see the photo for more details. The first pstree shows the processes when a game is being played. And the second pstree is when time is up, and the "emulationstation" process has been killed by the gameplay shell script (by sudo killall emulationstation).

But the "mame" process is still there and the game can still be played. I have also tried "sudo killall mame" but it doesn't work.

I've been trying many ways but none works so far.

UPDATE AFTER SOME OF YOUR KIND COMMENTS

I found that all the processes in the branch can be killed, except for the "mame" process, no matter what I tried. The best killall effort always results in "mame" becoming a sub-branch of "init" as: "init--mame---5*[{mame}]". Running "sudo killall mame" here gives nothing - no error, no effect. Why can't I just kill this process? (and this is a process of the "pi" user).

Then I tried a more violent kill, as @Colin Ameigh suggested below: "killall -9 mame" after the "killall emulationstation". And it ALMOST works, only that something was screwed up in the "gameplay" script - the terminal becomes blank, and the INSERT COIN... text is no longer there (but except for this, it works as intended, I also don't remember noticing any zombie processes). Here is the content of the gameplay script:

while true
do

clear
echo INSERT COIN...

while [[ $(cat /sys/class/gpio/gpio4/value) == '0' ]]; do
    sleep 1
    done

echo STARTING GAME...

#start game as separate process
emulationstation &

while [[ $(cat /sys/class/gpio/gpio4/value) == '1' ]]; do
    sleep 1
    done

sudo killall emulationstation
#sudo killall mame
sudo killall -9 mame

done
2

3 Answers 3

1

For a more general approach to killing processes...

This command should show the process

pgrep -f runcommand.sh

Then either cut and paste the process ID

kill PROCESSID

or, if you're a little braver using pipes

pgrep -f runcommand.sh | xargs -I{} kill {}

If you don't have pgrep (for some reason), then you can replace the pgrep command with this monstrosity:

ps aux | grep "[r]uncommand.sh" | sed 's/\s\+/ /g' | cut -f 2 -d ' '

The "[r]uncommand.sh" part just ensures that grep doesn't find its own process ID as well.

If the kill command does not end the process, then you may need to send a "SIGKILL" signal instead. To do that just add -9 to the kill command.

Given your specific use case - perhaps the shell script could have a SIGHUP handler that kills the mame process (since it should have the process ID) leaving the script running and the arduino trigger just has to send a SIGHUP to the script instead (via this mechanism, kill can send SIGHUP with -1), which can then return to a loop waiting for the coin signal.

1
  • I updated my question after your input, please have a look. Thanks!
    – Dave
    Jul 1, 2015 at 15:01
0

Try sudo killall --process-group emulationstation

1
  • Thanks, I tried but that results in the entire branch from "gameplay--emulationstation---..." is killed, but what is remained is "init--mame----5*[{mame}] and the game is still playing, while the gameplay process is also killed against the intention
    – Dave
    Jul 1, 2015 at 9:56
0

Give a try to

killall runcommand.sh

EDIT:

 |─lightdm─┬─Xorg───2*[{Xorg}]
 │         ├─lightdm─┬─init─┬─at-spi-bus-laun─┬─dbus-daemon
 │         │         │      │                 └─3*[{at-spi-bus-laun}]
 │         │         │      ├─at-spi2-registr───{at-spi2-registr}
 │         │         │      ├─bamfdaemon───3*[{bamfdaemon}]
 │         │         │      ├─dbus-daemon
 │         │         │      ├─dconf-service───2*[{dconf-service}]
 │         │         │      ├─evolution───11*[{evolution}]
 │         │         │      ├─evolution-calen───4*[{evolution-calen}]
 │         │         │      ├─evolution-sourc───2*[{evolution-sourc}]
 │         │         │      ├─firefox─┬─plugin-containe───8*[{plugin-containe}]
 │         │         │      │         └─64*[{firefox}]
 │         │         │      ├─gconfd-2
 │         │         │      ├─gnome-session─┬─compiz───7*[{compiz}]
 │         │         │      │               ├─deja-dup-monito───2*[{deja-dup-monito}]
 │         │         │      │               ├─evolution-alarm───4*[{evolution-alarm}]
 │         │         │      │               ├─hp-systray─┬─hp-systray───hp-systray
 │         │         │      │               │            └─3*[{hp-systray}]
 │         │         │      │               ├─nemo───3*[{nemo}]   <= See this line
 │         │         │      │               ├─nm-applet───2*[{nm-applet}]
 │         │         │      │               ├─polkit-gnome-au───2*[{polkit-gnome-au}]
 │         │         │      │               ├─python───2*[{python}]
 │         │         │      │               ├─telepathy-indic───2*[{telepathy-indic}]
 │         │         │      │               ├─unity-fallback-───2*[{unity-fallback-}]
 │         │         │      │               ├─update-notifier───3*[{update-notifier}]
 │         │         │      │               ├─zeitgeist-datah───6*[{zeitgeist-datah}]
 │         │         │      │               └─3*[{gnome-session}]
 │         │         │      ├─gnome-terminal─┬─bash───sudo───su───bash───pstree
 │         │         │      │                ├─gnome-pty-helpe
 │         │         │      │                └─3*[{gnome-terminal}]
 │         │         │      ├─gpg-agent

root@SHW:~ # killall nemo root@SHW:~ # pstree

─lightdm─┬─Xorg───2*[{Xorg}]
 │         ├─lightdm─┬─init─┬─at-spi-bus-laun─┬─dbus-daemon
 │         │         │      │                 └─3*[{at-spi-bus-laun}]
 │         │         │      ├─at-spi2-registr───{at-spi2-registr}
 │         │         │      ├─bamfdaemon───3*[{bamfdaemon}]
 │         │         │      ├─dbus-daemon
 │         │         │      ├─dconf-service───2*[{dconf-service}]
 │         │         │      ├─evolution───11*[{evolution}]
 │         │         │      ├─evolution-calen───4*[{evolution-calen}]
 │         │         │      ├─evolution-sourc───2*[{evolution-sourc}]
 │         │         │      ├─firefox─┬─plugin-containe───8*[{plugin-containe}]
 │         │         │      │         └─54*[{firefox}]
 │         │         │      ├─gconfd-2
 │         │         │      ├─gnome-session─┬─compiz───7*[{compiz}]
 │         │         │      │               ├─deja-dup-monito───2*[{deja-dup-monito}]
 │         │         │      │               ├─evolution-alarm───4*[{evolution-alarm}]
 │         │         │      │               ├─hp-systray─┬─hp-systray───hp-systray
 │         │         │      │               │            └─3*[{hp-systray}]
 │         │         │      │               ├─nm-applet───2*[{nm-applet}]
 │         │         │      │               ├─polkit-gnome-au───2*[{polkit-gnome-au}]
 │         │         │      │               ├─python───2*[{python}]
 │         │         │      │               ├─telepathy-indic───2*[{telepathy-indic}]
 │         │         │      │               ├─unity-fallback-───2*[{unity-fallback-}]
 │         │         │      │               ├─update-notifier───3*[{update-notifier}]
 │         │         │      │               ├─zeitgeist-datah───6*[{zeitgeist-datah}]
 │         │         │      │               └─3*[{gnome-session}]
 │         │         │      ├─gnome-terminal─┬─bash───sudo───su───bash───pstree
 │         │         │      │                ├─gnome-pty-helpe
 │         │         │      │                └─3*[{gnome-terminal}]
 │         │         │      ├─gpg-agent

After I execute the command, all the instances of nemo get killed. Though we can send the signal. But I prefer killall. Hence suggested above command.

EDIT 2:

while true
do

clear
echo INSERT COIN...

while [[ $(cat /sys/class/gpio/gpio4/value) == '0' ]]; do
    sleep 1
    done

echo STARTING GAME...

#start game as separate process
emulationstation & emuPID=$!   <= This

while [[ $(cat /sys/class/gpio/gpio4/value) == '1' ]]; do
    sleep 1
    done

sudo killall $emuPID           <= and this
#sudo killall mame
sudo killall -9 mame

done

In the above script, saving the PID of script into a variable and killing the process with the help of it's PID. Give it a try

6
  • will this kill the sub processes of the command, a little explanation would improve your answer, can a signal also be specified ?
    – X Tian
    Jul 1, 2015 at 10:52
  • @SHW: thanks for taking the time to do the experiment. I tried killall runcommand.sh but doesn't work. With the first pstree in my original photo, I did "sudo killall runcommand.sh" and the game still playing, and the branch became: "init--login--bash--gameplay---emulationstatio---emulationstatio---5*[{emulationstatio}]" and what is left from the killall, resulted in a separate branch: "init--mame---5*[{mame}]". I even tried "sudo killall mame" afterwards, bash accepted the command but nothing happended, mame is still there
    – Dave
    Jul 1, 2015 at 14:19
  • @SHW: I updated my question after your input, please have a look. Thanks!
    – Dave
    Jul 1, 2015 at 15:00
  • @SHW.Thanks, but funny this is getting very confusing. I tried your exact modification of script-> mame is killed cuz of the existing sudo killall -9 mame, but emulationstation is still there, in the same branch, sudo killall $emuPID does nothin. Then in the last 3 lines of the script I removed all those killalls and do "sudo kill $emuPID" (note not killall) then some funny thing happened. the mame process is killed (yes, mame), emulationstation still there, and the rest of the branch was split into 2 branches: init--login--bash--gameplay--sleep AND init--emulationstatio--4*[{emulationstatio}]
    – Dave
    Jul 2, 2015 at 10:53
  • @SHW. With those 2 split up branches, I did a "sudo killall emulationstation" and the emulationstation process was gone, just as I want. Only thing is that it seems that the gameplay process is not provoked? because the terminal is blank without the INSERT COIN... text, though inserting a new coin will start emulationstation
    – Dave
    Jul 2, 2015 at 10:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .