service-scripts.txt (5277B)
1 SERVICE SCRIPTS 2 -------------------------------------------------------------------------------- 3 4 TABLE OF CONTENTS 5 - Creating service scripts [1.0] 6 - Service dependencies [1.1] 7 - Running commands after service stop [1.2] 8 9 10 [1.0] Creating service scripts 11 -------------------------------------------------------------------------------- 12 13 Service scripts can be anything from C binaries to shell scripts. All they need 14 is to be executable. However, given the simplicity of the services, most of them 15 are better written in POSIX sh. A simple service could be as following: 16 17 +--------------------------------------------------------------------------+ 18 | /etc/sysmgr/acpid | 19 +--------------------------------------------------------------------------+ 20 | | 21 | #!/bin/sh | 22 | exec acpid -f | 23 | | 24 +--------------------------------------------------------------------------+ 25 26 If you are familiar with acpid, you may have noticed the '-f' flag, which makes 27 sure that the program is daemonized, and the program stays in the foreground. 28 This is important, because sysmgr expects that the program doesn't fork itself 29 to the background. That's how we are tracking its process id. Usually service 30 programs such as SSHD, NTPD, DHCPCD, etc. have a flag for staying in the 31 foreground, and the service script must make use of such flags. 32 33 Another thing you may have noticed is the 'exec' part of the service. In shell, 34 'exec' makes the shell exit and run the given program with its own process id. 35 This is also necessary to track the correct process. 36 37 38 [1.1] Service dependencies 39 -------------------------------------------------------------------------------- 40 41 Some services may depend on other services in order to start working. An example 42 for this is NetworkManager which depends on dbus in order to launch. sysmgr 43 comes with a utility named sysmgr-needs which can be used to manage service 44 dependencies. Here are example service scripts for NetworkManager and dbus. 45 46 +--------------------------------------------------------------------------+ 47 | /etc/sysmgr/dbus | 48 +--------------------------------------------------------------------------+ 49 | | 50 | #!/bin/sh | 51 | mkdir -p /run/dbus | 52 | exec dbus-daemon --system --nofork --nopidfile | 53 | | 54 +--------------------------------------------------------------------------+ 55 | /etc/sysmgr/NetworkManager | 56 +--------------------------------------------------------------------------+ 57 | | 58 | #!/bin/sh | 59 | sysmgr-needs dbus || exit 1 | 60 | exec NetworkManager -n | 61 | | 62 +--------------------------------------------------------------------------+ 63 64 65 [1.2] Running commands after service stop 66 -------------------------------------------------------------------------------- 67 68 sysmgr doesn't come with a way to handling service stops. Runit has the 'stop' 69 script and systemd comes with a 'ExecStop' command. This can be easily handled 70 within the service script itself. 71 72 +--------------------------------------------------------------------------+ 73 | /etc/sysmgr/agetty-1 | 74 +--------------------------------------------------------------------------+ 75 | | 76 | #!/bin/sh | 77 | setsid agetty tty1 38400 linux & | 78 | | 79 | # Get the process id and replace the stored process id with agetty's | 80 | printf '%s\n' "$!" > /run/sysmgr/agetty-1/pid | 81 | wait | 82 | | 83 | # Wait until agetty dies and continue the script | 84 | utmpset -w tty1 | 85 | | 86 +--------------------------------------------------------------------------+ 87