kevinhu 發表於 2015-12-9 16:33:26

在 Linux 上 用 C++ 来coding service application

要在 Linux 上开发 Daemon application 可以参考看看,C++ 来开发。
Linux Daemon Writing Howto

kevinhu 發表於 2015-12-30 18:09:55

Write Daemon or Service in Linux

kevinhu 發表於 2016-1-4 15:27:14

本帖最後由 kevinhu 於 2016-1-6 11:53 編輯

Coding 只是完成程序,但是要掛上去還需要寫個Script才行的。
參考網站鏈接
How to Write Linux Init Scripts Based on LSB Init Standard

kevinhu 發表於 2016-1-5 17:27:49

本帖最後由 kevinhu 於 2016-1-19 15:10 編輯

如果要在開機時能自動啟動的話還必須要先跟系統註冊哦
update-rc.d

底下是實作的參考
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 root15 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:
/etc/inittab
    id:2345:respawn:/bin/sh /path/to/application/startup
[*]Stop, then start, the service:

[*]sudo service service stop
[*]sudo service service start

[*]Reboot the server.

[*]sudo reboot
Test
To test that these are working, you can:

[*]Reboot the server, then verify that the service is up
[*]Search for the process number:

[*]ps -ef | grep service

[*]Kill the process:

[*]sudo kill -9 process_number

[*]Wait five minutes, then verify that the service is back up


kevinhu 發表於 2016-3-29 11:10:21

在Linux上目前有3種主要Init System,分別為 SysVinit、systemd及Upstart,依Linux各個分支所選用為主,有關這些的系統演進情況看參考下列的鏈接
淺析 Linux 初始化 init 系統

使用的方法可參考下列的轉貼文:
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.

Disable service:

echo manual > /etc/init/{SERVICENAME}.override

kevinhu 發表於 2016-4-12 14:23:57

本帖最後由 kevinhu 於 2016-4-13 00:15 編輯

目前許多Linux的新版本大多轉換使用Systemd來管理service,因此有需要的話還是要了解一下在Systemd的架構及script的編寫方式,相關資料請參考
systemd 說明

一篇國外寫的文章對systemd的使用說明及心得
Getting Started With systemd on Debian Jessie

這裡有一篇指導如何撰寫systemd的service script以及如何由SysV init script 轉換成systemd的unit file,出自於Redhat 的Administrator's Guid
Creating and Modifying systemd Unit Files
頁: [1]
查看完整版本: 在 Linux 上 用 C++ 来coding service application