Author Topic: Getting fe.plugin_command_bg to work  (Read 2729 times)

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Getting fe.plugin_command_bg to work
« on: October 25, 2018, 01:39:39 AM »
When exiting a game on my Raspberry Pi3B and going back to Attract Mode, I'd like to execute a bash script.  I can get the script to work using the `system` command in the plugin.  However, I'd like the bash script to operate in the background because it causes a 5 to 10 second delay to returning to Attract Mode.

For the life of me, though, I can't get fe.plugin_command_bg to work with any executable file.  I'm on the latest 4.2.1 version of Attract Mode.  I'd be grateful for any help please.  Thanks.  The plugin is below:

Code: [Select]
// This plugin regenerates the "Most_Played_Games" romlists when a game is played
fe.add_transition_callback( "most_played_game" );
function most_played_game( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.FromGame:
                fe.plugin_command_bg( "sudo /home/pi/.attract/MPG.sh", "" ); // Starts the Most Played Games bash script to update the Most_Played_Games_By_Play_Count and Most_Played_Games_By_Play_Time romlists whenever a game is played
                break;
                }
                return false;
}

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Getting fe.plugin_command_bg to work
« Reply #1 on: October 25, 2018, 02:22:49 AM »
I’m not a rpi or Linux pro. My guess would be that you need to launch the terminal and run what you already have as arguments passed to the terminal.

spud1

  • Full Member
  • ***
  • Posts: 40
    • View Profile
Re: Getting fe.plugin_command_bg to work
« Reply #2 on: October 25, 2018, 03:31:37 PM »
Thanks, keilmillerjr.

In the end, I couldn't get fe.plugin_command_bg to work, but I did find a workaround using the system command and 2 bash scripts.  By calling one bash script (which had a "&" at the end) with another, the result was that there was no delay to returning to Attract Mode after exiting a game.  If anyone is interested, here is the code:

The plugin:

Code: [Select]
// This plugin regenerates the "Most_Played_Games" romlists when a game is played
fe.add_transition_callback( "most_played_game" );
function most_played_game( ttype, var, ttime )
{
 switch ( ttype )
 {
  case Transition.FromGame:
system( "sudo /home/pi/.attract/NOHUP1.sh" ); // Starts the Most Played Games bash script to update the Most_Played_Games_By_Play_Count and Most_Played_Games_By_Play_Time romlists whenever a game is played
        return false;
}}

The bash scripts - NOHUP1.sh:

Code: [Select]
#!/bin/bash
sudo nohup /home/pi/.attract/MPG.sh &

