@ Michael Daffin 의 솔루션 외에도 다음 예제와 같이 daemonize 도구를 사용하여 사용법을 얻을 수 있습니다 forking.
내가 데몬 화하고 systemd를 제어하고 싶은 작은 쉘 스크립트가 주어지면 다음과 같이 저장했습니다 /home/pi/testscript.sh.
#!/bin/bash
while true;
do
sleep 1
echo -n "."
done
아직없는 경우 다음과 같이 daemonize를 설치하십시오.
sudo apt install daemonize
이제 파일 서비스 정의 파일을 작성하십시오.
sudo vi /etc/systemd/system/testomat.service
# It is not recommended to modify this file in-place, because it will
# be overwritten during package upgrades. If you want to add further
# options or overwrite existing ones then use
# $ systemctl edit testomat.service
# See "man systemd.service" for details.
# copied from https://github.com/bitcoin/bitcoin/blob/master/contrib/init/bitcoind.service and modified by Michael
[Unit]
Description=Test service
After=network.target
[Service]
ExecStart=daemonize -p /run/testomat/testomat.pid -o /home/pi/testscript.log /home/pi/testscript.sh
TimeoutSec=1200
# Make sure the config directory is readable by the service user
PermissionsStartOnly=true
# Process management
####################
Type=forking
PIDFile=/run/testomat/testomat.pid
Restart=on-failure
GuessMainPID = true
# Directory creation and permissions
####################################
# Run as pi:pi
User=pi
Group=pi
# /run/testomat
RuntimeDirectory=testomat
RuntimeDirectoryMode=0710
# /var/lib/testomat
StateDirectory=testomat
StateDirectoryMode=0710
# Hardening measures
####################
# Provide a private /tmp and /var/tmp.
PrivateTmp=true
# Mount /usr, /boot/ and /etc read-only for the process.
ProtectSystem=full
# Allow access to /home, /root and /run/user
# Chosing "false" is actually no hardening, this is just to demonstrate the usage of a service. Well, I could have omitted it. True. :)
ProtectHome=false
# Disallow the process and all of its children to gain
# new privileges through execve().
NoNewPrivileges=true
# Use a new /dev namespace only populated with API pseudo devices
# such as /dev/null, /dev/zero and /dev/random.
PrivateDevices=true
# Deny the creation of writable and executable memory mappings.
MemoryDenyWriteExecute=true
[Install]
WantedBy=multi-user.target
새로 생성 된 서비스는 systemd에 발표되어야합니다.
systemctl daemon-reload
이제 서비스와 스크립트 포크를 시작할 수 있습니다. 예상대로 서비스 시작은 즉시 셸로 돌아옵니다. 결과는 분명하다 :
$ tail -f testscript.log
.....................
Type=forking. 또한execStart쉘 확장으로 실행되지 않으므로&결국 백그라운드 플래그로 이해되지 않습니다.