PLCnext起動/終了時のシェルスクリプト自動実行

Linux 用に作成した自作のシェルスクリプトやプログラムを PLCnext の起動/終了時に自動的に実行したい場合、init.d と update-rc.d を用います。

この記事は PLCnext 限定ではなく、init.d と update-rc.d コマンドを使用している Linux システム全般に適用可能です。

目次

手順概要

  1. PLCnext へ root でログイン
  2. 自動実行したいシェルスクリプトを /etc/init.d/ 直下へ配置し、権限を変更
  3. update-rc.d コマンドを実行し、シェルスクリプトを起動/終了時実行用のディレクトリへ登録
  4. PLCnext 再起動

この記事で紹介する方法で登録したシェルスクリプトはシステムの起動/終了の両方のタイミングで実行されます。条件をより細かく指定したい場合は、update-rc.d に関するサイト等をご参照ください。

手順詳細

root ログイン

STEP
admin ユーザとしてログイン

TeraTerm 等を用いて PLCnext へ admin ユーザとして SSH でログインしてください。
(admin のデフォルトパスワードは、PLCnext 本体表面に印字された 8桁の英数字です。)

STEP
root ユーザとしてログイン

さらに su コマンドで root ユーザとしてログインしてください。

例:su

PLCnext のデフォルト設定では root にパスワードが設定されていないため、root としてログインできません。
その場合は、Linux root ユーザー作成 | PLCnext Technology の記事をご参照ください。

シェルスクリプトの配置 (/etc/init.d/) と権限変更

ここでは、起動/終了時に自動実行したいシェルスクリプトのファイル名を boot_script.sh とします (ファイル名は任意です)。

シェルスクリプト内にユーザーからの対話的な反応を待つようなコマンド (例:readコマンドなど) を決して含めないでください。PLCnext の起動処理がブロックされてウォッチドッグが働き再起動がかかるようになるため、正常に起動できなくなります。
そうなった場合は、PLCnext のファームウエアをリセットしてください。
AXC F x152 リセット手順 | PLCnext Technology

STEP
/etc/init.d へシェルスクリプトを配置

boot_script.sh を /etc/init.d/ へ配置してください。

例:cp boot_script.sh /etc/init.d/

[パソコン上でシェルスクリプトを作成した場合]
権限の都合により、パソコンから SFTP や SCP を用いて PLCnext の /etc/init.d/ 配下へファイルを直接コピーすることは出来ません。
その場合はいったん /opt/plcnext/ 等 admin ユーザが書き込み権限を持つディレクトリの下へ転送したのち、ターミナルソフト上で root ユーザとして /etc/init.d/ 配下へ配置してください。

STEP
権限の変更

chmod コマンドを実行し、必要な権限を自動実行したいシェルスクリプトへ与えてください。

例:chmod -v 777 /etc/init.d/boot_script.sh 

シェルスクリプトの登録 (update-rc.d)

update-rc.d コマンドを実行し、シェルスクリプトを起動/終了時実行用のディレクトリへ登録してください。

例:update-rc.d boot_script.sh defaults 99

“99”はブート時のシーケンス番号 (1~99) です。数値が小さいほど優先順位が高くなります。
自動実行したいシェルスクリプトが他の機能に依存している場合、依存先の機能よりも後に実行されるよう優先順位に注意を払う必要がありますが、99としておけばこの心配がなくなります。

シェルスクリプトの登録解除は
update-rc.d <シェルスクリプト名> remove
例:
root@axcf2152:/opt/plcnext/# update-rc.d boot_script.sh remove
Removing any system startup links for boot_script.sh …
/etc/rc0.d/K99boot_script.sh
/etc/rc1.d/K99boot_script.sh
/etc/rc2.d/S99boot_script.sh
/etc/rc3.d/S99boot_script.sh
/etc/rc4.d/S99boot_script.sh
/etc/rc5.d/S99boot_script.sh
/etc/rc6.d/K99boot_script.sh

PLCnext 再起動

reboot コマンドでPLCnext を再起動してください。

例:reboot

シェルスクリプトの例①:起動/終了時に単純な処理を実行

単純な処理の例として日時・ランレベル・ファイル名をファイル出力するシェルスクリプトを作成し、システム起動/終了時に自動実行させます。

シェルスクリプトの作成

/opt/plcnext/ 直下に、以下の内容で boot_script.sh を作成してください。

#!/bin/bash

echo $(date)', runlevel = ['$(runlevel)'], File "'$(basename $0)'" was executed.' >> /opt/plcnext/sample.txt

