runit.wht (2485B)
1 # Runit 2 3 ## Usage 4 5 - sv status, sv s 6 > prints status of service 7 - sv up, sv u 8 > start service, restart if stops 9 - sv down, sv d 10 > stop service, do not restart 11 - sv once, sv o 12 > start service, do not restart 13 - sv pause, cont, hup, quit, term, kill 14 > restarts service 15 - sv exit, sv e 16 > kill service, do not restart 17 18 ## User services 19 20 Create a service named `runsvdir-<uname>` with `run` script containing: 21 22 ```bash 23 #!/bin/sh 24 25 export USER="<uname>" 26 export HOME="/home/<uname>" 27 28 groups="$(id -Gn "$USER" | tr ' ' ':')" 29 svdir="$HOME/<servicedir>" 30 31 exec chpst -u "$USER:$groups" runsvdir "$svdir" 32 ``` 33 34 Afterwards you can export `SVDIR="$HOME/<servicedir>"` for convenience. 35 36 ## Service management 37 38 ### Service structure 39 40 ``` 41 *run - (mandatory) starts process 42 *check - exit 0 means service is running 43 *finish - is ran after any shutdown (for cleanup) 44 control/ 45 ├── *c - replaces SIGCONT 46 ├── *d - is ran after control/t for down 47 ├── *x - is ran after control/t for exit 48 ├── *t - replaces SIGTERM 49 └── *u - is ran before run 50 log/ - managed by sv, contains logs 51 supervise/ - managed by sv 52 ``` 53 54 ### Files 55 56 #### run 57 58 Mandatory (everything else is optional) script that is ran to start the service. 59 60 #### check 61 62 Script that performs manual check of service status and returns `0` if service is up and running or any non-zero code otherwise. 63 64 #### finish 65 66 A cleanup script. Is ran last after service is terminated by any means, including crashes. 67 68 #### control/d and control/x 69 70 Ran after `SIG____` when `sv` is called with either `down` or `exit` respectively. 71 72 #### control/t, control/c, control/h, etc 73 74 Scripts that replace `SIGTERM`, `SIGCONT` and `SIGHUP` respectively. Can be used to control the way that service is shut down. If they fail to stop service then they must exit with any non-zero code. 75 76 The letter comes from first letter that comes after `SIG`. 77 78 #### control/u 79 80 Ran when `sv` is called with `up` before the `run` script. 81 82 ### Using with screen 83 84 #### Run 85 86 ```bash 87 #!/bin/sh 88 89 # exec seems to be crucial 90 exec screen -DmS session_name program 91 ``` 92 93 #### Stop, term and down 94 95 ```bash 96 #!/bin/sh 97 98 screen -S session_name -X kill 99 # or if needs to be terminated with command 100 screen -S session_name -X stuff stop 101 102 sleep 5 103 ``` 104 105 ### Do not autostart 106 107 Create `/path/to/service/down` 108 109 ### Fast check status as exit code 110 111 ```bash 112 sv status devdocs | perl -ne 'exit(/^run:/gi ? 0 : 1)' && $() || $() 113 ``` 114 115