Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| gentoo:locald [2024/04/05 07:57] – willy | gentoo:locald [2025/05/06 05:45] (current) – willy | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Custom User Services ====== | + | ====== | 
| Since i am running lots of various shaped and managed services, i needed an efficient and **simple** way to automatically start and stop them. | Since i am running lots of various shaped and managed services, i needed an efficient and **simple** way to automatically start and stop them. | ||
| Line 8: | Line 8: | ||
| Let me show how i am managing this approach. | Let me show how i am managing this approach. | ||
| + | |||
| + | *NOTE:* this approach _do not applies_ to podman containers. See [[gentoo: | ||
| ===== local.d ===== | ===== local.d ===== | ||
| Line 30: | Line 32: | ||
| The other way is to use local.d service leveraging a specific script with symlinks: the idea is to use one script for most of the services, and just create a symlink to the start and stop scripts themselves. | The other way is to use local.d service leveraging a specific script with symlinks: the idea is to use one script for most of the services, and just create a symlink to the start and stop scripts themselves. | ||
| - | Assuming you have a service called **myservice**, which is a podman container, run as // | + | For a simple non-containerized | 
| - | <code bash> | + | |
| - | cd / | + | |
| - | ln -s _servicer.sh 50-myservice-myuser-podman.start | + | |
| - | ln -s _servicer.sh 50-myservice-myuser-podman.stop | + | |
| - | </ | + | |
| - | + | ||
| - | and that's it. This will automatically create the **/ | + | |
| - | + | ||
| - | Similarly, for a simple non-containerized service: | + | |
| <code bash> | <code bash> | ||
| cd / | cd / | ||
| Line 68: | Line 61: | ||
| #  - user is the user the service will run as. Can be omitted, in this case use two " | #  - user is the user the service will run as. Can be omitted, in this case use two " | ||
| # - type is one of: | # - type is one of: | ||
| - | # - podman: the service is a podman-compose based containeration | ||
| #        - script: the service is managed by a script ($user_home/ | #        - script: the service is managed by a script ($user_home/ | ||
| # - service: the service command is specified inside a service_${service}_start file in the user home folder | # - service: the service command is specified inside a service_${service}_start file in the user home folder | ||
| Line 91: | Line 83: | ||
| USER_HOME=$(echo ${passwd_entry} | cut -d: -f 6) | USER_HOME=$(echo ${passwd_entry} | cut -d: -f 6) | ||
| - | echo " | + | echo " | 
| if [ " | if [ " | ||
| Line 99: | Line 91: | ||
| } && chown -R ${USER} " | } && chown -R ${USER} " | ||
| extra_opts=(-1 " | extra_opts=(-1 " | ||
| - | if [ " | + | if [ " | 
| - | then | + | |
| - | COMMAND=" | + | |
| - | ARGUMENTS=(compose up) | + | |
| - | echo " | + | |
| - | iptables -L -t nat &> /dev/null | + | |
| - | echo " | + | |
| - | podman network create ${SERVICE}-net &> /dev/null | + | |
| - | echo " | + | |
| - | su - " | + | |
| - | elif [ " | + | |
| then | then | ||
| echo " | echo " | ||
| Line 132: | Line 114: | ||
| then | then | ||
| extra_opts=() | extra_opts=() | ||
| - | if [ " | + | if [ " | 
| - | then | + | |
| - | echo " | + | |
| - | su - " | + | |
| - | action=(--stop " | + | |
| - | elif [ " | + | |
| then | then | ||
| echo " | echo " | ||
| Line 152: | Line 129: | ||
| fi | fi | ||
| - | start-stop-daemon -p / | + | echo start-stop-daemon -p / | 
| + | start-stop-daemon -p /var/run/${SERVICE}.pid ${extra_opts[@]} -u ${USER} -d ${USER_HOME} ${action[@]} | ||
| - | echo " | + | echo " | 
| </ | </ | ||
| Line 173: | Line 151: | ||
| **User name:** which user shall run the service. Can be omitted (just leave two " | **User name:** which user shall run the service. Can be omitted (just leave two " | ||
| - | **Service type:** can be // | + | **Service type:** can be //service// or //script//: | 
| - | - //podman//: run **podman compose up (or down)** as the user | + | |
| - // | - // | ||
| - //script//: will run $HOME/< | - //script//: will run $HOME/< | ||
| Line 194: | Line 171: | ||
| If you use (and you sohuld) LogRotate to keep your logs sanely rotated and trimmed, add the following **/ | If you use (and you sohuld) LogRotate to keep your logs sanely rotated and trimmed, add the following **/ | ||
| <file - servicer> | <file - servicer> | ||
| - | / | + | /var/log/ | 
| + | missingok | ||
| + | } | ||
| + | / | ||
| missingok | missingok | ||
| } | } | ||