天天看點

Linux 應用程式開發入門

版權 © 2011, 2012 http://netkiller.github.com

摘要

我會實作一個守護程序,從這個程式你将了解,linux 應用程式開發基本流程

我們将實作一個遠端shell的功能,可以通過tcp協定,運作遠端機器上的指令或shell腳本

通過這個指令可以實作批量操作,管理上千台伺服器。需要發揮你的想象力,靈活使用它。

寫這個腳本,我是為了替代ssh遠端操作,因為ssh不能控制運作指令,操作風險大,也不安全。

程式還不完善,還需要很多後續改進工作,比如通過ssl建立socket連結,使用者認證,acl通路控制等等.

<a></a>

下面是我多年積累下來的經驗總結,整理成文檔供大家參考:

<a href="http://netkiller.github.com/architect/index.html" target="_top">netkiller architect 手劄</a>

<a href="http://netkiller.github.com/linux/index.html" target="_top">netkiller linux 手劄</a>

<a href="http://netkiller.github.com/developer/index.html" target="_top">netkiller developer 手劄</a>

<a href="http://netkiller.github.com/database/index.html" target="_top">netkiller database 手劄</a>

<a href="http://netkiller.github.com/debian/index.html" target="_top">netkiller debian 手劄</a>

<a href="http://netkiller.github.com/centos/index.html" target="_top">netkiller centos 手劄</a>

<a href="http://netkiller.github.com/freebsd/index.html" target="_top">netkiller freebsd 手劄</a>

<a href="http://netkiller.github.com/shell/index.html" target="_top">netkiller shell 手劄</a>

<a href="http://netkiller.github.com/www/index.html" target="_top">netkiller web 手劄</a>

<a href="http://netkiller.github.com/monitoring/index.html" target="_top">netkiller monitoring 手劄</a>

<a href="http://netkiller.github.com/storage/index.html" target="_top">netkiller storage 手劄</a>

<a href="http://netkiller.github.com/mail/index.html" target="_top">netkiller mail 手劄</a>

<a href="http://netkiller.github.com/security/index.html" target="_top">netkiller security 手劄</a>

<a href="http://netkiller.github.com/postgres/index.html" target="_top">netkiller postgresql 手劄</a>

<a href="http://netkiller.github.com/mysql/index.html" target="_top">netkiller mysql 手劄</a>

<a href="http://netkiller.github.com/ldap/index.html" target="_top">netkiller ldap 手劄</a>

<a href="http://netkiller.github.com/cryptography/index.html" target="_top">netkiller cryptography 手劄</a>

<a href="http://netkiller.github.com/intranet/index.html" target="_top">netkiller intranet 手劄</a>

<a href="http://netkiller.github.com/cisco/index.html" target="_top">netkiller cisco ios 手劄</a>

<a href="http://netkiller.github.com/docbook/index.html" target="_top">netkiller writer 手劄</a>

<a href="http://netkiller.github.com/version/index.html" target="_top">netkiller version 手劄</a>

<a href="http://netkiller.github.com/multimedia/index.html" target="_top">netkiller studio linux 手劄</a>

目錄

os: ubuntu 10.10

程式目錄: /srv/nodekeeper

目錄與相關檔案

usage()

getopt.getopt 實作unix風格的指令參數,例如:

具體實作代碼

--daemon 參數實作背景運作,原理是首先通過os.fork()克隆一個程序,然後退出目前程序,克隆的新程序繼續運作

如果是shell程式,你可使用“&amp;”符号背景運作,但作為一個應用程式,使用“&amp;”顯得不專業。

具體實作的代碼如下

程式一旦進入背景,目前程序即将關閉,是以你必須儲存pid,為後面的推出程式操作使用,這裡我們可以通過 --pidfile 指定一個pid檔案

程式一旦進入背景,你隻能通過ps,pstree, top 等指令檢視狀态,運作情況必須通過日志的形式,列印出來

具體實作代碼如下:

繼承 asynchat.async_chat 實作多線程

連接配接數限制

可以将這個參數提出來,然後通過指令行設定。

linux 所有守護程序都是用init.d下面的腳本來管理

當人你也可以直接運作指令:

但這樣隻能算是一個半成品,也不夠專業,我們寫的是linux運用程式,必須遵循linux規範,所有要實作一個init.d腳本

我們将使用daemon_opts變量,提供所有需要的參數