標題: 在 Linux 上 用 C++ 来coding service application [打印本頁] 作者: kevinhu 時間: 2015-12-9 16:33 標題: 在 Linux 上 用 C++ 来coding service application 要在 Linux 上开发 Daemon application 可以参考看看,C++ 来开发。 Linux Daemon Writing Howto
作者: kevinhu 時間: 2015-12-30 18:09 Write Daemon or Service in Linux作者: kevinhu 時間: 2016-1-4 15:27 本帖最後由 kevinhu 於 2016-1-6 11:53 編輯
底下是實作的參考 Auto-start Checklist for System VThis section is a quick reference to make sure your service is set to automatically start. Configuration Checklist
Make sure the service has a functional Bash init script located at /etc/init.d/service
Use the update-rc.d command to enable the service (or for a CentOS system, chkconfig):
sudo update-rc.d service enable
This should create a symlink in /etc/rc2.d that looks like the following (do NOT create this manually):
lrwxrwxrwx 1 root root 15 Jul 31 07:09 S02mysql -> ../init.d/serviceNote that you should also see links from directories /etc/rc3.d through /etc/rc5.d; learn more about these numbers when we discuss runlevels.
Add a respawn line for this service at the bottom of the /etc/inittab file. Here's a generic example:
使用的方法可參考下列的轉貼文:
There are currently 3 main init systems used by linux. A few years ago, there was just one, SysVinit. But SysVinit was seriously lacking in capabilities such as service dependency graphing, so it's been deprecated in most distros by now. Currently most distros are switching to systemd. Though there is also upstart.
But here's the answer to your question for each of the 3 init systems:
SysVinit
SysVinit currently used by Debian and RedHat. Though the next version of RedHat (7) will be using systemd.
The univeral way of enabling SysVinit services on boot is to symlink them in /etc/rc3.d (or /etc/rc2.d). All services can be found in /etc/init.d. Note however that distros will often have their own tool for managing these files, and that tool should be used instead. (Fedora/RedHat has service and chkconfig, ubuntu has update-rc.d)
List services:
ls /etc/init.d/
Start service:
/etc/init.d/{SERVICENAME} start
Stop service:
/etc/init.d/{SERVICENAME} stop
Enable service:
cd /etc/rc3.d
ln -s ../init.d/{SERVICENAME} S95{SERVICENAME}
(the S95 is used to specify order. S01 will start before S02, etc)
Disable service:
rm /etc/rc3.d/*{SERVICENAME}
Systemd
The most notable distribution using systemd is Fedora. Though it is used by many others. Additionally, with Debian having chosen to go with systemd over upstart, it will become the defacto upstart system for most distributions (ubuntu has already announced they will be dropping upstart for systemd).
List services:
systemctl list-unit-files
Start service:
systemctl start {SERVICENAME}
Stop service:
systemctl stop {SERVICENAME}
Enable service:
systemctl enable {SERVICENAME}
Disable service:
systemctl disable {SERVICENAME}
Upstart
Upstart was developed by the Ubuntu folks. But after debian decided to go with systemd, Ubuntu announced they would drop upstart.
Upstart was also briefly used by RedHat, as it is present in RHEL-6, but it is not commonly used.
List services:
initctl list
Start service:
initctl start {SERVICENAME}
Stop service:
initctl stop {SERVICENAME}
Enable service:
2 ways unfortunately:
There will be a file /etc/default/{SERVICENAME} which contains a line ENABLED=.... Change this line to ENABLED=1.
There will be a file /etc/init/{SERVICENAME}.override. Make sure it contains start (or is absent entirely), not manual.