@ মাইকেল ড্যাফিনের সমাধান ছাড়াও , আপনি নিম্নোক্ত উদাহরণে দেখানো হিসাবে এর ব্যবহার অর্জন করতে ডিমনাইজ সরঞ্জামটিও ব্যবহার করতে পারেন forking।
একটি সামান্য শেল স্ক্রিপ্ট দেওয়া হয়েছে যা আমি ডিমনাইজ করতে চাই এবং যা আমি সিস্টেমডের উপর নিয়ন্ত্রণ করতে চাই, আমি এটিকে এটি সংরক্ষণ করেছিলাম /home/pi/testscript.sh:
#!/bin/bash
while true;
do
sleep 1
echo -n "."
done
আপনার যদি এটি এখনও না থেকে থাকে তবে ডিমনোমাইজ ইনস্টল করুন, এটির মতো:
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
সদ্য নির্মিত পরিষেবাটি সিস্টেমডে ঘোষণা করতে হবে:
systemctl daemon-reload
এখন আপনি পরিষেবা এবং স্ক্রিপ্ট কাঁটাচামচ শুরু করতে পারেন। যেমনটি প্রত্যাশা করা হয়েছিল, সার্ভিস শুরুটি তত্ক্ষণাত শেলটিতে ফিরে আসে। ফলাফল সুস্পষ্ট:
$ tail -f testscript.log
.....................
Type=forking। তদ্ব্যতীত, এটিexecStartশেল প্রসার হিসাবে চলবে না , যাতে&শেষে কখনই ব্যাকগ্রাউন্ড পতাকা হিসাবে বোঝা যায় না।