シェルスクリプトの権限変更と手動でのテスト実行

以下の様に boot_script.sh の権限を変更し、正しく動くかどうかを手動でチェックします。

root@axcf2152:/opt/plcnext/# chmod -v 777 boot_script.sh
mode of 'boot_script.sh' changed from 0664 (rw-rw-r--) to 0777 (rwxrwxrwx)
root@axcf2152:/opt/plcnext/# ./boot_script.sh
root@axcf2152:/opt/plcnext/# cat /opt/plcnext/sample.txt
Wed Apr 10 07:39:51 UTC 2024, runlevel = [N 5], File "boot_script.sh" was executed.

黄色でハイライトしたとおり、/opt.plcnext/sample.txt に日時から始まるメッセージが出力されていれば OK です。

シェルスクリプトを起動/終了時実行用ディレクトリへ登録

以下の様に boot_script.sh を /etc/init.d/ 配下へコピーし、update-rc.d コマンドで起動/終了時実行用ディレクトリへ登録します。

root@axcf2152:/opt/plcnext/# cp boot_script.sh /etc/init.d/
root@axcf2152:/opt/plcnext/# update-rc.d boot_script.sh defaults 99
 Adding system startup for /etc/init.d/boot_script.sh.

再起動してシェルスクリプトの自動実行を確認

reboot コマンドで PLCnext を再起動してください。

root@axcf2152:/opt/plcnext/# reboot

Broadcast message from root@axcf2152 (pts/0) (Wed Apr 10 05:35:19 2024):

The system is going down for reboot NOW!

PLCnext が起動したら、SSH でログインして /opt/plcnext/sample.txt の内容を確認してください。

admin@axcf2152:~$ cat /opt/plcnext/sample.txt
Wed Apr 10 07:39:51 UTC 2024, runlevel = [N 5], File "boot_script.sh" was executed.
Wed Apr 10 07:43:03 UTC 2024, runlevel = [5 6], File "rc" was executed.
Wed Apr 10 07:43:33 UTC 2024, runlevel = [N 5], File "rc" was executed.

黄色でハイライトしたとおり、終了時 (runlevel = [5 6]) と起動時 (runlevel = [N 5]) にシェルスクリプトが実行されたことがわかります。

runlevel [5 6] は、「前回のランレベルは5, 現在のランレベルは6」を意味します。
runlevel [N 5] は、「前回のランレベルはN (なし), 現在のランレベルは5」を意味します。
詳細は、Linux や runlevel コマンドに関するサイト等をご参照ください。

シェルスクリプトの例②:起動時にPythonプログラムのバックグラウンド実行を開始

プログラムのバックグラウンド実行の例としてPython のプログラムを作成し、起動時にシェルスクリプトから Python プログラムのバックグラウンド実行を開始させます。

シェルスクリプトの第1引数 ($1) には、起動時は “start”、終了時は “stop” が格納されています。

Python プログラムの作成

10 秒ごとにファイルへ文字列を出力する Pytyhon プログラムを用意します。

下記のテキストファイルを interval_write.py として /opt/plcnext/ 配下に用意してください。

import os
import time

def write_current_datetime(output_filename, script_filename):
    while True:
        current_datetime = time.strftime("%Y-%m-%d %H:%M:%S")
        with open(output_filename, "a") as file:
            file.write(f"[{current_datetime}] from {script_filename}\n")
        time.sleep(10)

if __name__ == "__main__":
    script_filename = os.path.basename(__file__) # Script file name
    output_filename = "/opt/plcnext/output.txt"  # Output file path
    write_current_datetime(output_filename, script_filename)

Python プログラムを手動でテスト実行

STEP
Python プログラムのバックグラウンド実行

以下の用に Python のプログラムをバックグラウンド実行してください。

admin@axcf2152:~$ python3 interval_write.py &
[3] 13173
STEP
出力ファイルのモニタ

以下の様に、出力ファイルの内容を tail コマンドでモニタしてください

日時で始まる文字列の行が 10 秒ごとに増えていけば成功です。

admin@axcf2152:~$ tail -f /opt/plcnext/output.txt
[2024-04-10 09:19:05] from interval_write.py
[2024-04-10 09:19:15] from interval_write.py
[2024-04-10 09:19:25] from interval_write.py
^C

モニタリングを終了するときは、Ctrl + C を押してください。

STEP
pkill コマンドによる Python プログラムの終了

以下の様に pkill コマンドで Python のプログラムを終了させてください。

root@axcf2152:/opt/plcnext/# pkill -f interval_write.py
[1]+  Terminated              python3 interval_write.py

