systemd
systemd
is a Linux initialization system and service manager, controls what programs run when the system boots and provides on-demand starting of daemons.
Creating scripts
Script directory:
/etc/systemd/system/
Script template
[Unit] After=network.target network-online.target [Service] ExecStart=/home/smgr/connect.sh [Install] WantedBy=multi-user.target
Type Option
simple
:: this option is used for standard long running processes. This option tellssystemd
it is responsible for forking the process and keeping it alive. This is the default option is atype
is not specified. Note that this option should not be used if your process forks itself; instead, theforking
option below should be used. In such a case, under this optionsystemd
will simply exit after the process forks itself and the child process will be lost and killed (I think).forking
:: this option should be used when it is expected that the process will fork itself (e.g. using&
). It is expected that the parent process will exit after start-up has been completed, and the (forked) child process will continue and be tracked bysystemd
.- Source: freedesktop
Useful commands
- Reload the daemon:
sudo systemctl daemon-reload
- Enable a service (to start on boot):
sudo systemctl enable name.service
- Start a service:
sudo systemctl start name.service
- Check service status:
sudo systemctl status name.service
Suspension/Hibernation
- Suspend machine:
sudo systemctl suspend
- Hibernate machine:
sudo systemctl hibernate
Mount Units
Standard mount unit might looks as follows
[Unit]
Description=Mount data drive
[Mount]
What=/dev/disk/by-uuid/<id>
Where=/path/to/mount
Type=ext4
Options=defaults
[Install]
WantedBy=multi-user.target
You simply need to get the partition uuid (so it can consistently be identified) and specify the path where you’d like to mount the drive, along with the drive format type. This file must be named path-to-mount.mount
, i.e. named according to the path at which the drive is to be mounted. Call systemctl enable path-to-mount.mount
to mount the drive on boot.
Sources
- Great article for deeper understanding of systemd: systemd unit files (DigitalOcean)