MPG.sh:
Code: [Select]
#!/bin/bash
sudo cp /home/pi/.attract/romlists/Masterlist.txt /home/pi/.attract/stats/ && # copies the Masterlist.txt romlist (which has a complete record of all games on my arcade machine) into the stats folder
sudo find /home/pi/.attract/stats/ -type f -name "*.stat" -size 0 -print0 | xargs -0 rm # removes all 0 byte files from the stats folder
sudo find /home/pi/.attract/stats/ -type f -name "*.stat" -print -exec cat {} \; > /home/pi/.attract/stats/files.txt && # finds all game .stat files (statistic files containing the "played count" and the "played time" information) and inserts the full path of the stat files and their respective contents into files.txt
sudo awk 'FNR%3' < /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes every 3rd line ie the "played time" line
sudo paste -d ' ' - - < /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes the carriage return at the end of every 2nd line so that the "played count" is on the same line as the game name
sudo awk '{l=$NF;$NF=NF-1;print l,$0}' /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # moves the "played count" to the start of the line but unfortunately leaves numbers at the end of each line too
sudo sed s/'\w*$'// /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes the unnecessary numbers at the end of each line
sudo sort -r -n /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # sorts the games in order of "play count" with the game with the highest play count being at the top of the list
sudo awk '!/Mode/' /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes any "Attract Mode" script entries
sudo sed 's/......$//' /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes the .stat extension from the game names
sudo awk '{print $NF}' FS=/ /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # deletes everything before the last forward slash ie removes the path to each file leaving only the game names
sudo awk -F\; 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1]}' /home/pi/.attract/stats/Masterlist.txt /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/Most_Played_Games_By_Play_Count.txt # produces the Most_Played_Games_By_Play_Count romlist in order of play count
# sudo rm /home/pi/.attract/stats/files.txt && rm /home/pi/.attract/stats/Masterlist.txt && # removes all temporary files from stats folder
sudo rm /home/pi/.attract/stats/files.txt && # removes all temporary files from stats folder
sudo awk '!x[$0]++' /home/pi/.attract/stats/Most_Played_Games_By_Play_Count.txt > /home/pi/.attract/stats/Most_Played_Games_By_Play_Count1.txt && rm /home/pi/.attract/stats/Most_Played_Games_By_Play_Count.txt && mv /home/pi/.attract/stats/Most_Played_Games_By_Play_Count1.txt /home/pi/.attract/stats/Most_Played_Games_By_Play_Count.txt # removes duplicate entries in Most_Played_Games_By_Play_Count.txt romlist without sorting
sudo mv /home/pi/.attract/stats/Most_Played_Games_By_Play_Count.txt /home/pi/.attract/romlists/Most_Played_Games_By_Play_Count.txt # moves the new Most_Played_Games_By_Play_Count.txt romlist to the romlists folder
sudo find /home/pi/.attract/stats/ -type f -name "*.stat" -size 0 -print0 | xargs -0 rm # removes all 0 byte files from the stats folder
sudo find /home/pi/.attract/stats/ -type f -name "*.stat" -print -exec cat {} \; > /home/pi/.attract/stats/files.txt && # finds all game .stat files (statistic files containing the "played count" and the "played time" information) and inserts the full path of the stat files and their respective contents into files.txt
# sudo sed -n 2p /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files2.txt # Takes the second line and inserts it into files2.txt
sudo sed -n 3p /home/pi/.attract/stats/files.txt >> /home/pi/.attract/stats/files2.txt # Takes the third line and inserts it into files2.txt for temporary storage
sudo awk 'NR!=2 && NR!=3 { print }' /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt  && rm -f  /home/pi/.attract/stats/files.txt && mv -f /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt # removes the second and third lines of files.txt
sudo awk 'FNR%3' < /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes every 3rd line ie the "play count" line, leaving "play time"
sudo sed -n 1p /home/pi/.attract/stats/files2.txt > patch
sudo sed -i 1rpatch /home/pi/.attract/stats/files.txt     # The above 2 lines taken together insert the first line of files2.txt as the second line of files.txt
sudo paste -d ' ' - - < /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes the carriage return at the end of every 2nd line so that the "played time" is on the same line as the game name
sudo awk '{l=$NF;$NF=NF-1;print l,$0}' /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # moves the "played time" to the start of the line but unfortunately leaves numbers at the end of each line too
sudo sed s/'\w*$'// /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes the unnecessary numbers at the end of each line
sudo sort -r -n /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # sorts the games in order of "played time" with the game with the highest play time being at the top of the list
sudo awk '!/Mode/' /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes any "Attract Mode" script entries
sudo sed 's/......$//' /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # removes the .stat extension from the game names
sudo awk '{print $NF}' FS=/ /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/files1.txt && rm /home/pi/.attract/stats/files.txt && mv /home/pi/.attract/stats/files1.txt /home/pi/.attract/stats/files.txt && # deletes everything before the last forward slash ie removes the path to each file leaving only the game names
sudo awk -F\; 'NR==FNR{a[$1]=$0;next}$1 in a{print a[$1]}' /home/pi/.attract/stats/Masterlist.txt /home/pi/.attract/stats/files.txt > /home/pi/.attract/stats/Most_Played_Games_By_Play_Time.txt # produces the Most_Played_Games_By_Play_Time romlist in order of play time
sudo rm /home/pi/.attract/stats/patch && sudo rm /home/pi/.attract/stats/files1.txt && sudo rm /home/pi/.attract/stats/files.txt && sudo rm /home/pi/.attract/stats/files2.txt && rm /home/pi/.attract/stats/Masterlist.txt && # removes all temporary files from stats folder
sudo awk '!x[$0]++' /home/pi/.attract/stats/Most_Played_Games_By_Play_Time.txt > /home/pi/.attract/stats/Most_Played_Games_By_Play_Time1.txt && rm /home/pi/.attract/stats/Most_Played_Games_By_Play_Time.txt && mv /home/pi/.attract/stats/Most_Played_Games_By_Play_Time1.txt /home/pi/.attract/stats/Most_Played_Games_By_Play_Time.txt # removes duplicate entries in Most_Played_Games_By_Play_Time.txt romlist without sorting
sudo mv /home/pi/.attract/stats/Most_Played_Games_By_Play_Time.txt /home/pi/.attract/romlists/Most_Played_Games_By_Play_Time.txt # moves the new Most_Played_Games_By_Play_Time.txt romlist to the romlists folder