File: shepherd.texi

package info (click to toggle)
shepherd 1.0.9-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,828 kB
  • sloc: lisp: 8,779; sh: 3,586; makefile: 290; ansic: 50
file content (2874 lines) | stat: -rw-r--r-- 111,148 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
\input texinfo @c -*- texinfo -*-
@c shepherd.texi -- The documentation in Texinfo format.
@documentencoding UTF-8
@setfilename shepherd.info
@settitle The GNU Shepherd Manual

@include version.texi
@set OLD-YEARS 2002, 2003
@set NEW-YEARS 2013, 2016, 2018--2020, 2022--2025

@copying
Copyright @copyright{} @value{OLD-YEARS} Wolfgang J@"ahrling@*
Copyright @copyright{} @value{NEW-YEARS} Ludovic Courtès@*
Copyright @copyright{} 2020 Brice Waegeneire@*
Copyright @copyright{} 2020 Oleg Pykhalov@*
Copyright @copyright{} 2020, 2023 Jan (janneke) Nieuwenhuizen@*
Copyright @copyright{} 2024 Jakob Kirsch@*

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.  A
copy of the license is included in the section entitled ``GNU Free
Documentation License''.
@end copying

@dircategory System software
@direntry
* shepherd: (shepherd). The Shepherd service manager.
* herd: (shepherd)Invoking herd
                        Controlling the Shepherd service manager.
* reboot: (shepherd)Invoking reboot
                        Rebooting a Shepherd-controlled system.
* halt: (shepherd)Invoking halt
                        Turning off a Shepherd-controlled system.
@end direntry

@titlepage
@title The GNU Shepherd Manual
@subtitle For use with the GNU Shepherd @value{VERSION}
@subtitle Last updated @value{UPDATED}

@author Wolfgang J@"ahrling
@author Ludovic Courtès

@insertcopying
@end titlepage

@contents


@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@ifnottex
@node Top
@top The GNU Shepherd Manual

This manual documents the GNU@tie{}Shepherd version @value{VERSION}, a
service manager for the GNU system.

@menu
* Introduction::                Introduction to the Shepherd service manager.
* Jump Start::                  How to do simple things with the Shepherd.
* herd and shepherd::           User interface to service management.
* Services::                    Details on services.
* Service Collection::          Services that come with the Shepherd.
* Misc Facilities::             Generally useful things provided by the Shepherd.
* Internals::                   Hacking shepherd.

* GNU Free Documentation License::  The license of this manual.
* Concept Index::
* Procedure and Macro Index::
* Variable Index::
* Type Index::
@end menu
@end ifnottex

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Introduction
@chapter Introduction

@cindex service manager
This manual documents the GNU@tie{}Daemon Shepherd, or GNU@tie{}Shepherd
for short.  The Shepherd looks after system services, typically @dfn{daemons}.
It is used to start and stop them in a reliable
fashion.  For instance, it will dynamically determine and start any
other services that our desired service depends upon.

The Shepherd is the @dfn{init system} of the GNU operating system---it is the
first user process that gets started, typically with PID 1, and runs
as @code{root}.  Normally the purpose of init systems is to manage all
system-wide services, but the Shepherd can also be a useful tool assisting
unprivileged users in the management of their own daemons.

Flexible software requires some time to master and
the Shepherd is no different.  But don't worry: this manual should allow you to
get started quickly.  Its first chapter is designed as a practical
introduction to the Shepherd and should be all you need for everyday use
(@pxref{Jump Start}).  In chapter two we will describe the
@command{herd} and @command{shepherd} programs, and their relationship, in
more detail (@ref{herd and shepherd}).  Subsequent chapters provide a full
reference manual and plenty of examples, covering all of Shepherd's
capabilities.  Finally, the last chapter provides information for
those souls brave enough to hack the Shepherd itself.

@cindex dmd
The Shepherd was formerly known as ``dmd'', which stands for @dfn{Daemon
Managing Daemons} (or @dfn{Daemons-Managing Daemon}?).

@cindex Guile
@cindex Scheme
This program is written in Guile Scheme.  Guile is also the Shepherd's
configuration language.  @xref{Introduction,,, guile, GNU Guile
Reference Manual}, for an introduction to Guile.  We have tried to make
the Shepherd's basic features as accessible as possible---you should be
able to use these even if you do not know how to program in Scheme.  A
basic grasp of Guile is required only if you wish to make use of the
Shepherd's more advanced features.

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Jump Start
@chapter Jump Start

The Shepherd comes with two main commands: @command{shepherd}, which is
the daemon that manages services (@pxref{Invoking shepherd}), and
@command{herd}, the command to monitor and control @command{shepherd}
(@pxref{Invoking herd}).  The @command{shepherd} command itself may run
as an ``init system'', also known as PID@tie{}1@footnote{In that case it
is the first process started by the system, hence the process identifier
(PID) 1.}, where it manages system services, or as a user, where it
manages user services.

As a user and system administrator, the @command{herd} command is your
main entry point; it allows you to get an overview of the services and
their status:

@example
herd status
@end example

@noindent
... to get detailed information about a specific service, including
recently-logged messages:

@example
herd status sshd
@end example

@noindent
... and to start, stop, restart, enable, or disable services, among
other possible actions:

@example
herd start nginx
herd stop tor
herd disable tor
herd restart guix-daemon
@end example

@cindex service graph
@cindex graph of services
Each service may depend on other services.  Services and their
dependencies form a @dfn{graph}, which you can view, for instance with
the help of @uref{https://github.com/jrfonseca/xdot.py, xdot}, by
running:

@example
herd graph | xdot -
@end example

Since @command{shepherd} knows about these dependencies, the
@command{herd start nginx} command above starts not just the
@code{nginx} service, but also every service it depends on.  Likewise,
@command{herd stop tor} stops not just @code{tor} but also everything
that depends on it.

Services may define @dfn{custom actions} in addition to @code{start},
@code{stop}, and the other actions mentioned above.  For example, the
@code{log-rotation} service has a custom @code{files} action to list log
files subject to rotation (@pxref{Log Rotation Service}):

@example
herd files log-rotation
@end example

A special service is @code{root}, which is used for controlling the
Shepherd itself (@pxref{The root Service}).  Its @code{status} action
displays info about the @command{shepherd} process, including messages
it recently logged, possibly on behalf of other services:

@example
herd status root
@end example

The @code{load} action of @code{root} lets you dynamically load new
service definitions from a configuration file, which we'll discuss
later; note that this action takes the file name as an extra argument:

@example
herd load root ~/additional-services.scm
@end example

To view a log of service events, including the time at which each
service was started or stopped, run:

@example
herd log
@end example

Last, you can view documentation about services and their actions:

@example
$ herd doc root
The root service is used to operate on shepherd itself.
$ herd doc root action power-off
power-off: Halt the system and turn it off.
@end example

That's pretty much all there is to know from the user and system
administrator viewpoint.

Packagers, distro developers, and advanced system administrator,
certainly noticed that we haven't talked about a crucial aspect for
them: the configuration file for @command{shepherd}.  That's certainly
the most interesting and unique bit of the Shepherd: @emph{configuration
is a Scheme program}.  You do not need to be an expert in the Scheme
programming language to write it though: for common uses, configuration
is entirely declarative and contains the same information you would find
in a systemd @file{.service} file or anything similar.  You can define
services that spawn a process, others that wait for incoming connections
and pass it on to a daemon (inetd-style or systemd-style @dfn{socket
activation}), as well as timed services that run a program periodically.
@xref{Service Examples}, to get started.

@cindex extensibility
What's interesting is that you can go beyond the kind of services
Shepherd offers you: You need a service that mounts/unmounts file
systems? A service that sets up networking?  One that runs a daemon in a
container?  This is where extensibility shines: you can write services
that do this---and more.  @xref{Defining Services}, for a reference of
the programming interface to define services.  And, whether or not you
use Guix System, check out the variety of services it provides
(@pxref{Services,,, guix, GNU Guix Reference Manual}).

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node herd and shepherd
@chapter @command{herd} and @command{shepherd}

@cindex herd
@cindex shepherd
@cindex daemon
@cindex daemon controller
@cindex relative file names
@cindex herding, of daemons
The daemon that runs in the background and is responsible for
controlling the services is @command{shepherd}, while the user interface
tool is called @command{herd}: it's the command that allows you to
actually @emph{herd} your daemons@footnote{
@cindex deco, daemon controller
In the past, when the
GNU@tie{}Shepherd was known as GNU@tie{}dmd, the @command{herd} command
was called @code{deco}, for @dfn{DaEmon COntroller}.}.  To perform an
action, like stopping a service or calling an action of a service, you
use the herd program.  It will communicate with shepherd over a Unix
Domain Socket.

Thus, you start @command{shepherd} once, and then always use
@command{herd} whenever you want to do something service-related.  Both
@command{shepherd} and @command{herd} understand the standard arguments
@code{--help}, @code{--version} and @code{--usage}.


@menu
* Invoking shepherd::           How to start the service damon.
* Invoking herd::               Controlling daemons.
* Invoking reboot::             Rebooting a shepherd-controlled system.
* Invoking halt::               Turning off a shepherd-controlled system.
@end menu

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Invoking shepherd
@section Invoking @command{shepherd}

@cindex @command{shepherd} Invocation
@cindex invoking @command{shepherd}
The @code{shepherd} program has the following synopsis:

@example
shepherd [@var{option}@dots{}]
@end example

@cindex configuration file
When @command{shepherd} starts, it reads and evaluates a configuration
file---actually a Scheme program that uses the Shepherd's programming
interface to define, register, and start services (@pxref{Defining
Services}).  When it is started with superuser privileges, it tries to
read @code{/etc/shepherd.scm}; when started as a unprivileged user, it
looks for a file called @code{$XDG_CONFIG_HOME/shepherd/init.scm}.  If
the @env{XDG_CONFIG_HOME} environment variable is undefined,
@code{$HOME/.config/shepherd/init.scm} is used instead (@pxref{Managing
User Services}).  The @option{--config} option described below lets you
choose a different file.

@cindex socket file, for @command{shepherd}
As the final ``d'' suggests, @command{shepherd} is a daemon that runs in
the background, so you will not interact with it directly.  After it is
started, @command{shepherd} listens on a socket, usually
@file{/var/run/shepherd/socket} (this can be changed with the
@option{--socket} option or the @env{SHEPHERD_SOCKET} environment
variable).  The @dfn{herd} tool sends commands to @command{shepherd}
using this socket (@pxref{Invoking herd}).

The @command{shepherd} command accepts the following options:

@cindex configuration file
@table @samp
@item -c @var{file}
@itemx --config=@var{file}
Read and evaluate @var{file} as the configuration file on startup.

Scheme code in @var{file} is evaluated in the context of a fresh module
where bindings from the @code{(shepherd service)} module are available,
in addition to the default set of Guile bindings.  It may typically
perform three actions:

@enumerate
@item
defining services using the @code{service} procedure (@pxref{Defining
Services});
@item
registering those services with @code{register-services} (@pxref{Service
Registry});
@item
starting some or all of those services with
@code{start-in-the-background} (@pxref{Interacting with Services}).
@end enumerate

@xref{Service Examples}, for examples of what @var{file} might look
like.

Note that @var{file} is evaluated @emph{asynchronously}:
@command{shepherd} may start listening for client connections (with the
@command{herd} command) before @var{file} has been fully loaded.  Errors
in @var{file} such as service startup failures or uncaught exceptions do
@emph{not} cause @command{shepherd} to stop.  Instead the error is
reported, leaving users the opportunity to inspect service state and,
for example, to load an updated config file with:

@example
herd load root @var{file}
@end example

@item -I
@itemx --insecure
@cindex security
@cindex insecure
Do not check if the directory where the socket---our communication
rendez-vous with @command{herd}---is located has permissions @code{700}.
If this option is not specified, @command{shepherd} will abort if the
permissions are not as expected.

@item -l [@var{file}]
@itemx --logfile[=@var{file}]
@cindex logging
@cindex log file
Log output into @var{file}.

For unprivileged users, the default log file is
@file{$XDG_STATE_HOME/shepherd/shepherd.log} with @file{$XDG_STATE_HOME}
defaulting to @file{$HOME/.local/state}.

@item --syslog[=@var{file}]
@cindex syslog
Force logging to @var{file} (@file{/dev/log} by default), a @dfn{syslog}
socket (@pxref{Overview of Syslog,,, libc, The GNU C Library Reference
Manual}).

When running as root, the default behavior is to connect to
@file{/dev/log}.  A syslog daemon,
@command{syslogd}, is expected to read messages from there
(@pxref{syslogd invocation, syslogd,, inetutils, GNU Inetutils}).

When @file{/dev/log} is unavailable, for instance because
@command{syslogd} is not running, as is the case during system startup
and shutdown, @command{shepherd} falls back to the Linux kernel
@dfn{ring buffer}, @file{/dev/kmsg}.  If @file{/dev/kmsg} is missing, as
is the case on other operating systems, it falls back to
@file{/dev/console}.

@item --pid[=@var{file}]
When @command{shepherd} is ready to accept connections, write its PID to @var{file} or
to the standard output if @var{file} is omitted.

@item -s @var{file}
@itemx --socket=@var{file}
@cindex socket special file
Receive further commands on the socket special file @var{file}.

@vindex XDG_RUNTIME_DIR
If this
option is not specified, @file{@var{localstatedir}/run/shepherd/socket} is
taken when running as @code{root}; when running as an unprivileged
user, @command{shepherd} listens to @file{/run/user/@var{uid}/shepherd/socket},
where @var{uid} is the user's numerical ID@footnote{On GNU/Linux, the
@file{/run/user/@var{uid}} directory is typically created by elogind or by
systemd, which are available in most distributions.}, or to
@file{$XDG_RUNTIME_DIR/shepherd} when the @code{XDG_RUNTIME_DIR}
environment variable is defined.

