User Tools

Site Tools


doc:appunti:linux:sa:systemd

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
doc:appunti:linux:sa:systemd [2017/12/13 11:56] – [Servizi enabled, disabled, masked, ...] niccolodoc:appunti:linux:sa:systemd [2023/12/13 12:36] – [Sostituto di rc.local] niccolo
Line 1: Line 1:
 ====== Systemd ====== ====== Systemd ======
  
 +**[[wp>Systemd|systemd]]** è il nuovo sistema di avvio dei servizi adottato a partire da da **Debian 8 Jessie**. Sostituisce il vecchio **[[https://wiki.debian.org/Debate/initsystem/sysvinit|sysvinit]]**.
 ===== Comandi di servizio ===== ===== Comandi di servizio =====
  
-Quando si modifica qualche file di configurazione (ad esempio gli script in ''/etc/init.d/''oppure gli //unit files// in ''/lib/systemd/system/'') è necessario notificare ''systemd'' in modo che rilegga tutte le dipendenze, ecc:+Quando si modifica qualche file di configurazione (ad esempio gli //unit files// in ''/lib/systemd/system/'' oppure gli script di compatibilità con il vecchio //sysvinit// contenuti in ''/etc/init.d/'') è necessario notificare ''systemd'' in modo che rilegga tutte le dipendenze, ecc:
  
 <code> <code>
Line 100: Line 101:
  
 La condizione **masked** di alcuni **unit file** è equivalente a //disabled//, ma previene anche l'avvio manuale del servizio. Se un file è disabled i suoi link simbolici sono rimossi, se è masked esso viene linkato a ''/dev/null''. La condizione **masked** di alcuni **unit file** è equivalente a //disabled//, ma previene anche l'avvio manuale del servizio. Se un file è disabled i suoi link simbolici sono rimossi, se è masked esso viene linkato a ''/dev/null''.
 +
 +===== Starting a service when a filesystem is available an mounted =====
 +
 +This solution was tested on **Debian 9 Stretch**.
 +
 +We have a filesystem on an **external USB disk**, which is not guaranteed to be present at bootstrap; to avoid hangs or errors during bootstrap, we have the following in **/etc/fstab**:
 +
 +<file>
 +PARTUUID=92e84ecd-a306-44df-929b-... /data  ext4  auto,nofail  0  2
 +</file>
 +
 +If the USB disk is not plugged during bootstrap, the filesystem is not mounted and there are not blocking errors (due the **nofail** option). We want to start a service (actually the Kodi Media Center) only if the filesystem is mounted. The relevant lines in the unit file **/etc/systemd/system/kodi.service** are:
 +
 +<file>
 +# See man systemd.unit for the following directives.
 +# This is equivalent of placing an "After" and "Requires" upon "data.mount";
 +RequiresMountsFor = /data
 +# When systemd starts or stops "data.mount", propagate action to this service.
 +PartOf = data.mount
 +</file>
 +
 +In this way, the Kodi service is not started if the USB disk is not connected. If the device is connected at a later time, the filesystem is automatically mounted and the Kodi service is automatically started.
 +
  
 ===== Modificare Ctrl-Alt-Del con Systemd ===== ===== Modificare Ctrl-Alt-Del con Systemd =====
Line 120: Line 144:
  
 Non è necessario modificare il contenuto di questa directory, si può fare un override creando il link simbolico opportuno nella directory **/etc/systemd/system/**. Invece del **reboot.target** si utilizza l'esistente **poweroff.target**. Non è necessario modificare il contenuto di questa directory, si può fare un override creando il link simbolico opportuno nella directory **/etc/systemd/system/**. Invece del **reboot.target** si utilizza l'esistente **poweroff.target**.
-===== Riferimenti web =====+===== Timeout in attesa di un device =====
  
 +Può capitare che il processo di **bootstrap fallisce** perché un device elencato in **/etc/fstab** non risulta disponibile nei tempi richiesti (lentezza del disco? CPU troppo veloce?). In questi casi si può aggiungere in /etc/fstab una opzione di montaggio:
 +
 +<file>
 +/dev/sda3  /home  ext4  defaults,x-systemd.device-timeout=300s  0  2
 +</file>
 +
 +===== Verifica Servizi =====
 +
 +Alcuni comandi utili per **verificare il funzionamento** dei servizi gestiti da **systemd**.
 +
 +Mostra lo stato di tutti i **servizi** e di tutte le **unit**:
 +
 +<code>
 +systemctl --all --full status
 +</code>
 +
 +Mostra le **unit** //required// e //wanted// dalla unit specificata:
 +
 +<code>
 +systemctl --all list-dependencies ntp.service
 +</code>
 +
 +===== Alternatriva allo script rc.local =====
 +
 +Come eseguire uno script all'avvio di un sisetma Systemd.
 +
 +Creare una unit Systemd **/etc/systemd/system/my-startup.service**:
 +
 +<file>
 +# /etc/systemd/system/my-startup.service
 +#
 +# Service executed once the system has reached the multi-user status.
 +#
 +#  Type=oneshot         The unit is up after the main process exits.
 +#  RemainAfterExit=yes  The service shall be considered active even
 +#                       when all its processes exited.
 +#
 +# Eanble the service with:
 +#   systemctl enable my-startup.service
 +
 +[Service]
 +Type=oneshot
 +RemainAfterExit=yes
 +ExecStart=/usr/local/sbin/my-startup-script
 +
 +[Install]
 +WantedBy=multi-user.target
 +</file>
 +
 +Abilitare la unit e predisporre lo script **/usr/local/sbin/my-startup-script**.
 +
 +===== Riferimenti web =====
  
   * **[[https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files|   * **[[https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files|
 Understanding Systemd Units and Unit Files]]** Understanding Systemd Units and Unit Files]]**
   * **[[https://askubuntu.com/questions/710420/why-are-some-systemd-services-in-the-masked-state|Why are some systemd services in the “masked” state?]]**   * **[[https://askubuntu.com/questions/710420/why-are-some-systemd-services-in-the-masked-state|Why are some systemd services in the “masked” state?]]**
doc/appunti/linux/sa/systemd.txt · Last modified: 2023/12/13 12:37 by niccolo