Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
scripting_services_rules_examples [2024/10/14 01:15] – [Usage] Tone, spelling, formatting, factual corrections in second paragraph maximumentropyscripting_services_rules_examples [2025/06/07 06:59] (current) – Scripts and Wikicode crcerror
Line 7: Line 7:
   - The **user services** can be created and modified by the user in ''/userdata/system/services/''   - The **user services** can be created and modified by the user in ''/userdata/system/services/''
  
-Each of the services may be enabled or disabled in the EmulationStation frontend: **MAIN MENU** -> **SYSTEM SETTINGS** -> **SERVICES** +====== Usage ======
- +
-==== Usage ====+
  
 How to write a proper user service script: How to write a proper user service script:
Line 20: Line 18:
   - Store your script in ''/userdata/system/services/''   - Store your script in ''/userdata/system/services/''
   - Reboot your device to make the services selectable in EmulationStation.   - Reboot your device to make the services selectable in EmulationStation.
 +
 +Each of the services may be enabled or disabled in the EmulationStation frontend: **MAIN MENU** -> **SYSTEM SETTINGS** -> **SERVICES**.  Toggling the service state in this menu will also immediately start or stop the service, in addition to controlling whether it will start at next system boot.
  
 **Disabling** services (with ''batocera-services disable'') doesn't **stop** them. That needs to be done manually with ''batocera-services stop''. Disabling them only means that they will not be automatically started after the next system startup. **Disabling** services (with ''batocera-services disable'') doesn't **stop** them. That needs to be done manually with ''batocera-services stop''. Disabling them only means that they will not be automatically started after the next system startup.
Line 25: Line 25:
 **Enabling** them (with ''batocera-services enable'') works the same. To actually **start** a service, enable it first, then restart Batocera or use ''batocera-services start''. **Enabling** them (with ''batocera-services enable'') works the same. To actually **start** a service, enable it first, then restart Batocera or use ''batocera-services start''.
  
- +===== Filename conventions =====
- +
- +
-==== Filename conventions ====+
  
 For filenames there are some specfic rules! This is caused as every service-file is exported as systemvariable. So only charaters from A-Z (upper- and lowercase, mixed), the underscore and digits (Please avoid as first character) are allowed. For filenames there are some specfic rules! This is caused as every service-file is exported as systemvariable. So only charaters from A-Z (upper- and lowercase, mixed), the underscore and digits (Please avoid as first character) are allowed.
Line 54: Line 51:
 </code> </code>
  
-==== Conditions ====+===== Conditions =====
  
 All these scripts are initiated through ''/etc/init.d/S99userservices'' so there is a **start** and a **stop** condition that can be used inside the scripts. S99 will wait for all scripts to be finished in stop condition, so be aware of your scripts using sleep timers and infinite do-while loops. All these scripts are initiated through ''/etc/init.d/S99userservices'' so there is a **start** and a **stop** condition that can be used inside the scripts. S99 will wait for all scripts to be finished in stop condition, so be aware of your scripts using sleep timers and infinite do-while loops.
  