@quotation Note
If the listening socket is deleted, it becomes impossible to control
@command{shepherd}.  Thus, upon deletion, @command{shepherd} tries to
reopen the socket (when running as PID@tie{}1) or shuts down (when
running as an unprivileged user).

The latter behavior is useful when the listening socket is under
@file{/run/user/@var{uid}} (the default) since that directory is usually
deleted as soon as the user session terminates.
@end quotation

@item -S
@itemx --silent
Don't do output to stdout.

@item --quiet
Synonym for @code{--silent}.

@end table

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Invoking herd
@section Invoking @command{herd}

@cindex herd
The @command{herd} command is a generic client program to control a
running instance of @command{shepherd} (@pxref{Invoking shepherd}).
When running as root, it communicates with the @dfn{system
instance}---the process with PID@tie{}1; when running as a normal user,
it communicates with the @dfn{user's instance}, which is a regular,
unprivileged process managing the user's own services (this can be
changed by passing the @option{--socket} option or by setting the
@env{SHEPHERD_SOCKET} environment variable).  For example, the following
command displays the status of all the @dfn{system services}:

@example
sudo herd status
@end example

Conversely, the command below displays the status of @dfn{user
services}, assuming a user @command{shepherd} is running:

@example
herd status
@end example

The command has the following synopsis:

@example
herd [@var{option}@dots{}] @var{action} [@var{service} [@var{arg}@dots{}]]
@end example

It causes the @var{action} of the @var{service} to be invoked.  When
@var{service} is omitted and @var{action} is @code{status} or
@code{detailed-status}, the @code{root} service is used@footnote{This
shorthand does not work for other actions such as @code{stop}, because
inadvertently typing @code{herd stop} would stop all the services, which
could be pretty annoying.} (@pxref{The root Service}, for
more information on the @code{root} service.)

For each action, you should pass the appropriate @var{arg}s.  Actions
that are available for every service are @code{start}, @code{stop},
@code{restart}, @code{status}, @code{enable}, @code{disable}, and
@code{doc}.

If you pass a file name as an @var{arg}, it will be passed as-is to
the Shepherd, thus if it is not an absolute name, it is local to the current
working directory of @command{shepherd}, not to that of @command{herd}.

The @code{herd} command understands the following option:

@table @samp

@item -s @var{file}
@itemx --socket=@var{file}
Send commands to the socket special file @var{file}. If this option is
not specified, @file{@var{localstatedir}/run/shepherd/socket} is taken.

@item -n @var{number}
@itemx --log-history=@var{number}
Display up to @var{number} lines of the of @var{service} when running:

@example
herd status @var{service} -n @var{number}
@end example

By default up to ten lines are printed.
@end table

In addition, the options below are understood by service actions that
spawn other processes or services, such as @command{herd schedule timer}
(@pxref{timer-schedule-command, @code{timer-service}}) and @command{herd
spawn transient} (@pxref{Transient Service Maker}).

@table @samp

@item --user=@var{user}
@itemx --group=@var{group}
Run the process under the specified @var{user} and @var{group}.

@item -d @var{directory}
@itemx --working-directory=@var{directory}
Run the process or service from @var{directory}.

@item -E @var{environment}
@itemx --environment-variable=@var{environment}
Add @var{environment} to the environment variables of the service, where
@var{environment} has the form @code{@var{variable}=@var{value}}.

@item -N @var{name}
@itemx --service-name=@var{name}
Register the service under @var{name}.

@item --log-file=@var{file}
Log service output to @var{file}.

@end table

The @code{herd} command returns zero on success, and a non-zero exit
code on failure.  In particular, it returns a non-zero exit code when
@var{action} or @var{service} does not exist and when the given action
failed.


@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Invoking reboot
@section Invoking @command{reboot}

@cindex herd
The @command{reboot} command is a convenience client program to instruct
the Shepherd (when used as an init system) to stop all running services and
reboot the system.  It has the following synopsis:

@example
reboot [@var{option}@dots{}]
@end example

It is equivalent to running @command{herd stop shepherd}.  The
@code{reboot} command understands the following option:

@table @samp

@item -s @var{file}
@itemx --socket=@var{file}
Send commands to the socket special file @var{file}. If this option is
not specified, @file{@var{localstatedir}/run/shepherd/socket} is taken.

@cindex kexec, rebooting into a new Linux kernel
@cindex rebooting into a new Linux kernel with kexec
@item -k
@itemx --kexec
Reboot the system using Linux's kexec (this is equivalent to running
@command{herd kexec root}).  The kernel that was previously loaded using
the @command{kexec -l @var{file}} command is executed directly instead
of rebooting into the BIOS, keeping the downtime to a minimum.  See the
@uref{https://linux.die.net/man/8/kexec, @command{kexec} command
documentation} for more information.

This feature is only available on Linux-based systems.  It has no effect
on systems where kexec is unsupported or when no system was loaded for
eventual kexec reboot.  Last, if kexec reboot fails at run time,
@command{shepherd} falls back to normal reboot.

@end table

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Invoking halt
@section Invoking @command{halt}

@cindex herd
The @command{halt} command is a convenience client program to instruct
the Shepherd (when used as an init system) to stop all running services and turn
off the system.  It has the following synopsis:

@example
halt [@var{option}@dots{}]
@end example

It is equivalent to running @command{herd power-off shepherd}.  As
usual, the @code{halt} command understands the following option:

@table @samp

@item -s @var{file}
@itemx --socket=@var{file}
Send commands to the socket special file @var{file}. If this option is
not specified, @file{@var{localstatedir}/run/shepherd/socket} is taken.

@end table


@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Services
@chapter Services

@cindex service
@tindex <service>
The @dfn{service} is obviously a very important concept of the Shepherd.
On the Guile level, a service is represented as a record of type
@code{<service>}.  Each service has a number of properties that you
specify when you create it: how to start it, how to stop it, whether to
respawn it when it stops prematurely, and so on.  At run time, each
service has associated @dfn{state}: whether it is running or stopped,
what PID or other value is associated with it, and so on.

This section explains how to define services and how to query their
configuration and state using the Shepherd's programming interfaces.

@quotation Note
The programming interface defined in this section may only be used
within a @command{shepherd} process.  Examples where you may use it
include:

@itemize
@item
the @command{shepherd} configuration file (@pxref{Service Examples});
@item
as an argument to @command{herd eval root @dots{}} (@pxref{The root
Service});
@item
at the REPL (@pxref{REPL Service}).
@end itemize

These procedures may not be used in Guile processes other than
@command{shepherd} itself.
@end quotation

@menu
* Defining Services::           Defining services.
* Service Registry::            Mapping service names to records.
* Interacting with Services::   Accessing service state.
* Service De- and Constructors::  Commonly used ways of starting and
                                   stopping services.
* Timers::                      Timed services.
* The root Service::            The service that comes first.
* Legacy GOOPS Interface::      Former interface and how to migrate.
* Service Examples::            Examples that show how services are used.
* Managing User Services::      Running the Shepherd as a user.
@end menu

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Defining Services
@section Defining Services

A service is created by calling the @code{service} procedure, from the
@code{(shepherd service)} module (automatically visible from your
configuration file), as in this example:

@lisp
(service
  '(sshd ssh-daemon)  ;for convenience, give it two names
  #:start (make-forkexec-constructor
           '("/usr/sbin/sshd" "-D")
           #:pid-file "/etc/ssh/sshd.pid")
  #:stop (make-kill-destructor)
  #:respawn? #t)
@end lisp

The example above creates a service with two names, @code{sshd} and
@code{ssh-daemon}.  It is started by invoking @command{/usr/sbin/sshd},
and it is considered up and running as soon as its PID file
@file{/etc/ssh/sshd.pid} is available.  It is stopped by terminating the
@command{sshd} process.  Finally, should @command{sshd} terminate
prematurely, it is automatically respawned.  We will look at
@code{#:start} and @code{#:stop} later (@pxref{Service De- and
Constructors}), but first, here is the reference of the @code{service}
procedure and its optional keyword arguments.

