[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
playground:runit [2018/12/19 20:17 (UTC)] – [9. run scripts] chrisabela | playground:runit [2018/12/19 20:33 (UTC)] (current) – removed chrisabela | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== runit ====== | ||
- | ===== 1. Introduction ===== | ||
- | |||
- | runit is a UNIX init scheme with service supervision. It is a cross-platform Unix init scheme with service supervision, | ||
- | |||
- | In Unix-based computer operating systems, init (short for initialization) is the first process started during booting of the computer system. Init is a daemon process that continues running until the system is shut down. Slackware comes with its own legacy init (/ | ||
- | |||
- | The init daemon (or its replacement) is characterised by Process ID 1 (PID 1). | ||
- | |||
- | To read on the benefits of runit, see here: http:// | ||
- | |||
- | ===== 2. Use runit with traditional init ===== | ||
- | |||
- | runit is not provided by Slackware, but a SlackBuild is maintained on https:// | ||
- | |||
- | As we do not want yet to replace init with runit, run the slackbuild with CONFIG=no: | ||
- | |||
- | < | ||
- | CONFIG=no ./ | ||
- | </ | ||
- | |||
- | Then install the resulting package and proceed as follows: | ||
- | |||
- | < | ||
- | mkdir /etc/runit/ /service/ | ||
- | cp -a / | ||
- | / | ||
- | </ | ||
- | |||
- | For a typical Slackware-stlyle service, you can also edit ''/ | ||
- | |||
- | < | ||
- | if [ -x / | ||
- | / | ||
- | fi | ||
- | </ | ||
- | |||
- | if you require it. | ||
- | |||
- | and then edit write ''/ | ||
- | |||
- | < | ||
- | #!/bin/sh | ||
- | RUNIT=x$( /sbin/pidof runsvdir ) | ||
- | if [ “$RUNIT” ! = x ]; then | ||
- | kill $RUNIT | ||
- | fi | ||
- | </ | ||
- | |||
- | Then give '' | ||
- | |||
- | < | ||
- | chmod +x / | ||
- | </ | ||
- | |||
- | and reboot | ||
- | |||
- | ===== 3. Using runit with sysvinit and inittab ===== | ||
- | |||
- | Remove the entries in ''/ | ||
- | |||
- | Edit ''/ | ||
- | |||
- | < | ||
- | cat >>/ | ||
- | SV: | ||
- | EOT | ||
- | </ | ||
- | |||
- | and tell init to re-read its configuration, | ||
- | |||
- | < | ||
- | init q | ||
- | </ | ||
- | |||
- | or reboot | ||
- | |||
- | ===== 4. How to replace init with runit ===== | ||
- | |||
- | If you followed the previous chapter, then stop runsvdir and reverse all the changes that you made on your system. | ||
- | |||
- | Remove the last line from ''/ | ||
- | |||
- | < | ||
- | rm -rf /etc/runit /service | ||
- | </ | ||
- | |||
- | Reboot: | ||
- | |||
- | < | ||
- | shutdown -r now | ||
- | </ | ||
- | |||
- | Also, it is best to re-build and re-install runit | ||
- | |||
- | This time, when running slackbuild, do pass any value for CONFIG, or set it to yes: | ||
- | |||
- | < | ||
- | unset CONFIG | ||
- | sh runit.SlackBuild | ||
- | </ | ||
- | |||
- | and upgrade with the resulting runit package. | ||
- | |||
- | Disable services that are already provided by the traditional init: | ||
- | |||
- | < | ||
- | cd /service/ | ||
- | rm * | ||
- | </ | ||
- | |||
- | Create a new TTY service for runit: | ||
- | |||
- | < | ||
- | cd /etc/sv/ | ||
- | cp -ar agetty-tty1/ | ||
- | sed -i ' | ||
- | ln -s / | ||
- | </ | ||
- | |||
- | Start runit' | ||
- | |||
- | < | ||
- | / | ||
- | </ | ||
- | |||
- | And check that the agetty is running, by logging in as an unprivileged user (not as root!) | ||
- | |||
- | < | ||
- | CTRL+ALT+F8 | ||
- | </ | ||
- | |||
- | If this has been successful, then reverse the last intervention: | ||
- | |||
- | < | ||
- | kill $( pidof runsvdir ) | ||
- | |||
- | rm / | ||
- | rm -rf / | ||
- | for N in {1..6}; do | ||
- | ln -s / | ||
- | done | ||
- | </ | ||
- | |||
- | Then, to boot in runit, reboot and enter '' | ||
- | |||
- | {{: | ||
- | |||
- | //Image 1.: Hit the Tab key on the Lilo Menu before the counter times out.// | ||
- | |||
- | {{: | ||
- | |||
- | //Image 2: Add init=/ | ||
- | |||
- | Confirm that runit has taken PID 1: | ||
- | |||
- | < | ||
- | ps -o comm= -p 1 | ||
- | runit | ||
- | </ | ||
- | |||
- | If you do not have a multi boot system, you may append the init selection '' | ||
- | |||
- | For example: | ||
- | |||
- | < | ||
- | append=" | ||
- | < | ||
- | |||
- | Then run lilo: | ||
- | |||
- | < | ||
- | lilo -v | ||
- | </ | ||
- | |||
- | Or just replace ''/ | ||
- | |||
- | < | ||
- | cp /sbin/init / | ||
- | cp / | ||
- | </ | ||
- | |||
- | However, with the last alternative, | ||
- | |||
- | ===== 5. Startup and Shutdown ===== | ||
- | |||
- | The traditional init reads ''/ | ||
- | |||
- | The script ''/ | ||
- | |||
- | Note that the shutdown command provided by sysvinit package will not work in runit. To reboot you enter | ||
- | |||
- | < | ||
- | / | ||
- | </ | ||
- | |||
- | and to halt enter | ||
- | |||
- | < | ||
- | / | ||
- | </ | ||
- | |||
- | A convenient script is provided with the configuration files which is placed in ''/ | ||
- | |||
- | The '' | ||
- | |||
- | ===== 6. runsv ===== | ||
- | |||
- | Although runit may replace init as the PID 1 daemon, there is not much benefit unless other services are migrated from Slackware’s BSD style to runsv(8). While this is not a complicated task, some familiarisation with runit’s characteristics is recommended and makes the task easier. This short example should help illustrate runsv to new users. | ||
- | |||
- | Make an arbitrary directory under ''/ | ||
- | |||
- | < | ||
- | mkdir / | ||
- | </ | ||
- | |||
- | In order to let the non-privileged user (say user chris) run it, change the ownership of the service | ||
- | |||
- | < | ||
- | chmod -R chris.users / | ||
- | </ | ||
- | |||
- | As a non-priviliged user, create some directories | ||
- | |||
- | < | ||
- | mkdir -p / | ||
- | </ | ||
- | |||
- | Then edit a script that simulates a (finicky) service in a convenient directory; say | ||
- | |||
- | < | ||
- | #!/bin/bash | ||
- | echo " | ||
- | for i in {1..10} | ||
- | do | ||
- | echo "Doing stuff..." | ||
- | sleep 1 | ||
- | done | ||
- | echo "Oh no I crashed..." | ||
- | exit 1 | ||
- | </ | ||
- | |||
- | Give it executable permission: | ||
- | |||
- | < | ||
- | chmod +x / | ||
- | </ | ||
- | |||
- | Run it to check and appreciate its execution: | ||
- | |||
- | < | ||
- | / | ||
- | </ | ||
- | |||
- | Log back to root, and edit a file called ''/ | ||
- | |||
- | < | ||
- | #!/bin/sh -e | ||
- | exec 2>&1 | ||
- | exec chpst -u chris / | ||
- | </ | ||
- | |||
- | < | ||
- | chmod +x / | ||
- | </ | ||
- | |||
- | Note the exec in the last line, it tells the shell that interprets the script to replace itself with the service daemon | ||
- | |||
- | Naturally edit as necessary. | ||
- | |||
- | Create another directory: | ||
- | |||
- | < | ||
- | mkdir / | ||
- | </ | ||
- | |||
- | and edit another file under it also called ''/ | ||
- | |||
- | < | ||
- | #!/bin/sh | ||
- | exec chpst -u chris svlogd -tt / | ||
- | </ | ||
- | |||
- | Give it executable permission: | ||
- | |||
- | < | ||
- | chmod +x / | ||
- | </ | ||
- | |||
- | Run the sv service to ensure that it works: | ||
- | |||
- | < | ||
- | / | ||
- | </ | ||
- | |||
- | Finally, we are ready to deploy the service. Create a symbolic link in ''/ | ||
- | |||
- | < | ||
- | ln -s / | ||
- | </ | ||
- | |||
- | Watch how your “service” works, crashes, but it is recovered by runit: | ||
- | |||
- | < | ||
- | tail -f / | ||
- | </ | ||
- | |||
- | ===== 7. Managing Services with sv ===== | ||
- | |||
- | To see the status of a supervised service use '' | ||
- | |||
- | < | ||
- | sv s example | ||
- | </ | ||
- | |||
- | returns | ||
- | |||
- | < | ||
- | run: example: (pid 42) 1587s | ||
- | </ | ||
- | |||
- | To see the status of all services, use | ||
- | |||
- | < | ||
- | sv s /service/* | ||
- | </ | ||
- | ==== 7.1 Stop/ | ||
- | |||
- | Start a service | ||
- | |||
- | < | ||
- | sv u example | ||
- | </ | ||
- | |||
- | Stop a service | ||
- | |||
- | < | ||
- | sv d example | ||
- | </ | ||
- | |||
- | Restart a service | ||
- | |||
- | < | ||
- | sv t example | ||
- | </ | ||
- | |||
- | Each of these is a shortcut, for ‘up’, ‘down’, and ‘terminate’, | ||
- | More verbose forms of the above | ||
- | |||
- | < | ||
- | sv start sshd | ||
- | |||
- | sv stop sshd | ||
- | |||
- | sv restart sshd | ||
- | </ | ||
- | |||
- | Each of these will also return the status of the service upon exit. | ||
- | |||
- | ==== 7.2 Enabling a service ==== | ||
- | |||
- | Service directories are placed under ''/ | ||
- | |||
- | < | ||
- | ln -s / | ||
- | </ | ||
- | |||
- | Once a service is linked it will always start on boot and restart if it stops (unless this is disabled). | ||
- | |||
- | ==== 7.3 Disabling a service ==== | ||
- | |||
- | To disable a service in the current runlevel remove the symlink to its service directory from ''/ | ||
- | |||
- | < | ||
- | rm / | ||
- | </ | ||
- | |||
- | Removing the symlink will also stop the service. | ||
- | |||
- | Check out the sv man page to see additional options. | ||
- | |||
- | < | ||
- | man sv | ||
- | </ | ||
- | |||
- | If you do not want a service to start upon bootup, or when enabled, just touch an empty file called '' | ||
- | |||
- | < | ||
- | touch / | ||
- | </ | ||
- | ==== 7.4 Dependencies ==== | ||
- | |||
- | Dependencies of service are supported by starting the dependent run script as follows: | ||
- | |||
- | < | ||
- | #!/bin/sh | ||
- | sv start dependent-service || exit 1 | ||
- | . | ||
- | . | ||
- | exec … | ||
- | </ | ||
- | ===== 8. Runlevels ===== | ||
- | |||
- | If you installed the slackbuild configuration files, you have two runlevels: default and single. The current runlevel is default. You can verify by looking under ''/ | ||
- | |||
- | Change runlevel to another runlevel; single: | ||
- | |||
- | < | ||
- | runsvchdir single | ||
- | </ | ||
- | |||
- | You will see that ''/ | ||
- | |||
- | < | ||
- | runsvchdir default >/ | ||
- | </ | ||
- | |||
- | You can create any other runlevel and name them as you like. To start you can copy an existing runlevel directory and modify as you wish. | ||
- | |||
- | < | ||
- | cp -ar / | ||
- | </ | ||
- | |||
- | You can edit the runsvdir line of ''/ | ||
- | ===== 9. run scripts ===== | ||
- | |||
- | Only one executable can be called for a service and the last line must be called by the '' | ||
- | |||
- | In direct contrast to Slackware’s BSD style service scripts, runit run scripts must not must run in the foreground, otherwise runit would think that they have crashed and restart them. | ||
- | |||
- | For some services, this may not be possible, but there are workarounds. You can forcefully terminate them by ending the run script like this: | ||
- | |||
- | < | ||
- | sv d service_name | ||
- | </ | ||
- | |||
- | Or you can use the '' | ||
- | |||
- | < | ||
- | exec chpst -b service_name pause | ||
- | </ | ||
- | |||
- | ===== 10. Migrating Services ===== | ||
- | |||
- | It is suggested that services are migrated from stage 2 (which are still under the Slackware’s BSD init scheme) to stage 3 carefully. Start from the end of / | ||
- | |||
- | Then continue for / | ||
- | |||
- | Proceed with other services to benefit’s from runit features. Note that for some services, such as atd and crond, you would need to edit / | ||
- | |||
- | Let’s take the popular NetworkManager service as an example. According to the permission of / | ||
- | |||
- | Stop the service: | ||
- | |||
- | / | ||
- | |||
- | Disable it: | ||
- | |||
- | chmod -x / | ||
- | |||
- | Create a suitable directory for runit | ||
- | |||
- | mkdir / | ||
- | |||
- | Write a runit run script: / | ||
- | |||
- | #!/bin/sh | ||
- | prefix=/usr | ||
- | exec_prefix=/ | ||
- | sbindir=${exec_prefix}/ | ||
- | NETWORKMANAGER_BIN=${sbindir}/ | ||
- | |||
- | export XDG_CACHE_HOME=/ | ||
- | exec $NETWORKMANAGER_BIN -n > /dev/null 2>&1 | ||
- | |||
- | Symlink it to /service and NetworkManager to enable under current runlevel and it will start in a few seconds: | ||
- | |||
- | ln -s / | ||
- | |||
- | For udev, it is stuggested to let it under runit’s stage 2, but you can setup its monitoring for stage 3. | ||
- | |||
- | |||
- | Sources: | ||
- | http:// | ||
- | |||
- | https:// | ||
- | |||
- | https:// | ||
- | |||
- | https:// | ||
- | |||
- | https:// | ||
- | |||
- | http:// | ||
- | |||
- | https:// | ||
- | |||
- | https:// | ||
- | |||
- | Author: Chris Abela for Slackware 14.2 | ||
- | December 2018 |