2016-08-11 6 views
1

多くのシステムデーモンは、起動/停止コマンドを使用して起動できます。私はちょうどLinuxシステム上でどのように起動/停止が機能するのか不思議であった。私はデーモンの実行可能ファイルを書きましたが、Linuxで起動/停止によって制御できるように、どのように設定する必要がありますか?Linuxでの起動および停止コマンドで制御可能なレジスタデーモン

+1

https://www.freedesktop.org/software/systemd/man/systemd.service.html(さらに一般的にはhttps://www.freedesktop.org/software/systemd/man/)を探している可能性があります。それはデーモン管理のためのただ一つの(普及した)可能性です。あなたが話しているディストリビューションを知らなければ、私たちはもっと具体的にすることはできません。 (私が入力しているLinuxベースのコンピュータでは、 "開始"と "停止"のコマンドはありません) – zwol

+0

ありがとう!ザック! – JAX

答えて

1

私は数年前にlinux(ArchLinux)でデーモンを作り、毎日完璧に動作します。 これには2通りの方法があります。ショート道と長い道のり:

ショートウェイ:

の/ etc/systemdに/システム/ mydaemon:

は、例えばmydaemon.serviceのために呼ばれるの/ etc/systemdに/システム/内のファイルを作成します。 .service

[Unit] 
Description=This is my first daemon! - Fernando Pucci 
After=network.target 

[Service] 
User=root 
WorkingDirectory=/root 
Type=oneshotmc 
RemainAfterExit=yes 
ExecStart=/bin/echo -e "Daemon started" 
ExecStop=/bin/echo -e "Daemon Stopped" 

[Install] 
WantedBy=multi-user.target 

このサービスは、デーモンが起動または停止示すが、何もしません。必要な文章でエコーを変更することができます。

あなたには、いくつかのスクリプトを実行する必要がある場合は、長い道のりてみてください: は、いくつかのディレクトリにファイルを作成します

長い道のり、ルートフォルダなどをまたは/ usr/libに/ systemdに例

を呼びかけ/スクリプト

あなたは (chmodコマンドのx)は、それが実行可能にする必要があります(そして、あなたはそれをのwiを実行することができ/root/mydaemon.sh

start() { 
    <your start sentences here 
    and here> 
} 
stop() { 
    <your stop sentences here 
    and here> 
} 

case $1 in 
    start|stop) "$1" ;; 
esac 

目の開始またはそれをテストするためのパラメータを停止します。)

を第二段階として、 /usr/lib/systemd/system/mydaemon.serviceに

[Unit] 
Description=Second daemon of Fernando Pucci 
After=network.target 

[Service] 
User=root 
WorkingDirectory=/root 
Type=oneshot 
RemainAfterExit=yes 
ExecStart=/bin/bash -c '/root/mydaemon.sh start' 
ExecStart=/bin/echo -e "MyDaemon Started" 
ExecStop=/bin/bash -c '/root/mydaemon.sh stop' 
ExecStop=/bin/echo -e "MyDaemon Stopped" 

[Install] 
WantedBy=multi-user.target 

起動とを停止し、別のファイルを作成します

systemctl start mydaemon 
systemctl stop mydaemon 
systemctl status mydaemon 
systemctl enable mydaemon 
systemctl disable mydaemon 

あなた(と誰か)は、私にプライベートメッセージを送信して、その旨を知らせることができます。

関連する問題