Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| docs:guide-developer:procd-init-script-example [2019/03/10 16:58] – created bobafetthotmail | docs:guide-developer:procd-init-script-example [2024/02/10 17:38] (current) – [Enabling the service] systemcrash | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== |
| <WRAP center round info 80%> | <WRAP center round info 80%> | ||
| - | This article is a verbatim copy of https:// | + | This article is a mostly |
| + | It was adapted to use an equivalent shell script instead of NodeJS JavaScript, because it's lighter and better for a simple testing setup on most OpenWrt devices. | ||
| </ | </ | ||
| Line 9: | Line 10: | ||
| =====Setting up===== | =====Setting up===== | ||
| - | As example, lets say we’d want to create | + | As example, lets say we’d want to create |
| - | < | + | < |
| - | /** | + | #!/bin/sh |
| - | | + | |
| - | */ | + | #these if statements will check input and place default values if no input is given |
| - | const name = process.argv[2] || 'You'; | + | #they will also check if input is a number so you can call |
| - | const every = process.argv[3] || 5000; | + | #this script with just a time and it will still work correctly |
| + | |||
| + | if [ " | ||
| + | | ||
| + | else | ||
| + | if echo " | ||
| + | name=" | ||
| + | else | ||
| + | name=" | ||
| + | fi | ||
| + | fi | ||
| + | |||
| + | if [ "$2" = '' | ||
| + | every=" | ||
| + | else | ||
| + | every=" | ||
| + | fi | ||
| + | |||
| + | if echo " | ||
| + | every=" | ||
| + | fi | ||
| + | |||
| + | #endless loop, will print the message every X seconds as indicated in the $every variable | ||
| + | |||
| + | while [ 1 ]; do | ||
| + | echo "Hey, $name, it's time to get up" | ||
| + | sleep $every | ||
| + | done | ||
| + | |||
| + | exit 0 | ||
| - | setInterval(function(){ | ||
| - | console.log(`Hey, | ||
| - | }, every); | ||
| - | view raw | ||
| </ | </ | ||
| - | Place it in **/var/mynodeservice** and test it by running on OpenWrt | + | Place it in **/var/myscript.sh** and test it by running on OpenWrt |
| - | < | + | < |
| =====Creating a basic procd script===== | =====Creating a basic procd script===== | ||
| - | Now that we have a working script, we can make a service out of it. Create a file in / | + | Now that we have a working script, we can make a service out of it. Create a file in / |
| <code bash> | <code bash> | ||
| #!/bin/sh / | #!/bin/sh / | ||
| Line 37: | Line 64: | ||
| start_service() { | start_service() { | ||
| procd_open_instance | procd_open_instance | ||
| - | procd_set_param command | + | procd_set_param command /bin/sh "/var/myscript.sh" |
| procd_close_instance | procd_close_instance | ||
| } | } | ||
| Line 44: | Line 71: | ||
| First, it includes the common ‘run commands’ file / | First, it includes the common ‘run commands’ file / | ||
| - | The START option basically tell the system when the service should start and stop during startup and shutdown of OpenWRT. | + | The START option basically tell the system when the service should start and stop during startup and shutdown of OpenWrt. |
| This init script isn’t very useful at the moment but it shows the basic building blocks on which we will develop the script further. | This init script isn’t very useful at the moment but it shows the basic building blocks on which we will develop the script further. | ||
| Line 50: | Line 77: | ||
| =====Enabling the service===== | =====Enabling the service===== | ||
| - | To tell OpenWRT | + | To tell OpenWrt |
| - | < | + | < |
| - | This will install a symlink for us in directory /etc/rc.d/ called | + | This will install a symlink for us in directory /etc/rc.d/ called |
| < | < | ||
| Line 71: | Line 98: | ||
| Testing the service | Testing the service | ||
| - | Finally, lets just test our service. Open a second shell to the OpenWRT | + | Finally, lets just test our service. Open a second shell to the OpenWrt |
| < | < | ||
| Line 78: | Line 105: | ||
| then enable (if you havent done that yet), and start the service. | then enable (if you havent done that yet), and start the service. | ||
| - | < | + | < |
| - | $ / | + | $ / |
| After about 5 seconds we should see the message repeat itself in the log, but we didn’t… | After about 5 seconds we should see the message repeat itself in the log, but we didn’t… | ||
| Line 92: | Line 119: | ||
| start_service() { | start_service() { | ||
| procd_open_instance | procd_open_instance | ||
| - | procd_set_param command | + | procd_set_param command /bin/sh "/var/myscript.sh" |
| procd_set_param stdout 1 | procd_set_param stdout 1 | ||
| procd_set_param stderr 1 | procd_set_param stderr 1 | ||
| Line 101: | Line 128: | ||
| Now, when we restart we should see something like | Now, when we restart we should see something like | ||
| - | < | + | < |
| - | ... ommitted ... node[20136]: | + | ... ommitted ... Hey, You, it's time to get up |
| - | ... ommitted ... node[20136]: | + | ... ommitted ... Hey, You, it's time to get up |
| - | ... ommitted ... node[20136]: | + | ... ommitted ... Hey, You, it's time to get up |
| - | ... ommitted ... node[20136]: | + | ... ommitted ... Hey, You, it's time to get up |
| - | ... ommitted ... node[20136]: | + | ... ommitted ... Hey, You, it's time to get up |
| - | ... ommitted ... node[20136]: | + | ... ommitted ... Hey, You, it's time to get up |
| ... | ... | ||
| </ | </ | ||
| + | |||
| Setting up a service simple script like above with procd already gives us some advantages | Setting up a service simple script like above with procd already gives us some advantages | ||
| Line 117: | Line 145: | ||
| =====Service configuration===== | =====Service configuration===== | ||
| - | It’s time to get more personal, and to that we will use OpenWRTs | + | It’s time to get more personal, and to that we will use OpenWrts |
| - | < | + | < |
| option name ' | option name ' | ||
| - | | + | |
| </ | </ | ||
| UCI will immediately pick this up and the config for our service can be inspected like | UCI will immediately pick this up and the config for our service can be inspected like | ||
| - | < | + | < |
| - | mynodeservice.hello=mynodeservice | + | myservice.hello=myservice |
| - | mynodeservice.hello.name=Ninja | + | myservice.hello.name=Joost |
| - | mynodeservice.hello.every=' | + | myservice.hello.every=' |
| </ | </ | ||
| Also single options can be requested | Also single options can be requested | ||
| - | < | + | < |
| and we can also change specific configuration with UCI | and we can also change specific configuration with UCI | ||
| - | < | + | < |
| $ uci commit</ | $ uci commit</ | ||
| Line 152: | Line 180: | ||
| STOP=01 | STOP=01 | ||
| - | CONFIGURATION=mynodeservice | + | CONFIGURATION=myservice |
| start_service() { | start_service() { | ||
| Line 166: | Line 194: | ||
| # pass config to script on start | # pass config to script on start | ||
| - | procd_set_param command | + | procd_set_param command /bin/sh "/var/myscript.sh" " |
| - | procd_set_param file / | + | procd_set_param file / |
| procd_set_param stdout 1 | procd_set_param stdout 1 | ||
| procd_set_param stderr 1 | procd_set_param stderr 1 | ||
| Line 176: | Line 204: | ||
| We can pass new configuration by running | We can pass new configuration by running | ||
| - | < | + | < |
| $ uci commit | $ uci commit | ||
| </ | </ | ||
| Line 185: | Line 213: | ||
| < | < | ||
| - | procd_set_param file / | + | procd_set_param file / |
| ...</ | ...</ | ||
| - | With that line in place we are able to only restart the service whenever our configuration has changed. | + | With that line in place we are able to restart the service whenever |
| - | < | + | < |
| </ | </ | ||
| Line 198: | Line 226: | ||
| I’ll list a few here, but this is by no means covering everything. | I’ll list a few here, but this is by no means covering everything. | ||
| - | * **respawn**\\ respawn your service automatically when it died for some reason.\\ <code bash> | + | * **respawn**\\ respawn your service automatically when it terminates |
| ${respawn_threshold: | ${respawn_threshold: | ||
| - | ${respawn_timeout: | + | ${respawn_timeout: |
| * **pidfile**\\ Configure where to store the pid file \\ <code bash> | * **pidfile**\\ Configure where to store the pid file \\ <code bash> | ||
| Line 206: | Line 234: | ||
| * **env vars** \\ Pass environment variables to your process with \\ <code bash> | * **env vars** \\ Pass environment variables to your process with \\ <code bash> | ||
| - | * **ulimit** \\ If you need to set resource limits for your process you can use \\ <code bash> | + | * **ulimit** \\ If you need to set resource limits for your process you can use \\ <code bash> |
| -f: file size (blocks) | -f: file size (blocks) | ||
| -t: cpu time (seconds) | -t: cpu time (seconds) | ||