How to use linux systemd daemon & examples - githeim/windheim_archive GitHub Wiki
Overview
You need the process start automatically when your develop the embedded system like robot, settop box, digital TV ... In that case, you can use systemd daemon. In this page, It introduces how to make systemd daemon & install it in the system.
example
make your *.service file
make the *.service file example) your_daemon.service
[Unit]
Description=your daemon service
[Service]
PermissionsStartOnly=yes
User=ubuntu
Group=ubuntu
Type=simple
Environment="YOUR_ENV_VALUE_A=11"
Environment="YOUR_ENV_VALUE_B=blabla"
Environment="YOUR_ENV_VALUE_C=/home/ubuntu/your_bin"
Environment="XDG_RUNTIME_DIR=/run/user/1000"
WorkingDirectory=/home/ubuntu/workspace/path_to_your_script
ExecStart=/home/ubuntu/workspace/path_to_your_script/bringup_your_service.sh
ExecStop=/home/ubuntu/workspace/path_to_your_script/cleanup_your_service.sh
[Install]
WantedBy=multi-user.target
make your bringup script
example) bringup_your_service.sh
it is placed on '/home/ubuntu/workspace/path_to_your_script/bringup_your_service.sh'
bringup_your_service.sh
#!/bin/bash
# start the process A with non blocking (&)
/home/ubuntu/workspace/path_to_your_script/Process_A &
# start the process B with non blocking (&)
/home/ubuntu/workspace/path_to_your_script/Process_B &
# And keep running
while :
do
echo -e ${ROBOT_NAME}" daemon is running"
sleep 5
done
make your cleanup script
example) cleanup_your_service.sh
it is placed on '/home/ubuntu/workspace/path_to_your_script/cleanup_your_service.sh'
cleanup_your_service.sh
#!/bin/bash
# kill the processes that bringup script made
pkill -9 Process_A
pkill -9 Process_B
Install your service script
example)
$ sudo cp your_daemon.service /etc/systemd/system
$ sudo systemctl enable your_daemon.service
$ sudo systemctl daemon-reload
Now your service will be run automatically when the system reboot. You can start or stop your service
$ sudo systemctl stop your_daemon.service
$ sudo systemctl start your_daemon.service
Uninstall your service script
example)
$ sudo systemctl stop your_daemon.service
$ sudo systemctl disable your_daemon.service
$ sudo rm /etc/systemd/system/your_daemon.service
$ sudo systemctl daemon-reload
$ sudo systemctl reset-failed