Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| gentoo:locald [2024/04/02 13:04] – [The Script] 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 74: | Line 66: | ||
| # | # | ||
| # | # | ||
| + | |||
| + | LOG_PATH=/ | ||
| + | test ! -d " | ||
| + | actions_logs=" | ||
| service_user_type_action=${0# | service_user_type_action=${0# | ||
| Line 85: | Line 81: | ||
| passwd_entry=$(getent passwd ${USER}) || exit 255 | passwd_entry=$(getent passwd ${USER}) || exit 255 | ||
| - | HOME=$(echo ${passwd_entry} | cut -d: -f 6) | + | USER_HOME=$(echo ${passwd_entry} | cut -d: -f 6) |
| + | |||
| + | echo " | ||
| if [ " | if [ " | ||
| then | then | ||
| - | | + | |
| - | | + | mkdir "${LOG_PATH}/ |
| - | COMMAND="$(which podman)" | + | |
| - | | + | |
| - | iptables | + | |
| - | podman network create | + | |
| - | | + | |
| then | then | ||
| - | start_script=" | + | |
| - | test -e ${start_script} || start_script=" | + | |
| - | | + | test -e ${start_script} || start_script=" |
| + | | ||
| + | COMMAND=" | ||
| ARGUMENTS="" | ARGUMENTS="" | ||
| elif [ " | elif [ " | ||
| then | then | ||
| - | source_script=" | + | |
| + | | ||
| test -e " | test -e " | ||
| - | echo " | + | echo " |
| exit 255 | exit 255 | ||
| } | } | ||
| + | echo " | ||
| source " | source " | ||
| fi | fi | ||
| Line 113: | Line 113: | ||
| elif [ " | elif [ " | ||
| then | then | ||
| - | if [ " | + | |
| + | | ||
| then | then | ||
| - | | + | |
| - | action=(--stop "${SERVICE}") | + | stop_script=" |
| - | elif [ " | + | test -e " |
| - | then | + | echo " |
| - | stop_script=" | + | |
| - | test -e " | + | } |
| elif [ " | elif [ " | ||
| then | then | ||
| Line 128: | 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 " | ||
| </ | </ | ||
| Line 147: | 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 161: | Line 164: | ||
| ===== Adding a container-based service ===== | ===== Adding a container-based service ===== | ||
| - | It's easy, just create the symlink in / | + | It's easy, just create the symlink in / |
| + | |||
| + | |||
| + | ===== Logrotate ===== | ||
| + | |||
| + | If you use (and you sohuld) LogRotate to keep your logs sanely rotated and trimmed, add the following **/ | ||
| + | <file - servicer> | ||
| + | / | ||
| + | missingok | ||
| + | } | ||
| + | / | ||
| + | missingok | ||
| + | } | ||
| + | </ | ||