RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    Launching games from web over Nginx

    Scheduled Pinned Locked Moved Help and Support
    bashremote
    3 Posts 1 Posters 428 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      whocares17
      last edited by whocares17

      Hey, a million thanks in advance to anyone who can help out

      I've set up Nginx and PHP to act as a control panel for RetroPie (adjusting volume, various settings, keeping logs etc) and I'm trying to integrate a feature which would allow a user to start a game from this control panel (eg over their phone)

      Here's what I've done:

      • Given the user www-data permissions to execute commands as the user pi in visudo
      www-data    ALL=(pi) NOPASSWD:ALL
      
      • Run this command in the terminal (locally and over SSH):
      sudo -u pi DISPLAY=:0 /opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ snes '/home/pi/RetroPie/roms/snes/an-example-rom-obviously-not-the-real-one-but-lets-say-mario-or-something.sfc'
      
      • It worked perfectly both locally and over SSH (started the game on the Pi)
      • Placed the exact same command in a PHP file like this and hit it from a browser over HTTP:
      <?php
      $command = shell_exec("sudo -u pi DISPLAY=:0 /opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ snes '/home/pi/RetroPie/roms/snes/an-example-rom-obviously-not-the-real-one-but-lets-say-mario-or-something.sfc'");
      echo $command; // redundant but you never know
      ?>
      
      • The game does not start. The page takes a minute to load, htop reveals the process starts, and then just quits
      • Replaced that command with a different one:
      <?php
      $command = shell_exec("sudo -u pi espeak 'hello one two three' --stdout | aplay");
      echo $command; // cant help myself
      ?>
      
      • The command executed properly and the TTS was heard from the speaker connected to the Pi
      • (same results over SSH and locally through the terminal as well)

      That's where I've gotten myself, again a million thanks to anyone who might point me in the right direction

      Edit: To clarify, when I executed that command

      sudo -u pi DISPLAY=:0 /opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ snes '/home/pi/RetroPie/roms/snes/an-example-rom-obviously-not-the-real-one-but-lets-say-mario-or-something.sfc'
      

      in the terminal over SSH and locally, I did it only after:

      su - www-data -s /bin/bash
      

      So that it would be running as the same user as it would be when over PHP

      • I then SSH'd into the pi and ran the following:
      su - www-data -s /bin/bash
      php ./that-file-with-the-command-that-didnt-work-on-the-web-server.php
      

      (ran the php file over the terminal as the www-data user)

      • And it worked, loaded the game, so I'm pretty desperately confused
      W 2 Replies Last reply Reply Quote 0
      • W
        whocares17 @whocares17
        last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • W
          whocares17 @whocares17
          last edited by whocares17

          Solved it

          In case anyone else ever tries to do something like this and finds this post, the problem was this:
          Somewhere along the line, something required to start up the ROM expected an interactive terminal, which is why it worked over SSH and locally, and even worked with running the PHP script from the terminal, but Nginx/Apache/another web server won't provide any interactive terminal.

          It doesn't actually need any interactive terminal, so you can wrap the startup command inside a "script" command like this, to lie to it and trick it into believing it has an interactive terminal

          <?php
          $command = '/opt/retropie/supplementary/runcommand/runcommand.sh 0 _SYS_ snes "/home/pi/RetroPie/roms/snes/an-example-rom-obviously-not-the-real-one-but-lets-say-mario-or-something.sfc"';
          
          $result = shell_exec("sudo -u pi script --return --quiet -c '".$command."' /dev/null");
          ?>
          

          And that does the trick and starts the game up from a PHP web panel

          1 Reply Last reply Reply Quote 0
          • First post
            Last post

          Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

          Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.