systemctlがサービスとして起動されるように、画面内でnode.jsアプリケーションを実行するスクリプトを作成します。

systemctlがサービスとして起動されるように、画面内でnode.jsアプリケーションを実行するスクリプトを作成します。

起動時に開始されるサービスに設定したいnode.jsアプリケーションがあります。

/etc/systemd/system/TestApp.service にスクリプトを生成したら、systemctl を使用してこれを行うことができます。

問題は、このアプリケーションを画面内で実行したいので、システムにログインし、screen -r -D TestAppなどのコマンドを実行して接続できることです。

ただし、これを行うとsysmtemctlが失敗し、アプリケーションが起動しなくなります。

私が作成したスクリプトは次のとおりです。

[Unit]
Description=Script to start TestApp website on bootup

[Service]
ExecStart=/usr/bin/screen -S TestApp "/usr/bin/node  /opt/NodeApps/TestApp/mainApp.js"

[Install]
WantedBy=multi-user.target

これはsystemctlの状態と発生するエラーです。

sudo systemctl status TestApp.service 
TestApp.service - Script to start TestApp website on bootup
   Loaded: loaded (/etc/systemd/system/TestApp.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2020-09-13 09:35:21 UTC; 30s ago
 Main PID: 13012 (code=exited, status=1/FAILURE)

Sep 13 09:35:21 sp-webserver systemd[1]: Started Script to start TestApp website on bootup.
Sep 13 09:35:21 sp-webserver screen[13012]: Must be connected to a terminal.
Sep 13 09:35:21 sp-webserver systemd[1]: TestApp.service: Main process exited, code=exited, status=1/FAILURE
Sep 13 09:35:21 sp-webserver systemd[1]: TestApp.service: Failed with result 'exit-code'.

起動時に画面内で実行するように設定する最良の方法を提案してください。

ベストアンサー1

結局、私は直接問題を解決し、問題を解決しました。解決策は次のとおりです。

/etc/systemd/system/TestApp.service ファイルを生成します。

[Unit]
Description=Script to start TestApp website on bootup

# Listed below are three [Service] sections, we need only one of them. These three list our three different ways we can do this.
All three sections will start the processes as non root user 'testuser'. If we want them to start the processes as root, then we'd say User=root OR User="root" (not sure which one)

# The service section without the use of screen.
[Service]
WorkingDirectory=/opt/NodeApps/TestApp
ExecStart=/usr/bin/node /opt/NodeApps/TestApp/mainApp.js
Restart=always
RestartSec=3
User=testuser

# The service section with the use of screen as separate process, i.e. node will start inside screen, but  top will show screen and node as separate processes
[Service]
Type=forking
WorkingDirectory=/opt/NodeApps/TestApp
ExecStartPre=/usr/bin/screen -dmS TestApp
ExecStart=/usr/bin/node /opt/NodeApps/TestApp/mainApp.js
Restart=always
RestartSec=3
User=testuser

# The service section with the use of screen i.e. node will start inside screen, and top will show screen  as a process (node does not get a separate pid) 
[Service]
Type=forking
WorkingDirectory=/opt/NodeApps/TestApp
ExecStart=/usr/bin/screen -dmS TestApp /usr/bin/node /opt/NodeApps/TestApp/mainApp.js
Restart=always
RestartSec=3
User=testuser

[Install]
WantedBy=multi-user.target 

このファイルを保存したら、次の手順を実行します。

  1. ファイルの所有権を取得します。 (修道chown)
  2. 実行できないように権限を644に設定します(chmod 644)。

次のコマンドを使用してテストします。

sudo systemctl start TestApp.service
sudo systemctl stop TestApp.service
sudo systemctl status TestApp.service

満足している場合は、次のようにアクティブにして起動時に実行してください。

sudo systemctl enable TestApp.service

おすすめ記事