I’ve written a bunch of shell script functions that allow me to switch between these 3 apps which require exclusive access to a vt channel: raspbian, emulationstation(/RetroPie), and kodi. My intent is to use them in homebridge-commander so that I can switch between these apps using Siri.

I wrote a function for each system to stop that system, but I have 2 problems.

Problem 1

Each OS will stop, but if I've run Kodi at some point, I don’t see the console when I stop whichever one is currently running. All I see is a black screen. I can start up another app, no problem. I can use the connected keyboard (and type without seeing what I'm typing) or ssh and start up another OS, but stopping one of these apps still results in a black screen. I have functions to switch the app in one call, but I want to be able to simply stop whichever app is running and get to the console.

One manual fix I’ve learned is to type alt+F1. I also discovered that if I type sudo chvt 2;sudo chvt 1 I can get back to a console I can see. (Note, simply typing sudo chvt 1 does not work. The screen stays completely black.)

So I think VT1 is getting into a strange state and I figure it must have something to do with the way I’m killing Kodi using pkill.

How do I stop Kodi in a way that returns VT1 back to a working console screen?

Note, manually stopping these apps using the GUI works fine and I get back to a visible console, but I want to be able to control it via a command line call in homebridge-commander.

Problem 2

I discovered recently that the way I'm killing emulationstation (pkill emulationstatio) doesn't actually stop all its components. I had a game running and tried killing emulationstation, but the game remained on the screen. I couldn't play the game. It just seemed frozen. Also, game play was occasionally freezing, which I suspect was due to previously having killed emulationstation in this fashion and starting it back up.

So what's the best way to stop emulationstation (and any emulators it's running) from the command line?

  • what system is running after you stop the OS? .... what system do you SSH into? .... it is unclear why you expect to see something other than a black screen when the RPi is stopped
    – jsotola
    May 11, 2019 at 22:29
  • I’ve learned that referring to kodi, emulationstation, and raspbian as OSs (terminology I’m parroting from other forums somewhere) is incorrect. I don’t know what you call them other than apps that have exclusive use of the monitor. I expect to be kicked out to the console as if I had stopped the app. For example, you can Quit Emulationstation via the menus and you get kicked out to the console where you can start raspbian or kodi. You can kill raspbian with pkill xinit and you get kicked out to the console again. But after running kodi, the vt gets messed up.
    – hepcat72
    May 12, 2019 at 2:33
  • that makes sense now ..... Raspbian is a Linux OS, it is a version of Debian Linux .... emulationstation is a program that runs in Linux or in Windows ..... Kodi is also a program that runs in Linux and Windows ..... there are also special versions of Kodi that come with its own minimal OS .... one is LibreELEC
    – jsotola
    May 12, 2019 at 4:00
  • so all you need to find out is how to start/stop emulationstation and how to start/stop kodi using script and/or bash commands in Linux
    – jsotola
    May 12, 2019 at 4:04
  • Yes. I have that. The question is, how to do it in such a way that doesn’t leave the console’s connection to VT1 screwed up. I am using chvt 2:chvt 1, but I was just wondering if there’s a better way to quit kodi that doesn’t mess up the vt. Interestingly, I can start up emulationstation or raspbian after stopping kodi, but when I stop either one, the vt is messed up because at one point, kodi was running.
    – hepcat72
    May 12, 2019 at 4:15

1 Answer 1


A child process of kodi still has control of vt 1. Just doing pkill kodi orphans the child, but doesn't kill it. The screen goes black because kodi is effectively half-dead. Killing kodi and all its child processes will return you back to the console.

The situation for emulationstation is similar - killing all the children will solve the other issue. But let's use kodi as the example...

To get all the pids of kodi and its children, you can do something like this (in bash). First, create a function to get all child PIDs of a given PID:

getfamilypids() {
    local inpid="$1"
    local pidarray=()

    function getfamilypidshelper() {
        local pidin="$1"
        local CPIDS="$(pgrep -P $pidin)"
        for cpid in $CPIDS; do
            getfamilypidshelper $cpid

    getfamilypidshelper "$inpid"

    #reverse the array
    local rpidarray=()
    for ((z=${#pidarray[*]}-1; z>-1; z--)); do

    echo ${rpidarray[*]}

To get the PID of the kodi process you started, you can do this:

pgrep -f kodi

Then you can get the child processes of the PID you get back:

getfamilypids __PID_FROM_ABOVE__

Note, the order of the PIDs from getfamilypids is reversed. You should kill them sequentially from left to right. I wrote a method to automatically kill all of them and wait for them to finish cleanly before killing the next, but that's a topic for another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.