シェルスクリプトの作成

システム起動時に Python プログラムのバックグラウンド実行を開始するシェルスクリプトを用意します。

下記の内容をboot_script2.sh として /opt/plcnext/ 配下に用意してください。

#!/bin/bash

echo $(date)', runlevel = ['$(runlevel)'], File "'$(basename $0)'" was executed.' >> /opt/plcnext/sample2.txt

case "$1" in 
    start)
        # Process for Startup
        echo "    System started." >> /opt/plcnext/sample2.txt
        python3 /opt/plcnext/interval_write.py &
        echo "    Python program started." >> /opt/plcnext/sample2.txt
        ;;
    stop)
        # Process for Shutdown
        echo "    System stopped." >> /opt/plcnext/sample2.txt
        echo "    Did nothing." >> /opt/plcnext/sample2.txt
        ;;
    *)
    echo "    Usage: $0 {start|stop}"
    exit 1
esac

exit 0

赤くハイライトした部分が Python プログラムのバックグラウンド実行を指示する箇所です。

シェルスクリプトの権限変更と手動でのテスト実行

以下の様に boot_script2.sh の権限を変更し、正しく動くかどうかを手動でチェックします。

青でハイライトしたとおり、シェルスクリプト名の後に引数 “start” をつけるのを忘れないでください。

root@axcf2152:/opt/plcnext/# chmod -v 777 boot_script2.sh
mode of 'boot_script2.sh' changed from 0664 (rw-rw-r--) to 0777 (rwxrwxrwx)

root@axcf2152:/opt/plcnext/# ./boot_script2.sh start
root@axcf2152:/opt/plcnext/# cat /opt/plcnext/sample2.txt
Wed Apr 10 10:19:04 UTC 2024, runlevel = [N 5], File "boot_script2.sh" was executed.
    System started.
    Python program started.
root@axcf2152:/opt/plcnext/# tail -f /opt/plcnext/output.txt
[2024-04-10 10:19:04] from interval_write.py
[2024-04-10 10:19:14] from interval_write.py
[2024-04-10 10:19:24] from interval_write.py
^C

黄色でハイライトしたとおり、/opt.plcnext/sample2.txt に日時から始まるメッセージが出力されていれば OK です。

また、赤でハイライトしたとおり、/opt/plcnext/output.txt の行が 10 秒ごとに増えていけば OK です。

tail によるモニタリングを終了するときは Ctrl + C を押してください。

シェルスクリプトを起動/終了時実行用ディレクトリへ登録

以下の様に boot_script2.sh を /etc/init.d/ 配下へコピーし、update-rc.d コマンドで起動/終了時実行用ディレクトリへ登録します。

root@axcf2152:/opt/plcnext/# cp boot_script2.sh /etc/init.d/
root@axcf2152:/opt/plcnext/# update-rc.d boot_script2.sh defaults 99
 Adding system startup for /etc/init.d/boot_script2.sh.

再起動してシェルスクリプトとPythonプログラムの自動実行を確認

reboot コマンドで PLCnext を再起動してください。

root@axcf2152:/opt/plcnext/# reboot

Broadcast message from root@axcf2152 (pts/0) (Wed Apr 10 10:00:24 2024):

The system is going down for reboot NOW!

PLCnext が起動したら、SSH でログインして /opt/plcnext/sample2.txt の内容を確認してください。

admin@axcf2152:~$ cat /opt/plcnext/sample2.txt
Wed Apr 10 10:19:04 UTC 2024, runlevel = [N 5], File "boot_script2.sh" was executed.
    System started.
    Python program Started.
Wed Apr 10 10:22:39 UTC 2024, runlevel = [5 6], File "rc" was executed.
    System stopped.
    Did nothing.
Wed Apr 10 10:23:09 UTC 2024, runlevel = [N 5], File "rc" was executed.
    System started.
    Python program started.

黄色でハイライトしたとおり、システム起動時にシェルスクリプトが実行され、そこから Python プログラムが実行されたことがわかります。

続いて、tail コマンドで /opt/plcnext/output.txt の内容をモニタしてください。

admin@axcf2152:~$ tail -f /opt/plcnext/output.txt
[2024-04-10 10:23:19] from interval_write.py
[2024-04-10 10:23:29] from interval_write.py
[2024-04-10 10:23:39] from interval_write.py

日時で始まる文字列の行が 10 秒ごとに増えていけば Python プログラムがバックグラウンドで動作しています。

以上が、システム起動/終了時に任意のシェルスクリプトを実行する基本的な手順です。

  • URLをコピーしました!
目次