- ==== Script Examples ====+ ===== Script Examples ====
 + 
 +**Just some examples to show how powerful these kind of scripts can be.** 
 + 
 +This script example is able to spawn "sub-services", these spawned scripts have just one single task: Set a single value to ''batocera.conf''. The script is furthermore able to detect a setted and active value and to show this in your ES-Service-Section. The usage is simple. Place the script in ''/userdata/system/services/'' and start the service. Then leave the menu and select the Service-Section again. You'll now see that there are 2 new services are in place: ''PI_RETROFLAG'' and ''PI_RETROFLAG_ADV''. If you select one of these, then the value **RETROFLAG** or **RETROFLAG_ADV** will be written to Batoceras config file. If you activate all the spawned services then the value is used. 
 + 
 + 
 +By **stopping** the mainservice, all sub-services will be removed from system. I used the ''system.power.switch'' value here that is reasonable for all Raspberry Pie platforms. By **starting** the mainservice again, all sub-services will be created again. Usually a system reboot is needed to make the powerswitches work. 
 + 
 +<code bash| PI_POWERSWITCHES> 
 +#!/bin/bash 
 +# Spawn Service Script by cyperghost (second life) 
 +# This is a crude example, but shows the ability of the new services 
 + 
 +switch="RETROFLAG RETROFLAG_ADV" #Add here more values 
 +path="/userdata/system/services" 
 + 
 +# First argument for condition: start=service switched on 
 +case ${1} in 
 +start) 
 +    # Create single scripts per echo command and place them to /services 
 +    # The test start inside the sub-services is needed to set the system.power.switch parameter only if service is ticked 
 +    for i in ${switch}; 
 +    do 
 +        echo "[ \${1} == start ] && batocera-settings-set system.power.switch ${i}" > ${path}/PI_${i} 
 +    done 
 +     
 +    # Check and set indicator to on if a proper value is found in batocera.conf, if there is no value then indicator is setted to off 
 +    active_service=$(batocera-settings-get system.power.switch) 
 +    if [ -n ${active_service} ]; 
 +    then 
 +        batocera-services enable PI_${active_service} 
 +    fi 
 +;; 
 + 
 +stop) 
 +    #Remove all entries from filesystem and in batocera.conf (done through batocera-services) 
 +    for i in ${switch}; 
 +    do 
 +        rm -f ${path}/PI_${i} 
 +        batocera-services disable PI_${i} 
 +    done 
 +;; 
 +esac 
 +</code> 
 + 
 +---- 
  
 This script will check for proper filenames and automatically alter them and even make backups. The idea was born by an interesting thread on github, where a user mentioned that downloaded scripts from this wiki got Windows linebreaks. This was just caused by browser download. The question is: **How to fix Windows linebreaks without manually Code editing?** \\ This script will check for proper filenames and automatically alter them and even make backups. The idea was born by an interesting thread on github, where a user mentioned that downloaded scripts from this wiki got Windows linebreaks. This was just caused by browser download. The question is: **How to fix Windows linebreaks without manually Code editing?** \\
Line 69: Line 113:
  
 #only on start condition #only on start condition
-[[ $1 == stop ]] && exit 0+[[ $1 == start ]] || exit 0
  
 # Sanatize Windows-CRLF to unix-style # Sanatize Windows-CRLF to unix-style
Line 76: Line 120:
 pushd /userdata/system/services > /dev/null pushd /userdata/system/services > /dev/null
  
-find -type f -printf '%f\n' | 
 while read USER_SERVICE while read USER_SERVICE
 do do
 +    grep -rlq $'\r' "${USER_SERVICE}" && dos2unix -k -q "${USER_SERVICE}"
 +
     SANATIZE="${USER_SERVICE//[^0-9A-Za-z_]/_}"     SANATIZE="${USER_SERVICE//[^0-9A-Za-z_]/_}"
     SANATIZE="$(echo "$SANATIZE" | sed 's/^[[:digit:]]*//')"     SANATIZE="$(echo "$SANATIZE" | sed 's/^[[:digit:]]*//')"
Line 84: Line 129:
     then     then
         mv -b --suffix=_bak "${USER_SERVICE}" "${SANATIZE}"         mv -b --suffix=_bak "${USER_SERVICE}" "${SANATIZE}"
-        USER_SERVICE="-back2life-"  
     fi     fi
  
-    [[ "${USER_SERVICE}" == "-back2life-" ]] && USER_SERVICE="${SANATIZE}" +done < <(find -type f -printf '%f\n')
-    grep -rlq $'\r"${USER_SERVICE}" && dos2unix -k -q "${USER_SERVICE}" +
- +
-done+
  
 popd > /dev/null popd > /dev/null
Line 101: Line 142:
   - Setted a bash envirionment with proper seperators   - Setted a bash envirionment with proper seperators
  
-Usually the command for the first codeline (line 3) would be ''exit 0'' but **exitcode 0** is not **exitcode 0^M** and therefore you would see an error like ''numeric argument required: exit: 0''Nevertheless the script is able to "repair" itself and will work properly if you start it up a second time or reboot Batocera twice after script activation through EmulationStation. +Nevertheless the script is able to "repair" itself and will work properly if you start it up a second time or reboot Batocera twice after script activation through EmulationStation. If you left this service active in background then at last on reboot, every user-added service script can be repaired now.
  • scripting_services_rules_examples.1728868541.txt.gz
  • Last modified: 20 months ago
  • by maximumentropy