@deffn {Procedure} service @var{provision} @
  [#:requirement '()] [#:one-shot? #f] [#:transient? #f] @
  [#:respawn? #f] [#:start (const #t)] [#:stop (const #f)] @
  [#:actions (actions)] @
  [#:termination-handler default-service-termination-handler] @
  [#:documentation #f]
@cindex canonical name of services
Return a new service with the given @var{provision}, a list of symbols
denoting what the service provides.  The first symbol in the list is the
@dfn{canonical name} of the service, thus it must be unique.

The meaning of keyword arguments is as follows:

@table @code
@item #:requirement
@code{#:requirement} is, like @var{provision}, a list of symbols that
specify services.  In this case, they name what this service depends on:
before the service can be started, services that provide those symbols
must be started.

Note that every name listed in @code{#:requirement} must be registered
so it can be resolved (@pxref{Service Registry}).

@item #:respawn?
@cindex respawning services
Specify whether the service should be respawned by @command{shepherd}.
If this slot has the value @code{#t}, then, assuming the service has an
associated process (its ``running value'' is a PID), restart the service
if that process terminates.

There is a limit to avoid endless respawning: when the service gets
respawned ``too fast'', it is @dfn{disabled}---see
@code{#:respawn-limit} below.

@cindex respawn delay
@item #:respawn-delay
Specify the delay before a service is respawned, in seconds (including a
fraction), for services marked with @code{#:respawn? #t}.  Its default
value is @code{(default-respawn-delay)} (@pxref{Service De- and
Constructors}).

@cindex respawn limit
@item #:respawn-limit
Specify the limit that prevents @command{shepherd} from respawning too
quickly the service marked with @code{#:respawn? #t}.  Its default value
is @code{(default-respawn-limit)} (@pxref{Service De- and
Constructors}).

The limit is expressed as a pair of integers: the first integer,
@var{n}, specifies a number of consecutive respawns and the second
integer, @var{t}, specifies a number of seconds.  If the service gets
respawned more than @var{n} times over a period of @var{t} seconds, it
is automatically @dfn{disabled} (@pxref{Interacting with Services,
@code{service-enabled?}}).  Once it is disabled, the service must be
explicitly re-enabled using @command{herd enable @var{service}} before
it can be started again.

Consider the service below:

@lisp
(service '(xyz)
         #:start (make-forkexec-constructor @dots{})
         #:stop (make-kill-destructor)
         #:respawn? #t
         #:respawn-limit '(3 . 5))
@end lisp

The effect is that this service will be respawned at most 3 times over a
period of 5 seconds; if its associated process terminates a fourth time
during that period, the service will be marked as disabled.

@item #:one-shot?
@cindex one-shot services
Whether the service is a @dfn{one-shot service}.  A one-shot service is
a service that, as soon as it has been successfully started, is marked
as ``stopped.''  Other services can nonetheless require one-shot
services.  One-shot services are useful to trigger an action before
other services are started, such as a cleanup or an initialization
action.

As for other services, the @code{start} method of a one-shot service must
return a truth value to indicate success, and false to indicate failure.

@item #:transient?
@cindex transient services
Whether the service is a @dfn{transient service}.  A transient service
is automatically unregistered when it terminates, be it because its
@code{stop} method is called or because its associated process
terminates.

This is useful in the uncommon case of synthesized services that may not
be restarted once they have completed.

@item #:start
@cindex constructor of a service
@cindex starting a service
Specify the @dfn{constructor} of the service, which will be called to
start the service.  This must be a procedure that accepts any number of
arguments; those arguments will be those supplied by the user, for
instance by passing them to @command{herd start}.  If the starting
attempt failed, it must return @code{#f} or throw an exception;
otherwise, the return value is stored as the @dfn{running value} of the
service.

@quotation Note
Constructors must terminate, successfully or not, in a timely fashion,
typically less than a minute.  Failing to do that, the service would
remain in ``starting'' state and users would be unable to stop it.
@end quotation

@xref{Service De- and Constructors}, for info on common service constructors.

@item #:stop
@cindex stopping a service
@cindex destructor of a service
This is the service @dfn{destructor}: a procedure of one or more
arguments that should stop the service.  It is called whenever the user
explicitly stops the service; its first argument is the running value of
the service, subsequent arguments are user-supplied.  Its return value
will again be stored as the running value, so it should
return @code{#f} if it is now possible again to start the service at a
later point.

@quotation Note
Destructors must also terminate in a timely fashion, typically less than
a minute.  Failing to do that, the service would remain in ``stopping''
state and users would be unable to stop it.
@end quotation

@xref{Service De- and Constructors}, for info on common service
destructors.

@item #:termination-handler
@cindex termination of a service's process
The procedure to call when the process associated with the service
terminates.  It is passed the service, the PID of the terminating
process, and its exit status, an integer as returned by @code{waitpid}
(@pxref{Processes, @code{waitpid},, guile, GNU Guile Reference Manual}).

The default handler is the @code{default-service-termination-handler}
procedure, which respawns the service if applicable.

@item #:actions
@cindex actions of services
@cindex service actions
The additional actions that can be performed on the service when it is
running.  A typical example for this is the @code{restart} action.  The
@code{actions} macro can be used to defined actions (see below).
@end table
@end deffn

A special service that every other service implicitly depends on is the
@code{root} (also known as @code{shepherd}) service.  @xref{The root
Service}, for more information.

@cindex graph of services
Services and their dependencies form a @dfn{graph}.  At the
command-line, you can view that export a representation of that graph
that can be consumed by any application that understands the Graphviz
format, such as @uref{https://github.com/jrfonseca/xdot.py, xdot}:

@example
herd graph | xdot -
@end example

Service actions are defined using the @code{action} procedure or the
@code{actions} (plural) macro, as shown below.

@deffn {Procedure} action @var{name} @var{proc} [@var{doc}]
Return a new action with the given @var{name}, a symbol, that executes
@var{proc}, a one-argument procedure that is passed the service's running
value.  Use @var{doc} as the documentation of that action.
@end deffn

@defmac actions (name proc) @dots{}
Create a value for the @code{#:actions} parameter of @code{service}.
Each @var{name} is a symbol and each @var{proc} the corresponding
procedure that will be called to perform the action.  A @var{proc} has
one argument, which will be the running value of the service.
@end defmac

Naturally, the @code{(shepherd service)} provides procedures to access
this information for a given service object:

@deffn {Procedure} service-provision @var{service}
Return the symbols provided by @var{service}.
@end deffn

@deffn {Procedure} service-canonical-name @var{service}
Return the @dfn{canonical name} of @var{service}, which is the first
element of the list returned by @code{service-provision}.
@end deffn

@deffn {Procedure} service-requirement @var{service}
Return the list of services required by @var{service} as a list of
symbols.
@end deffn

@deffn {Procedure} one-shot-service? @var{service}
@deffnx {Procedure} transient-service? @var{service}
Return true if @var{service} is a one-shot/transient service.
@end deffn

@deffn {Procedure} respawn-service? @var{service}
Return true if @var{service} is meant to be respawned if its associated
process terminates prematurely.
@end deffn

@deffn {Procedure} service-respawn-delay @var{service}
Return the respawn delay of @var{service}, in seconds (an integer or a
fraction or inexact number).  See @code{#:respawn-delay} above.
@end deffn

@deffn {Procedure} service-respawn-limit @var{service}
Return the respawn limit of @var{service}, expressed as a pair---see
@code{#:respawn-limit} above.
@end deffn

@deffn {Procedure} service-documentation @var{service}
Return the documentation (a string) of @var{service}.
@end deffn


@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Service Registry
@section Service Registry

@cindex service registry
At run time, @command{shepherd} maintains a @dfn{service registry} that
maps service names to service records.  Service dependencies are
expressed as a list of names passed as @code{#:requirement} to the
@code{service} procedure (@pxref{Defining Services}); these names are
looked up in the registry.  Likewise, when running @code{herd start
sshd} or similar commands (@pxref{Jump Start}), the service name is
looked up in the registry.

Consequently, every service must be appear in the registry before it can
be used.  A typical configuration file thus includes a call to the
@code{register-services} procedure (@pxref{Service Examples}).  The
following procedures let you interact with the registry.

@deffn {Procedure} register-services @var{services}
Register @var{services} so that they can be looked up by name, for instance
when resolving dependencies.

Each name uniquely identifies one service.  If a service with a given name has
already been registered, arrange to have it replaced when it is next stopped.
If it is currently stopped, replace it immediately.
@end deffn

@deffn {Procedure} unregister-services @var{services}
Remove all of @var{services} from the registry, stopping them if they are not
already stopped.
@end deffn

@deffn {Procedure} lookup-service @var{name}
Return the service that provides @var{name}, @code{#f} if there is none.
@end deffn

@deffn {Procedure} for-each-service @var{proc}
Call @var{proc}, a procedure taking one argument, once for each
registered service.
@end deffn

@deffn {Procedure} lookup-running @var{name}
Return the running service that provides @var{name}, or false if none.
@end deffn


@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Interacting with Services
@section Interacting with Services

What we have seen so far is the interface to @emph{define} a service and
to access it (@pxref{Defining Services}).  The procedures below, also
exported by the @code{(shepherd service)} module, let you modify and
access the state of a service.  You may use them in your configuration
file, for instance to start some or all of the services you defined
(@pxref{Service Examples}).

Under the hood, each service record has an associated @dfn{fiber}
(really: an actor) that encapsulates its state and serves user
requests---a fiber is a lightweight execution thread (@pxref{Service
Internals}).

The procedures below let you change the state of a service.

@deffn {Procedure} start-service @var{service} . @var{args}
Start @var{service} and its dependencies, passing @var{args} to its
@code{start} method.  Return its running value, @code{#f} on failure.
@end deffn

@deffn {Procedure} stop-service @var{service} . @var{args}
Stop @var{service} and any service that depends on it.  Return the list of
services that have been stopped (including transitive dependent services).

If @var{service} is not running, print a warning and return its canonical name
in a list.
@end deffn

@deffn {Procedure} perform-service-action @var{service} @var{the-action} . @var{args}
Perform @var{the-action} (a symbol such as @code{'restart} or @code{'status})
on @var{service}, passing it @var{args}.  The meaning of @var{args} depends on
the action.
@end deffn

The @code{start-in-the-background} procedure, described below, is
provided for your convenience: it makes it easy to start a set of
services right from your configuration file, while letting
@command{shepherd} run in the background.

@deffn {Procedure} start-in-the-background @var{services}
Start the services named by @var{services}, a list of symbols, in the
background.  In other words, this procedure returns immediately without
waiting until all of @var{services} have been started.

This procedure can be useful in a configuration file because it lets you
interact right away with shepherd using the @command{herd} command.
@end deffn

The following procedures let you query the current state of a service.

@deffn {Procedure} service-running? @var{service}
@deffnx {Procedure} service-stopped? @var{service}
@deffnx {Procedure} service-enabled? @var{service}
Return true if @var{service} is currently running/stopped/enabled, false
otherwise.
@end deffn

@deffn {Procedure} service-status @var{service}
Return the status of @var{service} as a symbol, one of: @code{'stopped},
@code{'starting}, @code{'running}, or @code{'stopping}.
@end deffn

@deffn {Procedure} service-running-value @var{service}
Return the current ``running value'' of @var{service}---a Scheme value
associated with it.  It is @code{#f} when the service is stopped;
otherwise, it is a truth value, such as an integer denoting a PID
(@pxref{Service De- and Constructors}).
@end deffn

@deffn {Procedure} service-status-changes @var{service}
Return the list of symbol/timestamp pairs representing recent state
changes for @var{service}.
@end deffn

@deffn {Procedure} service-startup-failures @var{service}
@deffnx {Procedure} service-respawn-times @var{service}
Return the list of startup failure times or respawn times of
@var{service}.
@end deffn

@deffn {Procedure} service-process-exit-statuses @var{services}
Return the list of last exit statuses of @var{service}'s main process
(most recent first).
@end deffn

@cindex replacement, or a service
@deffn {Procedure} service-replacement @var{service}
Return the @dfn{replacement} of @var{service}, or @code{#f} if there is
none.

The replacement is the service that will replace @var{service} when it
is eventually stopped.
@end deffn

@deffn {Procedure} service-recent-messages @var{service}
Return a list of messages recently logged by @var{service}---typically
lines written by a daemon on standard output.  Each element of the list
is a timestamp/string pair where the timestamp is the number of seconds
since January 1st, 1970 (an integer).
@end deffn

@deffn {Procedure} service-log-file @var{service}
Return the file where messages by @var{service} are logged, or @code{#f}
if there is no such file, for instance because @var{service}'s output is
logged by some mechanism not under shepherd's control.
@end deffn

@xref{Service Internals}, if you're curious about the nitty-gritty
details!


@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Service De- and Constructors
@section Service De- and Constructors

@cindex generating constructors
@cindex generating destructors
@cindex constructors, generation of
@cindex destructors, generation of
Each service has a @code{start} procedure and a @code{stop} procedure,
also referred to as its @dfn{constructor} and @dfn{destructor}
(@pxref{Services}).  The procedures listed below return procedures that
may be used as service constructors and destructors.  They are flexible
enough to cover most use cases and carefully written to complete in a
timely fashion.

@deffn {Procedure} make-forkexec-constructor @var{command} @
  [#:user #f] @
  [#:group #f] @
  [#:supplementary-groups '()] @
  [#:pid-file #f] [#:pid-file-timeout (default-pid-file-timeout)] @
  [#:input-port #f] @
  [#:log-file #f] @
  [#:directory (default-service-directory)] @
  [#:file-creation-mask #f] [#:create-session? #t] @
  [#:resource-limits '()] @
  [#:environment-variables (default-environment-variables)]
Return a procedure that forks a child process, closes all file
descriptors except the standard output and standard error descriptors,
sets the current directory to @var{directory}, sets the umask to
@var{file-creation-mask} unless it is @code{#f}, changes the environment
to @var{environment-variables} (using the @code{environ} procedure),
sets the current user to @var{user} the current group to @var{group}
unless they are @code{#f} and supplementary groups to
@var{supplementary-groups} unless they are @code{'()}, and executes
@var{command} (a list of strings.)  When
@var{input-port} is set, it refers to a port that is used as the standard input
of the created child process (@file{/dev/null} is used by default).  When
@var{create-session?} is true, the child process creates a new session with
@code{setsid} and becomes its leader.  The result of the procedure will be the
@code{<process>} record representing the child process.

@quotation Note
This will not work as expected if the process ``daemonizes'' (forks); in
that case, you will need to pass @code{#:pid-file}, as explained below.
@end quotation

When @var{pid-file} is true, it must be the name of a PID file
associated with the process being launched; the return value is the PID
once that file has been created.  If @var{pid-file} does not show up in less
than @var{pid-file-timeout} seconds, the service is considered as failing to
start.

When @var{log-file} is true, it names the file to which the service's
standard output and standard error are redirected.  @var{log-file} is
created if it does not exist, otherwise it is appended to.

@quotation Note
@xref{Log Rotation Service}, for a service to rotate log files specified
via the @code{#:log-file} parameter.
@end quotation

Guile's @code{setrlimit} procedure is applied on the entries in
@var{resource-limits}.  For example, a valid value would be:

@lisp
'((nproc 10 100)         ;number of processes
  (nofile 4096 4096))    ;number of open file descriptors
@end lisp
@end deffn

@deffn {Procedure} make-kill-destructor [@var{signal}] @
  [#:grace-period (default-process-termination-grace-period)]
Return a procedure that sends @var{signal} to the process group of the PID
given as argument, where @var{signal} defaults to @code{SIGTERM}.  If the
process is still running after @var{grace-period} seconds, send it
@code{SIGKILL}.  The procedure returns once the process has terminated.

This @emph{does} work together with respawning services,
because in that case the @code{stop} method of the @code{<service>}
arranges so that the service is not respawned.
@end deffn

@anchor{exec-command}
The @code{make-forkexec-constructor} procedure builds upon the following
procedures.

@deffn {Procedure} exec-command @var{command} @
  [#:user #f] @
  [#:group #f] @
  [#:supplementary-groups '()] @
  [#:log-file #f] [#:log-port #f] [#:input-port #f] @
  [#:directory (default-service-directory)] @
  [#:file-creation-mask #f] [#:create-session? #t] @
  [#:resource-limits '()] @
  [#:environment-variables (default-environment-variables)]
@deffnx {Procedure} fork+exec-command @var{command} @
  [#:user #f] @
  [#:group #f] @
  [#:supplementary-groups '()] @
  [#:input-port #f] @
  [#:log-file #f] [#:log-encoding "UTF-8"] @
  [#:directory (default-service-directory)] @
  [#:file-creation-mask #f] [#:create-session? #t] @
  [#:resource-limits '()] @
  [#:environment-variables (default-environment-variables)]
Run @var{command} as the current process from @var{directory}, with
@var{file-creation-mask} if it's true, with @var{rlimits}, and with
@var{environment-variables} (a list of strings like @code{"PATH=/bin"}.)
File descriptors 1 and 2 are kept as is or redirected to
either @var{log-port} or @var{log-file}
if it's true, whereas file descriptor 0
(standard input) points to @var{input-port} or @file{/dev/null}; all other file descriptors
are closed prior to yielding control to @var{command}.  When
@var{create-session?} is true, call @code{setsid} first
(@pxref{Processes, @code{setsid},, guile, GNU Guile Reference Manual}).

By default, @var{command} is run as the current user.  If the @var{user}
keyword argument is present and not false, change to @var{user}
immediately before invoking @var{command}.  @var{user} may be a string,
indicating a user name, or a number, indicating a user ID.  Likewise,
@var{command} will be run under the current group, unless the
@var{group} keyword argument is present and not false, and
@var{supplementary-groups} is not @code{'()}.

@code{fork+exec-command} does the same as @code{exec-command}, but in
a separate process whose PID it returns.

@quotation Warning
The @command{shepherd}'s main even loop is responsible for calling
@code{waitpid} to reap processes that have completed.  User code
@emph{must not} call @code{waitpid} as that would compete with
@code{waitpid} calls made by the main event loop, possibly leading to
deadlocks.

Use @code{system*} or @code{spawn-command} when you need to spawn a
command and wait for its exit status.  @ref{Process Utilities}.
@end quotation
@end deffn

@defvar default-environment-variables
This parameter (@pxref{Parameters,,, guile, GNU Guile Reference Manual})
specifies the default list of environment variables to be defined when
the procedures above create a new process.

It must be a list of strings where each string has the format
@code{@var{name}=@var{value}}.  It defaults to what @code{environ}
returns when the program starts (@pxref{Runtime Environment,
@code{environ},, guile, GNU Guile Reference Manual}).
@end defvar

@deffn {Procedure} user-environment-variables @
  [name-or-id (getuid)] @
  [environment-variables (default-environment-variables)]

Take the list @var{environment-variables}, replace @env{HOME} with home
directory of the user and @env{USER} with the name of the user and
return the result.
@end deffn

@defvar default-pid-file-timeout
This parameter (@pxref{Parameters,,, guile, GNU Guile Reference Manual})
specifies the default PID file timeout in seconds, when
@code{#:pid-file} is used (see above).  It defaults to 5 seconds.
@end defvar

@defvar default-process-termination-grace-period
This parameter (@pxref{Parameters,,, guile, GNU Guile Reference Manual})
specifies the ``grace period'' (in seconds) after which a process that
has been sent @code{SIGTERM} or some other signal to gracefully exit is
sent @code{SIGKILL} for immediate termination.  It defaults to 5
seconds.
@end defvar

@cindex respawn delay
@defvar default-respawn-delay
This parameter specifies the default value of the @code{#:respawn-delay}
parameter of @code{service} (@pxref{Defining Services}).  It defaults to
@code{0.1}, meaning a 100ms delay before respawning a service.
@end defvar

@cindex respawn limit
@cindex disabled service
@defvar default-respawn-limit
This parameter specifies the default value of the @code{#:respawn-limit}
parameter of @code{service} (@pxref{Defining Services}).

As an example, suppose you add this line to your configuration file:

@lisp
(default-respawn-limit '(3 . 10))
@end lisp

The effect is that services will be respawned at most 3 times over a
period of 10 seconds before being disabled.
@end defvar

@cindex on-demand, starting services
@cindex inetd-style services
One may also define services meant to be started @emph{on demand}.  In
that case, shepherd listens for incoming connections on behalf of the
program that handles them; when it accepts an incoming connection, it
starts the program to handle them.  The main benefit is that such
services do not consume resources until they are actually used, and they
do not slow down startup.

These services are implemented following the protocol of the venerable
inetd ``super server'' (@pxref{inetd invocation, inetd,, inetutils, GNU
Inetutils}).  Many network daemons can be invoked in ``inetd mode'';
this is the case, for instance, of @command{sshd}, the secure shell
server of the OpenSSH project.  The Shepherd lets you define inetd-style
services, specifically those in @code{nowait} mode where the daemon is
passed the newly-accepted socket connection while @command{shepherd} is
in charge of listening.

@anchor{endpoints}
@cindex endpoints, for inetd services
@cindex endpoints, for systemd services
Listening endpoints for such services are described as records built
using the @code{endpoint} procedure provided by the @code{(shepherd
endpoints)} module:

@deffn {Procedure} endpoint @var{address} [#:name "unknown"] @
  [#:style SOCK_STREAM] [#:backlog 128] @
  [#:socket-owner (getuid)] [#:socket-group (getgid)] @
  [#:socket-directory-permissions #o755] @
  [#:bind-attempts (default-bind-attempts)]
Return a new endpoint called @var{name} of @var{address}, an address as
return by @code{make-socket-address}, with the given @var{style} and
@var{backlog}.

When @var{address} is of type @code{AF_INET6}, the endpoint is
@emph{IPv6-only}.  Thus, if you want a service available both on IPv4
and IPv6, you need two endpoints.  For example, below is a list of
endpoints to listen on port 4444 on all the network interfaces, both in
IPv4 and IPv6 (``0.0.0.0'' for IPv4 and ``::0'' for IPv6):

@lisp
(list (endpoint (make-socket-address AF_INET INADDR_ANY 4444))
      (endpoint (make-socket-address AF_INET6 IN6ADDR_ANY 4444)))
@end lisp

This is the list you would pass to @code{make-inetd-constructor} or
@code{make-systemd-constructor}---see below.

When @var{address} is of type @code{AF_UNIX}, @var{socket-owner} and
@var{socket-group} are strings or integers that specify its ownership and that
of its parent directory; @var{socket-directory-permissions} specifies the
permissions for its parent directory.

Upon @samp{EADDRINUSE} (``Address already in use''), up to @var{bind-attempts}
attempts will be made to @code{bind} on @var{address}, one every second.
@end deffn

@defvar default-bind-attempts
This parameter specifies the number of times, by default, that
@command{shepherd} will try to bind an endpoint address if it happens to
be already in use.
@end defvar

The inetd service constructor takes a command and a list of such
endpoints:

@deffn {Procedure} make-inetd-constructor @var{command} @var{endpoints} @
  [#:service-name-stem _] [#:requirements '()] @
  [#:max-connections (default-inetd-max-connections)] @
  [#:user #f] @
  [#:group #f] @
  [#:supplementary-groups '()] @
  [#:directory (default-service-directory)] @
  [#:file-creation-mask #f] [#:create-session? #t] @
  [#:resource-limits '()] @
  [#:environment-variables (default-environment-variables)]
Return a procedure that opens sockets listening to @var{endpoints}, a list
of objects as returned by @code{endpoint}, and accepting connections in the
background.

Upon a client connection, a transient service running @var{command} is
spawned.  Only up to @var{max-connections} simultaneous connections are
accepted; when that threshold is reached, new connections are immediately
closed.

The remaining arguments are as for @code{make-forkexec-constructor}.
@end deffn

@deffn {Procedure} make-inetd-destructor
Return a procedure that terminates an inetd service.
@end deffn

@cindex systemd-style services
@cindex on-demand, starting services
@cindex socket activation, starting services
@cindex starting services, via socket activation
The last type is @dfn{systemd-style services}.  Like inetd-style
services, those are started on demand when an incoming connection
arrives, but using the protocol devised by the systemd service manager
and referred to as
@uref{https://www.freedesktop.org/software/systemd/man/daemon.html#Socket-Based%20Activation,
@dfn{socket activation}}.  The main difference with inetd-style services
is that shepherd hands over the listening socket(s) to the daemon; the
daemon is then responsible for accepting incoming connections.  A
handful of environment variables are set in the daemon's execution
environment (see below), which usually checks them using the libsystemd
or libelogind
@uref{https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html,
client library helper functions}.
The constructor and destructor for systemd-style daemons are described
below.

@deffn {Procedure} make-systemd-constructor @var{command} @var{endpoints} @
  [#:lazy-start? #t] @
  [#:user #f] @
  [#:group #f] @
  [#:supplementary-groups '()] @
  [#:log-file #f] @
  [#:directory (default-service-directory)] @
  [#:file-creation-mask #f] [#:create-session? #t] @
  [#:resource-limits '()] @
  [#:environment-variables (default-environment-variables)]
Return a procedure that starts @var{command}, a program and list of
argument, as a systemd-style service listening on @var{endpoints}, a list of
@code{<endpoint>} objects.

@var{command} is started on demand on the first connection attempt on one of
@var{endpoints} when @var{lazy-start?} is true; otherwise it is started as
soon as possible.  It is passed the listening sockets for @var{endpoints} in
file descriptors 3 and above; as such, it is equivalent to an @code{Accept=no}
@uref{https://www.freedesktop.org/software/systemd/man/systemd.socket.html,systemd
socket unit}.  The following environment variables are set in its environment:

@table @env
@item LISTEN_PID
It is set to the PID of the newly spawned process.

@item LISTEN_FDS
It contains the number of sockets available starting from file descriptor
3---i.e., the length of @var{endpoints}.

@item LISTEN_FDNAMES
The colon-separated list of endpoint names.
@end table

This must be paired with @code{make-systemd-destructor}.
@end deffn

@deffn {Procedure} make-systemd-destructor
Return a procedure that terminates a systemd-style service as created by
@code{make-systemd-constructor}.
@end deffn

The following constructor/destructor pair is also available for your
convenience, but we recommend using @code{make-forkexec-constructor} and
@code{make-kill-destructor} instead (this is typically more robust than
going through the shell):

@deffn {Procedure} make-system-constructor @var{command}@dots{}
The returned procedure will execute @var{command} in a shell and
return @code{#t} if execution was successful, otherwise @code{#f}.
For convenience, it takes multiple arguments which will be
concatenated first.
@end deffn

@deffn {Procedure} make-system-destructor @var{command}@dots{}
Similar to @code{make-system-constructor}, but returns @code{#f} if
execution of the @var{command} was successful, @code{#t} if not.
@end deffn

@xref{Timers}, for other service constructors and destructors: those for
timers.

@node Timers
@section Timers

@cindex timers (timed services)
@cindex timed services (timers)
@cindex periodic services
In addition to process, inetd-style, and systemd-style services
discussed before, another type of service is available: @dfn{timers}.

Timers, you guessed it, execute a command or call a procedure
periodically.  They work like other services: they can be started,
stopped, unloaded, and so on.  The constructor and destructor shown
below let you define a timer; they are exported by @code{(shepherd
service timer)}, so make sure to add a line like this to your
configuration file if you'd like to use them:

@lisp
(use-modules (shepherd service timer))
@end lisp

@cindex calendar events, for timers
@cindex timer periodicity
Timers fire on calendar events: ``every Sunday at noon'', ``everyday at
11AM and 8PM'', ``on the first of each month'', etc@.  If you ever used
cron, mcron, or systemd timers, this is similar.  Calendar events are
specified using the @code{calendar-event} procedure, which defines
@emph{constraints} on each calendar component: months, days of week,
hours, minutes, and so on.  Here are a few examples:

@lisp
;; Every Sunday and every Wednesday, at noon.
(calendar-event #:hours '(12) #:minutes '(0)
                #:days-of-week '(sunday wednesday))

;; Everyday at 11AM and 8PM.
(calendar-event #:hours '(11 20) #:minutes '(0))

;; At 9:10AM on the first and fifteenth of each month.
(calendar-event #:days-of-month '(1 15)
                #:hours '(9) #:minutes '(10))
@end lisp

@deffn {Procedure} calendar-event [#:months any-month] @
  [#:days-of-month any-day] [#:days-of-week any-day-of-week] @
  [#:hours any-hour] [#:minutes any-minute] @
  [#:seconds '(0)]
Return a calendar event that obeys the given constraints.  Raise an
error if one of the values is out of range.

All the arguments are lists of integers as commonly used in the
Gregorian calendar, except for @code{days-of-week} which is a list of
symbols denoting weekdays: @code{'monday}, @code{'tuesday}, etc.

@quotation Note
@cindex daylight saving time (DST), for timers
All calendar events are understood in the timezone they occur in, should
daylight saving time (DST) changes occur between consecutive events.

For example, in Western and Central Europe, the waiting time between two
consecutive daily events is 24 hours except on two occasions: it is 23
hours when switching from ``normal'' time (CET, or UTC+1) to summer time
(CEST, or UTC+2), and it is 25 hours when switching from summer time
to ``normal'' time.
@end quotation
@end deffn

@cindex Vixie cron, converting date specifications
@cindex cron, converting date specifications
Users familiar with the venerable Vixie cron can instead convert
cron-style date specifications to a calendar event data structure using
the @code{cron-string->calendar-event} procedure described below.

@deffn {Procedure} cron-string->calendar-event @var{str}
Convert @var{str}, which contains a Vixie cron date line, into the
corresponding @code{calendar-event}.  Raise an error if @var{str} is invalid.

A valid cron date line consists of 5 space-separated fields: minute, hour, day
of month, month, and day of week.  Each field can be an integer, or a
comma-separate list of integers, or a range.  Ranges are represented by two
integers separated by a hyphen, optionally followed by slash and a number of
repetitions.  Here are examples:

@table @code
@item 30 4 1,15 * *
4:30AM on the 1st and 15th of each month;
@item 5 0 * * *
five minutes after midnight, every day;
@item 23 0-23/2 * * 1-5
23 minutes after the hour every two hour, on weekdays.
@end table
@end deffn

To create a timer, you create a service with the procedures described
below as its @code{start} and @code{stop} methods (@pxref{Defining
Services}).

@deffn {Procedure} make-timer-constructor @var{event} @var{action} @
  [#:occurrences +inf.0] [#:log-file #f] [#:max-duration #f] @
  [#:wait-for-termination? #f]
Return a procedure for use as the @code{start} method of a service.  The
procedure will perform @var{action} for @var{occurrences} iterations of
@code{event}, a calendar event as returned by @code{calendar-event}.
@var{action} may be either a command (returned by @code{command}) or a thunk;
in the latter case, the thunk must be suspendable or it could block the whole
shepherd process.

When @var{log-file} is true, log the output of @var{action} to that file
rather than in the global shepherd log.

When @var{wait-for-termination?} is true, wait until @var{action} has finished
before considering executing it again; otherwise, perform @var{action}
strictly on every occurrence of @var{event}, at the risk of having multiple
instances running concurrently.

When @var{max-duration} is true, it is the maximum duration in seconds that a
run may last, provided @var{action} is a command.  Past @var{max-duration}
seconds, the timer's process is forcefully terminated with signal
@var{termination-signal}.
@end deffn

@deffn {Procedure} make-timer-destructor
Return a procedure for the @code{stop} method of a service whose
constructor was given by @code{make-timer-constructor}.
@end deffn

As we have seen above, @code{make-timer-constructor} can be passed a
command to execute.  Those are specified using the @code{command}
procedure below.

@deffn {Procedure} command @var{line} @
  [#:user #f] [#:group #f] [#:supplementary-groups '()] @
  [#:environment-variables (default-environment-variables)] @
  [#:directory (default-service-directory)] @
  [#:resource-limits '()]
Return a new command for @var{line}, a program name and argument list, to be
executed as @var{user} and @var{group}, with the given
@var{environment-variables}, in @var{directory}, and with the given
@var{resource-limits}.

These arguments are the same as for @code{fork+exec-command} and related
procedures (@pxref{exec-command, @code{fork+exec-command}}).
@end deffn

It's also possible to add a @code{trigger} action to timer
services, such that one can trigger it with:

@example
herd trigger @var{service}
@end example

To do that, you would add the predefined @code{timer-trigger-action} to
the service's @code{actions} field (@pxref{Defining Services}).

@defvar timer-trigger-action
This is the @code{trigger} service action.  When invoked, its effect is
to invoke the action passed to @code{make-timer-constructor}.
@end defvar

@xref{timer-example, timer example}, to see how to put it all together.

Last, this module also provides @code{timer-service}, a sort of ``meta''
timer service that lets you dynamically create timed services from the
command line.  @xref{Timer Service}.

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node The root Service
@section The @code{root} Service

@cindex root service
@cindex special services
The service @code{root} is special, because it is used to control the
Shepherd itself.  It has an alias @code{shepherd}.  It provides the
following actions (in addition to @code{enable}, @code{disable} and
@code{restart} which do not make sense here).

@table @code
@item status
Displays which services are started and which ones are not.

@item detailed-status
Displays detailed information about every registered service.

@cindex evaluating code in shepherd
@item load @var{file}
Evaluate in the @command{shepherd} process the Scheme code in
@var{file}, in a fresh module that uses the @code{(shepherd services)}
module---as with the @code{--config} option of @command{shepherd}
(@pxref{Invoking shepherd}).

@item eval @var{exp}
Likewise, evaluate Scheme expression @var{exp} in a fresh module with
all the necessary bindings.  Here is a couple of examples:

@example
# herd eval root "(+ 2 2)"
4
# herd eval root "(getpid)"
1
# herd eval root "(lookup-running 'xorg-server)"
(service (version 0) (provides (xorg-server)) @dots{})
@end example

@item unload @var{service-name}
Attempt to remove the service identified by @var{service-name}.
@command{shepherd} will first stop the service, if necessary, and then
remove it from the list of registered services.  Any services
depending upon @var{service-name} will be stopped as part of this
process.

If @var{service-name} simply does not exist, output a warning and do
nothing.  If it exists, but is provided by several services, output a
warning and do nothing.  This latter case might occur for instance with
the fictional service @code{web-server}, which might be provided by both
@code{apache} and @code{nginx}.  If @var{service-name} is the special
string and @code{all}, attempt to remove all services except for the Shepherd
itself.

@item reload @var{file-name}
Unload all known optional services using unload's @code{all} option,
then load @var{file-name} using @code{load} functionality.  If
file-name does not exist or @code{load} encounters an error, you may
end up with no defined services.  As these can be reloaded at a later
stage this is not considered a problem.  If the @code{unload} stage
fails, @code{reload} will not attempt to load @var{file-name}.

@item daemonize
Fork and go into the background.  This should be called before
respawnable services are started, as otherwise we would not get the
@code{SIGCHLD} signals when they terminate.

@item kexec
On GNU/Linux, reboot straight into a new Linux kernel previously loaded
with the @command{kexec -l @var{file}} command.  This is the action
invoked by the @command{reboot -k} command.  @xref{Invoking reboot}.

@end table

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Legacy GOOPS Interface
@section Legacy GOOPS Interface

@cindex GOOPS, former interface
From its inception in 2002 with negative version numbers (really!)@: up
to version 0.9.x included, the Shepherd's service interface used GOOPS,
Guile's object-oriented programming system (@pxref{GOOPS,,, guile, GNU
Guile Reference Manual}).  This interface was deprecated in 0.10.0 and
removed in 1.0.0.  The remainder of this section describes that
interface and how to migrate from it.

The GOOPSy interface provided
a @code{<service>} class whose instances you could access
directly with @code{slot-ref}; generic functions such as @code{start}
and @code{stop} had one method accepting a service object and another
method accepting the name of a service as a symbol, which it would
transparently resolve.

Fortunately, common idioms are easily converted to the current
interface.  For example, you would previously create a service like so:

@tindex <service>
@lisp
(make <service>       ;former GOOPS interface
  #:provides '(something)
  #:requires '(another thing)
  #:start @dots{}
  #:stop @dots{}
  #:respawn? #t)
@end lisp

With the new interface (@pxref{Defining Services}), you would write
something very similar:

@lisp
(service '(something)
  #:requirement '(another thing)
  #:start @dots{}
  #:stop @dots{}
  #:respawn? #t)
@end lisp

@findex start
@findex stop
Likewise, instead of writing:

@lisp
(start 'whatever)
@end lisp

@noindent
... you would write:

@lisp
(start-service (lookup-service 'whatever))
@end lisp

If in doubt, please get in touch with us at @email{help-guix@@gnu.org}.

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Service Examples
@section Service Examples

@cindex configuration file, examples
The configuration file of the @command{shepherd} command
(@pxref{Invoking shepherd}) defines, registers, and possibly starts
@dfn{services}.  Each service specifies other services it depends on and
how it is started and stopped.  The configuration file contains Scheme
code that uses the programming interface of the @code{(shepherd
service)} module (@pxref{Services}).

Let's assume you want to define and register a service to start mcron,
the daemon that periodically executes jobs in the background
(@pxref{Introduction,,, mcron, GNU mcron Manual}).  That service is
started by invoking the @command{mcron} command, after which
@command{shepherd} should monitor the running process, possibly
re-spawning it if it stops unexpectedly.  Here's the configuration file
for this one service:

@lisp
(define mcron
  (service
    '(mcron)
    ;; Run /usr/bin/mcron without any command-line arguments.
    #:start (make-forkexec-constructor '("/usr/bin/mcron"))
    #:stop (make-kill-destructor)
    #:respawn? #t))

(register-services (list mcron))
@end lisp

You can write the snippet above in the default configuration
file---@file{~/.config/shepherd/init.scm} if you run @command{shepherd}
as an unprivileged user.  When you launch it, @command{shepherd} will
evaluate that configuration; thus it will define and register the
@code{mcron} service, but it will @emph{not} start it.  To start the
service, run:

@example
herd start mcron
@end example

Alternatively, if you want @code{mcron} to be started automatically when
@command{shepherd} starts, you can add this snippet at the end of the
configuration file:

@lisp
(start-in-the-background '(mcron))
@end lisp

Now let's take another example: @command{sshd}, the secure shell daemon
of @uref{https://www.openssh.com,the OpenSSH project}.  We will pass
@command{sshd} the @option{-D} option so that it does not ``detach'',
making it easy for @command{shepherd} to monitor its process; we also
tell @command{shepherd} to check its @dfn{PID file} to determine once it
has started and is ready to accept connections:

@lisp
(define sshd
  (service
    '(sshd ssh-daemon)  ;for convenience, give it two names
    #:start (make-forkexec-constructor
             '("/usr/sbin/sshd" "-D")
             #:pid-file "/etc/ssh/sshd.pid")
    #:stop (make-kill-destructor)
    #:respawn? #t))

(register-services (list sshd))
(start-in-the-background '(sshd))
@end lisp

@cindex inetd mode, example
Alternatively, we can start @command{sshd} in @dfn{inetd mode}: in that
case, @command{shepherd} listens for connection and spawns
@command{sshd} only upon incoming connections.  The inetd mode is
enabled by passing the @option{-i} command-line option:

@lisp
(define sshd
  (service
    '(sshd ssh-daemon)
    #:start (make-inetd-constructor
             '("/usr/sbin/sshd" "-D" "-i")
             (list (endpoint
                    (make-socket-address AF_INET INADDR_ANY 22))
                   (endpoint
                    (make-socket-address AF_INET6 IN6ADDR_ANY 22)))
             #:max-connections 10)
    #:stop (make-inetd-destructor)
    #:respawn? #t))

(register-services (list sshd))
(start-in-the-background '(sshd))
@end lisp

The @code{make-socket-address} procedure calls above return the
@dfn{listening addresses} (@pxref{Network Socket Address,,, guile, GNU
Guile Reference Manual}).  In this case, it specifies that
@command{shepherd} will accept connections coming from any network
interface (``0.0.0.0'' in IPv4 notation and ``::0'' for IPv6) on port
22.  The @code{endpoint} calls wrap these addresses in endpoint records
(@pxref{endpoints}).  When a client connects, @command{shepherd} accepts
it and spawns @command{sshd -D -i} as a new @dfn{transient service},
passing it the client connection.  The @code{#:max-connections}
parameter instructs @command{shepherd} to accept at most 10 simultaneous
client connections.

@anchor{system-star-example}
@cindex nginx, service example
The example below---a service for the @url{https://nginx.org,nginx} web
server---illustrates a situation where a service constructor and
destructor work by invoking a program with @code{system*} instead of the
usual @code{make-forkexec-constructor} and @code{make-kill-destructor}.

@lisp
(service
  '(nginx)
  #:start (lambda ()
            ;; The 'nginx' command daemonizes and returns immediately;
            ;; it eventually writes a PID file once successfully started.
            (and (zero? (system* "/usr/sbin/nginx"))
                 (read-pid-file "/var/run/nginx/pid")))
  #:stop (lambda _
           ;; Stop the nginx daemon by running this command.
           (unless (zero? (system* "/usr/sbin/nginx" "-s" "stop"))
             (error "failed to stop nginx"))
           #f))
@end lisp

@xref{Process Utilities}, for more on @code{system*} and related process
helpers.

@anchor{timer-example}
@cindex timer, example
Let's now look at @dfn{timers}---services that run periodically, on
chosen calendar events (@pxref{Timers}).  If you ever used systemd
timers or the venerable cron, this is similar.  The example below
defines a timer that fires twice a day and runs the @command{updatedb}
command as root (@pxref{Invoking updatedb,,, find, Finding Files}):

@lisp
;; Import the module for 'make-timer-constructor' & co.
(use-modules (shepherd service timer))

(define updatedb
  (service
    '(updatedb)
    #:start (make-timer-constructor
              ;; Fire at midnight and noon everyday.
              (calendar-event #:hours '(0 12) #:minutes '(0))
              (command '("/usr/bin/updatedb"
                         "--prunepaths=/tmp")))
    #:stop (make-timer-destructor)
    #:actions (list timer-trigger-action)))

(register-services (list updatedb))
(start-in-the-background '(updatedb))
@end lisp

Thanks to the @code{#:actions} bit above, you can also run @code{herd
trigger updatedb} to trigger that job.

In these examples, we haven't discussed dependencies among
services---the @code{#:requires} keyword of @code{<service>}---nor did
we discuss systemd-style services.  These are extensions of what we've
seen so far.  @xref{Services}, for details.

If you use Guix System, you will see that it contains a wealth of
Shepherd service definitions.  The nice thing is that those give you a
@emph{complete view} of what goes into the service---not just how the
service is started and stopped, but also what software package is used
and what configuration file is provided.  @xref{Shepherd Services,,,
guix, GNU Guix Reference Manual}, for more info.

@node Managing User Services
@section Managing User Services

The Shepherd can be used to manage services for an unprivileged user.
First, you may want to ensure it is up and running every time you log
in.  One way to accomplish that is by adding the following lines to
@file{~/.bash_profile} (@pxref{Bash Startup Files,,, bash, The GNU Bash
Reference Manual}):

@verbatim
if [[ ! -S ${XDG_RUNTIME_DIR-$HOME/.cache}/shepherd/socket ]]; then
    shepherd
fi
@end verbatim

Then, we suggest the following top-level
@file{$XDG_CONFIG_HOME/shepherd/init.scm} file, which will automatically
load individual service definitions from
@file{~/.config/shepherd/init.d}:

@lisp
(use-modules (shepherd service)
             ((ice-9 ftw) #:select (scandir)))

;; Send shepherd into the background
(perform-service-action root-service 'daemonize)

;; Load all the files in the directory 'init.d' with a suffix '.scm'.
(for-each
  (lambda (file)
    (load (string-append "init.d/" file)))
  (scandir (string-append (dirname (current-filename)) "/init.d")
           (lambda (file)
             (string-suffix? ".scm" file))))

@end lisp

Then, individual user services can be put in
@code{$XDG_CONFIG_HOME/shepherd/init.d/}, e.g., for @command{ssh-agent}.

@lisp
;; Add to your ~/.bash_profile:
;;
;; SSH_AUTH_SOCK=$@{XDG_RUNTIME_DIR-$HOME/.cache@}/ssh-agent/socket
;; export SSH_AUTH_SOCK

(use-modules (shepherd support))

(define ssh-agent
  (service
    '(ssh-agent)
    #:documentation "Run `ssh-agent'"
    #:start (lambda ()
              (let ((socket-dir (string-append %user-runtime-dir "/ssh-agent")))
                (unless (file-exists? socket-dir)
                  (mkdir-p socket-dir)
                  (chmod socket-dir #o700))
                (fork+exec-command
                 `("ssh-agent" "-D" "-a" ,(string-append socket-dir "/socket"))
                 #:log-file (string-append %user-log-dir "/ssh-agent.log"))))
    #:stop (make-kill-destructor)
    #:respawn? #t))

(register-services (list ssh-agent))
(start-service ssh-agent)
@end lisp

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Service Collection
@chapter Service Collection

The Shepherd comes with a collection of services that let you control it
or otherwise extend its functionality.  This chapter documents them.

@menu
* System Log Service::          Good ol' syslog.
* Log Rotation Service::        Tidying up log files.
* Transient Service Maker::     Running commands in the background.
* Timer Service::               Scheduling command execution.
* Monitoring Service::          Monitoring shepherd resource usage.
* REPL Service::                Interacting with a running shepherd.
@end menu

@node System Log Service
@section System Log Service

@cindex system log (syslogd)
@cindex syslogd (system log)
Traditionally, GNU and Unix-like systems come with a system-wide logging
mechanism called @dfn{syslog} that applications and services can use.
This mechanism usually relies on a standalone process, the
@command{syslogd} daemon, that listens for incoming messages, typically
on the @file{/dev/log} Unix-domain socket, and writes them to files or
terminals according to user settings (@pxref{syslogd invocation,
syslogd,, inetutils, GNU Inetutils}).  Each message sent to
@command{syslogd} specifies its @dfn{priority} and originating
@dfn{facility} along with the actual message (@pxref{Overview of
Syslog,,, libc, The GNU C Library Reference Manual}).

The Shepherd provides an optional in-process service that can be used as
a substitute for the traditional @command{syslogd} program.  The
configuration snippet below instantiates and registers that service for
@command{shepherd} running as PID@tie{}1:

@lisp
(use-modules (shepherd service system-log))

(register-services
  ;; Create a system log with the default settings.
  (list (system-log-service)))

;; Start it.
(start-service (lookup-service 'system-log))
@end lisp

The configuration above starts the service under the names
@code{system-log} and @code{syslogd}.  Messages are logged in files
under @file{/var/log}, on @file{/dev/tty12} (the twelfth console
terminal on Linux), and on the console for emergency messages.  The
@code{system-log} service is integrated with the log rotation service:
log files it creates are subject to log rotation, in a ``race-free''
fashion (@pxref{Log Rotation Service}).

The destination of syslog messages---the files, terminals, etc.@: where
they are written---can be configured by passing a procedure as the
@code{#:message-destination} argument of @code{system-log-service}:

@lisp
(define (message-destination message)
  ;; Return the list of files where MESSAGE is to be written.
  (cond ((member (system-log-message-priority message)
                 (list (system-log-priority emergency)
                       (system-log-priority alert)))
         ;; Important messages go both to a file and to the console.
         '("/dev/console" "/var/log/messages))
        ((string-prefix? "sudo" (system-log-message-content message))
         ;; Messages coming from sudo go to a dedicated file.
         '("/var/log/sudo"))
        ((member (system-log-message-facility message)
                 (list (system-log-facility authorization)
                       (system-log-facility authorization/private)))
         ;; Security-sensitive messages are written only to this
         ;; one file.
         '("/var/log/secure"))
        (else
         ;; Everything else goes to /var/log/messages.
         '("/var/log/messages"))))

(define my-syslogd
  ;; Customized system log instance.
  (system-log-service #:message-destination message-destination))
@end lisp

The example above shows how to assign different destinations based on
message priority, facility, and content.  Note that each message can be
written to zero, one, or more files.

The interface to the system log service is provided by the
@code{(shepherd service system-log)} module and described thereafter.

@deffn {Procedure} system-log-message? @var{obj}
Return true if @var{obj} is a system log message.
@end deffn

@deffn {Procedure} system-log-message-facility @var{message}
@deffnx {Procedure} system-log-message-priority @var{message}
@deffnx {Procedure} system-log-message-content @var{message}
@deffnx {Procedure} system-log-message-sender @var{message}
Return the given attribute of @var{message}, a system log message:

@itemize
@item
Its facility, an integer as constructed by @code{system-log-facility}.
For example, the expression @code{(system-log-facility mail)} evaluates
to the value of the ``mail'' facility.

@item
Its priority, an integer as constructed by @code{system-log-priority}.
For example, @code{(system-log-priority debug)} returns the ``debug''
priority.

@item
Its content, a string.

@item
Its sender, either @code{#f}, in which case the message originates from
the local host, or a socket address as constructed by
@code{make-socket-address} if the message originates from a different
host (see below).
@end itemize
@end deffn

@deffn {Procedure} system-log-service [@var{sources}] @
         [#:provision '(system-log syslogd)] @
         [#:requirement '()] @
         [#:kernel-log-file (kernel-log-file)] @
         [#:message-destination (default-message-destination-procedure)] @
         [#:date-format default-logfile-date-format] @
         [#:history-size (default-log-history-size)] @
         [#:max-silent-time (default-max-silent-time)]
Return the system log service (@dfn{syslogd}) with the given
@var{provision} and @var{requirement} (lists of symbols).  The service accepts
connections on @var{sources}, a list of @code{<endpoint>} objects; optionally
it also reads messages from @code{#:kernel-log-file}, which defaults to
@file{/proc/kmsg} (Linux) or @file{/dev/klog} (the Hurd) when running as root.

Log messages are passed to @var{message-destination}, a one-argument procedure
that must return the list of files to write it to.  Write a mark to log files
when no message has been logged for more than @var{max-silent-time} seconds
(this feature is disabled by setting @var{max-silent-time} to @code{#f}).
Timestamps in log files are formatted according to @var{date-format}, a format
string for @code{strftime}, including delimiting space---e.g., @code{\"%c \"}
for a format identical to that of traditional syslogd implementations.

Keep up to @var{history-size} messages in memory for the purposes of allowing
users to view recent messages without opening various files.
@end deffn

@deffn {Procedure} default-message-destination-procedure
Return a procedure that, given a system log message, returns a ``good''
default destination to log it to.
@end deffn

@defvar kernel-log-file
This SRFI-39 parameter denotes the location of the kernel log file,
by default @file{/proc/kmsg} on Linux and @file{/dev/klog} on the Hurd.
@end defvar

@defvar default-max-silent-time
This SRFI-39 parameter denotes the maximum number of seconds after which
syslogd prints a ``mark'' in its files if it has not printed anything
for this long.
@end defvar


@node Log Rotation Service
@section Log Rotation Service

@cindex log rotation
@cindex rotating log files of services
All these services produce many log files; they're useful, for sure, but
wouldn't it be nice to archive them or even delete them periodically?
The @dfn{log rotation} service does exactly that.  Once you've enabled
it, it periodically @dfn{rotates} the log files of services---those
specified via the @code{#:log-file} argument of service constructors
(@pxref{Service De- and Constructors})---those of the system log
(@pxref{System Log Service}), and also, optionally,
``external'' log files produced by some other mechanism.

By ``rotating'' we mean this: if a service produces
@file{/var/log/my-service.log}, then rotating it consists in
periodically renaming it and compressing it to obtain, say,
@file{/var/log/my-service.log.1.gz}---after renaming the @emph{previous}
@file{my-service.1.log.gz} to @file{my-service.log.2.gz}, and so
on@footnote{This is comparable to what the venerable logrotate tool
would do.}.  Files older than some configured threshold are deleted
instead of being renamed.  The process is race-free: if the service is
running, not a single line that it logs is lost during rotation.

To enable the log rotation service, you can add the following lines to
your configuration file (@pxref{Service Examples}):

@lisp
(use-modules (shepherd service log-rotation))

(register-services
  ;; Create a service that rotates log files once a week.
  (list (log-rotation-service)))

;; Start it.
(start-service (lookup-service 'log-rotation))
@end lisp

This creates a @code{log-rotation} service, which is in fact a timed
service (@pxref{Timers}).  By default it rotates logs once a week and
you can see past and upcoming runs in the usual way:

@example
herd status log-rotation
@end example

You can also trigger it explicitly at any time, like so:

@example
herd trigger log-rotation
@end example

Last, you can list log files subject to rotation:

@example
herd files log-rotation
@end example

The default settings should be good for most use cases, but you can
change them by passing the @code{log-rotation-service} procedure a
number of arguments---see the reference documentation below.

@deffn {Procedure} log-rotation-service [@var{event}] @
          [#:provision '(log-rotation)] [#:requirement '()] @
          [#:external-log-files '()] @
          [#:compression (%default-log-compression)] @
          [#:expiry (%default-log-expiry)] @
          [#:rotation-size-threshold (%default-rotation-size-threshold)]
Return a timed service that rotates service logs along with
@var{external-log-files} (a list of file names such as
@file{/var/log/nginx/access.log} corresponding to ``external'' log files
not passed as @code{#:log-file} to any service) on every occurrence of
@var{event}, a calendar event.

Compress log files according to @var{method}, which can be one of
@code{'gzip}, @code{'zstd}, @code{'none}, or a one-argument procedure that
is passed the file name.  Log files smaller than @var{rotation-size-threshold}
are not rotated; copies older than @var{expiry} seconds are deleted.

Last, @var{provision} and @var{requirement} are lists of symbols specifying
what the service provides and requires, respectively.  Specifying
@var{requirement} is useful to ensure, for example, that log rotation runs
only if the service that mounts the file system that hosts log files is up.
@end deffn

@node Transient Service Maker
@section Transient Service Maker

@cindex transient service maker
@cindex background, running commands
The @dfn{transient service maker} lets you run commands in the
background, and it does so by wrapping those commands in transient
services (@pxref{Defining Services}).  It is similar to the
@uref{https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html,
@command{systemd-run}} command, which you might have encountered before.

A simple configuration file that uses this service looks like this:

@lisp
(use-modules (shepherd service transient))

(register-services (list (transient-service)))
@end lisp

This creates a service called @code{transient} that has a @code{spawn}
action, which you can use like this:

@example shell
# Run 'make' from the current directory.
herd spawn transient -d "$PWD" -- make -j4

# Run 'rsync' from the home directory, inheriting
# the 'SSH_AUTH_SOCK' environment variable.
herd spawn transient \
  --log-file=backup.log \
  -E SSH_AUTH_SOCK=$SSH_AUTH_SOCK -- \
  rsync -e ssh -vur . backup.example.org:
@end example

@c copied from 'herd schedule timer'
Each of these @command{herd spawn transient} commands creates a new
transient service.  Like any other service, they can be inspected and
stopped.  Running @command{herd stop transient} stops all the currently
running transients.

The command runs from the directory specified by
@code{default-service-directory} or from that specified by the
@option{--working-directory} option of @command{herd}; it starts with the
environment variables in @code{default-environment-variables}, augmented
with @env{HOME} when running as a different user, with the addition of
variables passed with @code{--environment-variable}.  @xref{Invoking
herd}, for more info on influential command-line options.

@deffn {Procedure} transient-service [@var{provision}] @
         [#:requirement '()]
Return a service with the given @var{provision} and
@var{requirement}.  The service has a @code{spawn} action that lets users run
commands in the background.
@end deffn

@node Timer Service
@section Timer Service

@anchor{timer-schedule-command}
@cindex timer service, like @command{at}
@cindex @command{at} command, with timer service
The @code{(shepherd service timer)} provides the @code{timer} service,
which lets you dynamically create timed services (@pxref{Timers}), from
the command line, in a way similar to the traditional @command{at}
command:

@example shell
# Run the 'mail' command, as root, as 12PM.
herd schedule timer at 12:00 -- \
  mail --to=charlie -s "Lunch time!"

# Run the 'mpg123' command as user 'charlie' at 7AM, from charlie's
# home directory.
herd schedule timer at 07:00 --user=charlie -- \
  mpg123 Music/alarm.mp3

# Run 'rsync' from the ~/doc directory, inheriting the 'SSH_AUTH_SOCK'
# environment variable.  Log the output in 'backup.log'.
herd schedule timer at 13:00 \
  --log-file=backup.log \
  -d ~/doc -E SSH_AUTH_SOCK=$SSH_AUTH_SOCK -- \
  rsync -e ssh -vur . backup.example.org:
@end example

Each of these @command{herd schedule timer} commands creates a new timed
service, which, like any other service, can be inspected and stopped;
those services are transient and vanish after they have executed their
command (@pxref{Defining Services}).

The command runs from the directory specified by
@code{default-service-directory} or from that specified by the
@option{--working-directory} option of @command{herd}; it has with the
environment variables in @code{default-environment-variables}, augmented
with @env{HOME} when running as a different user, with the addition of
variables passed with @code{--environment-variable}.  @xref{Invoking
herd}, for more info on influential command-line options.

This @code{timer} service can be added to your configuration like so:

@lisp
(use-modules (shepherd service timer))

(register-services (list (timer-service)))
@end lisp

The reference follows.

@deffn {Procedure} timer-service [@var{provision}] @
         [#:requirement '()]
Return a timer service with the given @var{provision} and
@var{requirement}.  The service has a @code{schedule} action that lets users
schedule command execution similar to the venerable @command{at} command.
@end deffn

@node Monitoring Service
@section Monitoring Service

The @dfn{monitoring service}, as its name suggests, monitors resource
usage of the shepherd daemon.  It does so by periodically logging
information about key resources: heap size (memory usage), open file
descriptors, and so on.  It is a simple and useful way to check whether
resource usage remains under control.

To use it, a simple configuration file that uses this service and
nothing else would look like this:

@lisp
(use-modules (shepherd service monitoring))

(register-services
  ;; Create a monitoring service that logs every 15 minutes.
  (list (monitoring-service #:period (* 15 60))))

;; Start it!
(start-service (lookup-service 'monitoring))
@end lisp

Using the @code{herd} command, you can get immediate resource usage
logging:

@example
$ herd log monitoring
service names: 3; heap: 8.77 MiB; file descriptors: 20
@end example

You can also change the logging period; for instance, here is how you'd
change it to 30 minutes:

@example
$ herd period monitoring 30
@end example

The @code{(shepherd service monitoring)} module exports the following
bindings:

@deffn {Procedure} monitoring-service @
          [#:period (default-monitoring-period)]
Return a service that will monitor shepherd resource usage by printing it
every @var{period} seconds.
@end deffn

@defvar default-monitoring-period
This parameter specifies the default monitoring period, in seconds.
@end defvar

@node REPL Service
@section Read-Eval-Print Loop Service

@cindex REPL, read-eval-print loop
@cindex read-eval-print loop, REPL
Scheme wouldn't be Scheme without support for @dfn{live hacking}, and
your favorite service manager had to support it too!  The @dfn{REPL
service} provides a read-eval-print loop (REPL) that lets you interact
with it from the comfort of the Guile REPL (@pxref{Running Guile
Interactively,,, guile, GNU Guile Reference Manual}).

The service listens for connections on a Unix-domain socket---by default
@file{/var/run/shepherd/repl} when running as root and
@file{/run/user/@var{uid}/shepherd/repl} otherwise---and spawns a new
service for each client connection.  Clients can use the REPL as they
would do with a ``normal'' REPL, except that it lets them inspect and
modify the state of the @command{shepherd} process itself.

@quotation Caveat
The live REPL is a powerful tool in support of live hacking and
debugging, but it's also a dangerous one: depending on the code you
execute, you could lock the @command{shepherd} process, make it crash,
or who knows what.

One particular aspect to keep in mind is that @command{shepherd}
currently uses Fibers in such a way that scheduling among fibers is
cooperative and non-preemptive.  Beware!
@end quotation

A configuration file that enables the REPL service looks like this:

@lisp
(use-modules (shepherd service repl))

(register-services (list (repl-service)))
@end lisp

With that in place, you can later start the REPL:

@example
herd start repl
@end example

From there you can connect to the REPL socket.  If you use Emacs, you
might fancy doing it with Geiser's @code{geiser-connect-local} function
(@pxref{Top,,, geiser, Geiser User Manual}).

The @code{(shepherd service repl)} module exports the following
bindings.

@deffn {Procedure} repl-service [@var{socket-file}]
Return a REPL service that listens to @var{socket-file}.
@end deffn

@defvar default-repl-socket-file
This parameter specifies the socket file name @code{repl-service} uses
by default.
@end defvar

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Misc Facilities
@chapter Misc Facilities

This is a list of facilities which are available to code running
inside of the Shepherd and is considered generally useful, but is not directly
related to one of the other topic covered in this manual.

@menu
* Process Utilities::           Helpers to deal with processes
* Errors::                      Signalling, handling and ignoring errors.
* Communication::               Input/Output in various ways.
@end menu

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Process Utilities
@section Process Utilities

@cindex spawning a program, from within shepherd
When writing custom service actions, you may find it useful to spawn
helper processes.  The @code{(shepherd service)} module provides helpers
that are safe to use within the @command{shepherd} process.
@xref{system-star-example, the nginx example}, for a typical use case.

@deffn {Procedure} system @var{command}
@deffnx {Procedure} system* @var{command} @var{arguments} @dots{}
Run @var{command} and return its exit status, which can be examined with
@code{status:exit-val} and related procedures (@pxref{Processes,,,
guile, GNU Guile Reference Manual}).

This works exactly like the @code{system} and @code{system*} procedures
provided by Guile but the @command{shepherd} process redefines them so
they cooperate with its own even loop and child process termination
handling.
@end deffn

@deffn {Procedure} spawn-command @var{command} @
  [#:user #f] [#:group #f] @
  [#:environment-variables (default-environment-variables)] @
  [#:directory (default-service-directory)] @
  [#:resource-limits '()] @
  [#:log-file #f]
Like @code{system*}, spawn @var{command} (a list of strings) and return
its exit status.  Additionally accept the same keyword arguments as
@code{fork+exec-command}: @code{#:user}, @code{#:directory}, and so on.
@ref{exec-command, @code{fork+exec-command}}.
@end deffn

@node Errors
@section Errors

@cindex assertions
@deffn {macro} assert expr
If @var{expr} yields @code{#f}, display an appropriate error
message and throw an @code{assertion-failed} exception.
@end deffn

@cindex system errors
@deffn {macro} without-system-error expr@dots{}
Evaluates the @var{expr}s, not going further if a system error occurs,
but also doing nothing about it.
@end deffn

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Communication
@section Communication

The @code{(shepherd comm)} module provides primitives that allow clients such
as @command{herd} to connect to @command{shepherd} and send it commands to
control or change its behavior (@pxref{Defining Services, actions of
services}).

@tindex <shepherd-command>
Currently, clients may only send @dfn{commands}, represented by the
@code{<shepherd-command>} type.  Each command specifies a service it
applies to, an action name, a list of strings to be used as arguments,
and a working directory.  Commands are instantiated with
@code{shepherd-command}:

@deffn {Procedure} shepherd-command @var{action} @var{service} @
         [#:@var{arguments} '()] [#:@var{directory} (getcwd)]
Return a new command (a @code{<shepherd-command>}) object for
@var{action} on @var{service}.
@end deffn

@noindent
Commands may then be written to or read from a communication channel
with the following procedures:

@deffn {Procedure} write-command @var{command} @var{port}
Write @var{command} to @var{port}.
@end deffn

@deffn {Procedure} read-command @var{port}
Receive a command from @var{port} and return it.
@end deffn

In practice, communication with @command{shepherd} takes place over a
Unix-domain socket, as discussed earlier (@pxref{Invoking shepherd}).
Clients may open a connection with the procedure below.

@deffn {Procedure} open-connection [@var{file}]
Open a connection to the daemon, using the Unix-domain socket at
@var{file}, and return the socket.

When @var{file} is omitted, the default socket is used.
@end deffn

@cindex output
The daemon writes output to be logged or passed to the
currently-connected client using @code{local-output}:

@deffn {Procedure} local-output format-string . args
This procedure should be used for all output operations in the Shepherd.  It
outputs the @var{args} according to the @var{format-string}, then
inserts a newline.  It writes to whatever is the main output target of
the Shepherd, which might be multiple at the same time in future versions.
@end deffn

@cindex protocol, between @command{shepherd} and its clients
Under the hood, @code{write-command} and @code{read-command} write/read
commands as s-expressions (sexps).  Each sexp is intelligible and
specifies a protocol version.  The idea is that users can write their
own clients rather than having to invoke @command{herd}.  For instance,
when you type @command{herd status}, what is sent over the wire is the
following sexp:

@lisp
(shepherd-command
  (version 0)
  (action status) (service root)
  (arguments ()) (directory "/data/src/dmd"))
@end lisp

The reply is also an sexp, along these lines:

@lisp
(reply (version 0)
       (result (((service @dots{}) @dots{})))
       (error #f) (messages ()))
@end lisp

This reply indicates that the @code{status} action was successful,
because @code{error} is @code{#f}, and gives a list of sexps denoting
the status of services as its @code{result}.  The @code{messages} field
is a possibly-empty list of strings meant to be displayed as is to the
user.


@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Internals
@chapter Internals

This chapter contains information about the design and the
implementation details of the Shepherd for people who want to hack it.

The GNU@tie{}Shepherd is developed by a group of people in connection
with @uref{https://www.gnu.org/software/guix/, Guix System}, GNU's
advanced distribution, but it can be used on other distros as well.
You're very much welcome to join us!  You can report bugs to
@email{bug-guix@@gnu.org} and send patches or suggestions to
@email{guix-devel@@gnu.org}.

@menu
* Coding Standards::            How to properly hack the Shepherd.
* Service Internals::           How services actually work.
* Design Decisions::            Why the Shepherd is what it is.
@end menu

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Coding Standards
@section Coding Standards

About formatting: Use common sense and GNU Emacs (which actually is
the same, of course), and you almost can't get the formatting wrong.
Formatting should be as in Guile and Guix, basically.  @xref{Coding
Style,,, guix, GNU Guix Reference Manual}, for more info.

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Service Internals
@section Service Internals

@cindex CSP, concurrent sequential processes
@cindex concurrent sequential processes, CSP
Under the hood, each service record has an associated @dfn{fiber}, a
lightweight execution thread and @command{shepherd} as a whole follows
the @acronym{CSP, Concurrent Sequential Processes} model
(@pxref{Introduction,,, fibers, Fibers}).
A service's fiber encapsulates all the @emph{state} of its corresponding
service: its status (whether it's running, stopped, etc.), its ``running
value'' (such as the PID of its associated process), the time at which
its status changed, and so on.  Procedures that access the state of a
service, such as @code{service-status}, or that modify it, such as
@code{start-service} (@pxref{Interacting with Services}), merely send a
message to the service's associated fiber.

@cindex actor model, for services
This pattern follows the @dfn{actor model}: each of these per-service
fibers is an @dfn{actor}@footnote{There is one noteworthy difference
compared to the actor model.  In the ``real'' actor model, messaging
among actors is asynchronous; in the CSP model, messaging is
synchronous, which means that the sender and receiver must always
@emph{rendezvous} and their send/receive operation blocks until the
other end is here to receive/send the message.  @xref{Context,,, fibers,
Fibers}, for more info.}.  There are several benefits:

@itemize
@item
each actor has a linear control flow that is easy to reason about;
@item
access and modification of the service state are race-free since they
are all handled sequentially by its actor;
@item
the actor's code is purely functional, which again makes it easier to
reason about it.
@end itemize

There are other actors in the code, such as the service registry
(@pxref{Service Registry}).  Fibers are used pervasively throughout the
code to achieve concurrency.

Note that Fibers is set up such that the @code{shepherd} process has
only one POSIX thread (this is mandated by POSIX for processes that call
@code{fork}, with all its warts), and fibers are scheduled in a
cooperative fashion.  This means that it is possible to block the
@code{shepherd} process for instance by running a long computation or by
waiting on a socket that is not marked as @code{SOCK_NONBLOCK}.  Be
careful!

We think this programming model makes the code base not only more
robust, but also very fun to work with---we hope you'll enjoy it too!

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Design Decisions
@section Design Decisions

@quotation Note
This section was written by Wolfgang Jährling back in 2003 and documents
the original design of what was then known as GNU@tie{}dmd.  The main
ideas remain valid but some implementation details and goals have
changed.
@end quotation

The general idea of a service manager that uses dependencies, similar
to those of a Makefile, came from the developers of the GNU Hurd, but
as few people are satisfied with System V Init, many other people had
the same idea independently.  Nevertheless, the Shepherd was written with the
goal of becoming a replacement for System V Init on GNU/Hurd, which
was one of the reasons for choosing the extension language of the GNU
project, Guile, for implementation (another reason being that it makes
it just so much easier).

The runlevel concept (i.e. thinking in @emph{groups} of services) is
sometimes useful, but often one also wants to operate on single
services.  System V Init makes this hard: While you can start and stop
a service, @code{init} will not know about it, and use the runlevel
configuration as its source of information, opening the door for
inconsistencies (which fortunately are not a practical problem
usually).  In the Shepherd, this was avoided by having a central entity that is
responsible for starting and stopping the services, which therefore
knows which services are actually started (if not completely
improperly used, but that is a requirement which is impossible to
avoid anyway).  While runlevels are not implemented yet, it is clear
that they will sit on top of the service concept, i.e. runlevels will
merely be an optional extension that the service concept does not rely
on.  This also makes changes in the runlevel design easier when it may
become necessary.

The consequence of having a daemon running that controls the services
is that we need another program as user interface which communicates
with the daemon.  Fortunately, this makes the commands necessary for
controlling services pretty short and intuitive, and gives the
additional bonus of adding some more flexibility.  For example, it is
easiely possible to grant password-protected control over certain
services to unprivileged users, if desired.

An essential aspect of the design of the Shepherd (which was already mentioned
above) is that it should always know exactly what is happening,
i.e. which services are started and stopped.  The alternative would
have been to not use a daemon, but to save the state on the file
system, again opening the door for inconsistencies of all sorts.
Also, we would have to use a separate program for respawning a service
(which just starts the services, waits until it terminates and then
starts it again).  Killing the program that does the respawning (but
not the service that is supposed to be respawned) would cause horrible
confusion.  My understanding of ``The Right Thing'' is that this
conceptionally limited strategy is exactly what we do not want.

The way dependencies work in the Shepherd took a while to mature, as it was not
easy to figure out what is appropriate.  I decided to not make it too
sophisticated by trying to guess what the user might want just to
theoretically fulfill the request we are processing.  If something
goes wrong, it is usually better to tell the user about the problem
and let her fix it, taking care to make finding solutions or
workarounds for problems (like a misconfigured service) easy.  This
way, the user is in control of what happens and we can keep the
implementation simple.  To make a long story short, @emph{we don't try
to be too clever}, which is usually a good idea in developing
software.

If you wonder why I was giving a ``misconfigured service'' as an
example above, consider the following situation, which actually is a
wonderful example for what was said in the previous paragraph: Service
X depends on symbol S, which is provided by both A and B.  A depends
on AA, B depends on BB.  AA and BB conflict with each other.  The
configuration of A contains an error, which will prevent it from
starting; no service is running, but we want to start X now.  In
resolving its dependencies, we first try to start A, which will cause
AA to be started.  After this is done, the attempt of starting A
fails, so we go on to B, but its dependency BB will fail to start
because it conflicts with the running service AA.  So we fail to
provide S, thus X cannot be started.  There are several possibilities
to deal with this:

@itemize @bullet
@item
When starting A fails, terminate those services which have been
started in order to fulfill its dependencies (directly and
indirectly).  In case AA was running already, we would not want to
terminate it.  Well, maybe we would, to avoid the conflict with BB.
But even if we would find out somehow that we need to terminate AA to
eventually start X, is the user aware of this and wants this to happen
(assuming AA was running already)?  Probably not, she very likely has
assumed that starting A succeeds and thus terminating AA is not
necessary.  Remember, unrelated (running) services might depend in AA.
Even if we ignore this issue, this strategy is not only complicated,
but also far from being perfect: Let's assume starting A succeeds, but
X also depends on a service Z, which requires BB.  In that case, we
would need to detect in the first place that we should not even try to
start A, but directly satisfy X's dependency on S with B.

@item
We could do it like stated above, but stop AA only if we know we won't
need it anymore (for resolving further dependencies), and start it
only when it does not conflict with anything that needs to get
started.  But should we stop it if it conflicts with something that
@emph{might} get started?  (We do not always know for sure what we
will start, as starting a service might fail and we want to fall back
to a service that also provides the particular required symbol in that
case.)  I think that either decision will be bad in one case or
another, even if this solution is already horribly complicated.

@item
When we are at it, we could just calculate a desired end-position, and
try to get there by starting (and stopping!) services, recalculating
what needs to be done whenever starting a service fails, also marking
that particular service as unstartable, except if it fails to start
because a dependency could not be resolved (or maybe even then?).
This is even more complicated.  Instead of implementing this and
thereby producing code that (a) nobody understands, (b) certainly has
a lot of bugs, (c) will be unmaintainable and (d) causes users to
panic because they won't understand what will happen, I decided to do
the following instead:

@item
Just report the error, and let the user fix it (in this case, fix the
configuration of A) or work around it (in this case, disable A so that
we won't start AA but directly go on to starting B).
@end itemize

I hope you can agree that the latter solution after all is the best
one, because we can be sure to not do something that the user does not
want us to do.  Software should not run amok.  This explanation was
very long, but I think it was necessary to justify why the Shepherd uses a very
primitive algorithm to resolve dependencies, despite the fact that it
could theoretically be a bit more clever in certain situations.

One might argue that it is possible to ask the user if the planned
actions are ok with her, and if the plan changes ask again, but
especially given that services are supposed to usually work, I see few
reasons to make the source code of the Shepherd more complicated than
necessary.  If you volunteer to write @emph{and} maintain a more
clever strategy (and volunteer to explain it to everyone who wants to
understand it), you are welcome to do so, of course@dots{}


@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node GNU Free Documentation License
@appendix GNU Free Documentation License

@include fdl-1.3.texi

@c @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@node Concept Index
@unnumbered Concept Index

@printindex cp

@node Procedure and Macro Index
@unnumbered Procedure and Macro Index

@printindex fn

@node Variable Index
@unnumbered Variable Index

@printindex vr

@node Type Index
@unnumbered Type Index

@printindex tp

@bye