Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| gentoo:locald [2024/04/02 12:56] – created 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 | ||
| - | if [ " | + | |
| + | mkdir " | ||
| + | } && chown -R ${USER} " | ||
| + | extra_opts=(-1 " | ||
| + | | ||
| then | then | ||
| - | | + | |
| - | ARGUMENTS=(compose up) | + | start_script=" |
| - | iptables -L -t nat &> /dev/null | + | test -e ${start_script} || start_script=" |
| - | podman network create ${SERVICE}-net &> /dev/null | + | |
| - | elif [ "${TYPE}" = " | + | COMMAND=" |
| - | then | + | |
| - | start_script=" | + | |
| - | test -e ${start_script} || start_script=" | + | |
| - | | + | |
| 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 " | ||
| </ | </ | ||
| + | Save the script and make it executable. | ||
| + | |||
| + | ===== How it works ===== | ||
| + | |||
| + | The script takes the name of itself ($0) and dissects it to extract: | ||
| + | - The service name | ||
| + | - The user that must run the service | ||
| + | - The service type | ||
| + | - The action | ||
| + | |||
| + | Based on this information, | ||
| + | |||
| + | **Service name:** needs to be unique, will be used to create **/ | ||
| + | |||
| + | **User name:** which user shall run the service. Can be omitted (just leave two " | ||
| + | |||
| + | **Service type:** can be //service// or //script//: | ||
| + | - // | ||
| + | - //script//: will run $HOME/< | ||
| + | |||
| + | Example **service_< | ||
| + | < | ||
| + | COMMAND="/ | ||
| + | ARGUMENTS=(param1 param2 param3) | ||
| + | </ | ||
| + | Please note that ARGUMENTS should be a bash array. | ||
| + | |||
| + | ===== Adding a container-based service ===== | ||
| + | |||
| + | 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 | ||
| + | } | ||
| </ | </ | ||
| + | |||