システムサービススクリプトはエコーされません。

システムサービススクリプトはエコーされません。

私は最近、systemd initシステムを使用しているコンピュータにFedora 22をインストールしました。

私はそれについて読んだので、postgresql用のシステム起動スクリプトを生成する必要があります。

テストのために、次のシェルスクリプトhello_worldを作成しました。

#! /bin/sh
#  testing systemctl script

start() {
   echo "Executing Start"
   echo "Testing 01"
   echo "Testing 02"
   echo "Testing 03"
}

stop() {
   echo "Stopping Hello World Script"
}

case "$1" in
   start)
      start
   ;;
   stop)
      stop
   ;;
   restart)
      stop
      sleep 2
      start
   ;; 
   *) exit 1
esac

端末を使用して実行すると、期待どおりに機能し、文字列をエコーし​​ます。

. hello_world start

これは /usr/lib/systemd/scripts に置かれた「Hello World スクリプトの起動」を反映しています。

その後、以下のようにシステムサービススクリプトhello_world.serviceを作成しようとしました。

[Unit]
Description=Hello World Testing Script

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/hello_world start
ExecStop=/usr/lib/systemd/scripts/hello_world stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

/usr/lib/systemd/systemに入れて実行してみました。

systemctl start hello_world.service

エラーは発生しませんが、hello_worldスクリプトのみを実行すると、予想されるエコー文字列を取得できません。

それで、それが実際に動作しているかどうかはわかりません。何か抜けましたか? systemctlコマンドがスクリプトの文字列をエコーし​​ないのはなぜですか?

ベストアンサー1

スクリプトの出力は端末に送信されないようですが、ステータスコマンドにリダイレクトされます。

サービスが実行された後

# systemctl start hello_world.service

状態で私の出力を見ることができます。

# systemctl status hello_world.service

これには、次のようにスクリプトのすべての出力が含まれます。

Apr 28 10:18:00 centos7.adminserv systemd[1]: Starting Hello World Testing S....
Apr 28 10:18:00 centos7.adminserv hello_world[18164]: Executing Start
Apr 28 10:18:00 centos7.adminserv hello_world[18164]: Testing 01
Apr 28 10:18:00 centos7.adminserv hello_world[18164]: Testing 02
Apr 28 10:18:00 centos7.adminserv hello_world[18164]: Testing 03
Apr 28 10:18:00 centos7.adminserv systemd[1]: Started Hello World Testing Sc.... 

おすすめ記事