This article describes the procedure to automatically start mosquitto’s MQTT Broker when powering on the PLCNEXT AXCF 2152 (Firmware 2023.0).
Let’s Start!
Before Start..
At first I will try to build it according to the official podman documentation…
https://podman.io/blogs/2018/09/13/systemd.html
sudo systemctl enable redis.service sudo systemctl start redis.service |
systemctrlコマンドがないとPLCNEXTに怒られました。
An Error from PLCNEXT that feedback to me for not having the systemctrl command.
root@axcf2152:/opt/plcnext/# systemctl sh: systemctl: command not found |
MR Martrin said systemctrl is not supported by PLCNEXT.
https://www.plcnext-community.net/forum/#/discussion/1184/controlling-the-openvpn-service
Start From Example
But there are still plenty of ways to do it. This time, we will modify the init.d File so that podman can be started automatically.You can download the Example from this link.
https://github.com/plcnextusa/init.d_Example
Be careful!
Operate carefully. If you delete the wrong File or Folder, PLCNEXT will not be able to start.
Clone the Example
Download>Unzip the File from the previous Link and transfer all the File to PLCNEXT via SFTP Software such as WinSCP.
git clone https://github.com/plcnextusa/init.d_Example.git |
Installation
The destination is /opt/plcnext/.
Login as Root
Please login as root with SSH Client.
Change the permission
Edit permissions so that all Downloaded Files can be executed.
root@axcf2152:chmod +x install.sh python.sh helloworld.py |
Execute the Shell Script
Just run install.sh.
root@axcf2152:./install.sh |
Check the Log Files
In the Example, Python Script automatically runs when the CPU is started up and writes text to hello.log once in every 10 seconds.
root@axcf2152:/opt/plcnext/# ls logs/ Output.log default.sqlite plcnextapp.log uatrace.log uatrace_2.log uatrace_4.log watchdogDaemon Output.log.lock hello.log security.sqlite uatrace_1.log uatrace_3.log uatrace_5.log |
Open hello.log to check it out.
root@axcf2152:cat /opt/plcnext/logs/hello.log |
root@axcf2152:/opt/plcnext/# cat logs/hello.log [2023-04-18T21:49:22] INFO [root.<module>:16] Hello from init.d! [2023-04-18T21:49:32] INFO [root.<module>:16] Hello from init.d! [2023-04-18T21:49:42] INFO [root.<module>:16] Hello from init.d! [2023-04-18T21:49:52] INFO [root.<module>:16] Hello from init.d! [2023-04-18T21:50:02] INFO [root.<module>:16] Hello from init.d! |
Auto Start Mqtt Podman
Once the basic operation is confirmed, the next step is to configure the MQTT Broker to start automatically.
Broker images
First, build a Broker Container.
structure
Here is the Folder structure.
├── docker-compose.yml ├── mosquitto └── config └── mosquitto.conf |
docker-compose.yml
Here is the Context of the Docker File.
version: “3” services: mqtt: image: eclipse-mosquitto container_name: mqtt expose: – 1883 ports: – 1883:1883 restart: unless-stopped volumes: – ./mosquitto/config:/mosquitto/config |
mosquitto.conf
This will be the MQTT Broker Configuration File.
set_tcp_nodelay true listener 1883 allow_anonymous true max_queued_messages 0 |
change rights
Edit permissions so that all Files can be executed before Building Container.
chmod +x |
build
Build the images.
docker-compose up |
startup.sh
The Shell Script contains commands to start the MQTT Container, etc.
#!/bin/sh echo “Hello start” >> /opt/plcnext/log.txtpodman network create podmanpodman start mqttpodman start mqttpodman start mqttpodman start mynoderedecho “end…” >> /opt/plcnext/log.txt |
Update the python.sh
Now we need to edit the python.sh.
Change DAEMON=$DIR/startup.sh and set starup.sh as the file to run.
Change DAEMON_USER=root to set the User to run as root.
#!/bin/sh ### BEGIN INIT INFO # Provides: myservice # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Put a short description of the service here # Description: Put a long description of the service here ### END INIT INFO # Change the next 3 lines to suit where you install your script and what you want to call it DIR=/opt/plcnext DAEMON=$DIR/startup.sh DAEMON_NAME=hello2 # Change the uuser that runs the script if accessing files. Otherwise keep as admin. DAEMON_USER=root # The process ID of the script when it runs is stored here: PIDFILE=/var/run/$DAEMON_NAME.pid do_start () { # Starts the daemon (our python script) in the background with the configured DAEMON_USER start-stop-daemon –start –background –pidfile $PIDFILE –make-pidfile –user $DAEMON_USER –chuid $DAEMON_USER –startas $DAEMON — $DAEMON_OPTS } do_stop () { # Starts the daemon (our python script) based to the process id, after 10 retries it just kills the process start-stop-daemon –stop –pidfile $PIDFILE –retry 10 } #keys to call the script, e.g. ./pystart.sh start case “$1” in start|stop) do_${1} ;; restart|reload|force-reload) do_stop do_start ;; *) echo “Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart}” exit 1 ;; esac exit 0 |
Updating your init.d service
If you change Scripts, etc. again in the future, and do not want to restart the CPU, you can restart only Services with the following command.
/etc/init.d/python.sh restart |
Check MQTT Docker
Done! After restarting, the MQTT Broker started automatically.
root@axcf2152:/opt/plcnext/# podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 34fa7e1faf20 docker.io/library/eclipse-mosquitto:latest /usr/sbin/mosquit… 17 hours ago Up 18 minutes ago 0.0.0.0:1883->1883/tcp mqtt |
Uninstalling the example
To remove the Example File you have just installed, execute the following command.
update-rc.d –f python.sh remove rm -r python.sh |
Be careful!
Before deleting Services in the init.d of PLCNEXT, be careful to check where you are deleting them. If you delete the wrong services file, PLCNEXT will not be able to start.
What is inide?
Finally, Here is a brief description of the contents of Install.sh.
Install.sh
#!/bin/bash echo “Starting install process…. Please wait…” echo “Changing permissions for execution” chmod +x /opt/plcnext/helloworld.py chmod +x /opt/plcnext/python.sh echo “Setting up init.d” cp –a /opt/plcnext/python.sh /etc/init.d/ cd /etc/init.d/ update-rc.d python.sh defaults ./python.sh start rm -r /opt/plcnext/python.sh echo “Removing residual files” cd /opt/plcnext/ rm -r README.md rm -r install.sh echo “Log file for errors is hello.log in the /opt/plcnext/logs folder.” echo “Install complete!” |
“#! /bin/bash” is a special comment written on the first line of a shell script to indicate which program the shell should use to execute the script. In this case, it instructs the shell to use the Bash shell to execute the script.
#!/bin/bash |
Modify File’s execute permissions to allow execution.
chmod +x /opt/plcnext/helloworld.py chmod +x /opt/plcnext/python.sh |
Move File to /etc/init.d/ and change the working Directoy to /etc/init.d/.
cp –a /opt/plcnext/python.sh /etc/init.d/ cd /etc/init.d/ |
update-rc.d” is a management tool for init scripts used in Linux distributions such as Debian and Ubuntu. init scripts are programs that run automatically when the Linux system boots and are used to start or stop specific services running for each runlevel.
In the following Example, System will register the python.sh init script at the run level and create a symbolic link to automatically start python.sh when the system boots.
update-rc.d python.sh defaults ./python.sh start rm -r /opt/plcnext/python.sh |
最後は作業Directoryを/opt/plcnext/に戻り、不要なFileを削除する。
cd /opt/plcnext/ rm -r README.md rm -r install.sh |
python.sh
Here is the python.sh.
#!/bin/sh ### BEGIN INIT INFO # Provides: myservice # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Put a short description of the service here # Description: Put a long description of the service here ### END INIT INFO # Change the next 3 lines to suit where you install your script and what you want to call it DIR=/opt/plcnext DAEMON=$DIR/helloworld.py DAEMON_NAME=hello # Change the uuser that runs the script if accessing files. Otherwise keep as admin. DAEMON_USER=admin # The process ID of the script when it runs is stored here: PIDFILE=/var/run/$DAEMON_NAME.pid do_start () { # Starts the daemon (our python script) in the background with the configured DAEMON_USER start-stop-daemon –start –background –pidfile $PIDFILE –make-pidfile –user $DAEMON_USER –chuid $DAEMON_USER –startas $DAEMON — $DAEMON_OPTS } do_stop () { # Starts the daemon (our python script) based to the process id, after 10 retries it just kills the process start-stop-daemon –stop –pidfile $PIDFILE –retry 10 } #keys to call the script, e.g. ./pystart.sh start case “$1” in start|stop) do_${1} ;; restart|reload|force-reload) do_stop do_start ;; *) echo “Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart}” exit 1 ;; esac exit 0 |
Set the User to execute this file.
DAEMON_USER=admin |
Configure the PIDFILE.
PIDFILE=/var/run/$DAEMON_NAME.pid |
start-stop-daemon” is a tool to start, stop, and restart daemon processes on Unix-based operating systems. Daemon processes are resident processes that run in the background and are responsible for various tasks such as system administration and service provision.
- –start: Specifies that the daemon process is to be started.
- -b, –background: Specifies that the daemon process should run in the background.
- -m, –make-pidfile: Specifies that a process ID file is to be created.
- -p, –pidfile PIDFILE: Specifies the location of the process ID file.
- -c, –chuid USER:GROUP: Specifies the user and group to run the daemon process.
- -d, –chdir DIR: Specifies the directory where the daemon process will run.
- -e, –exec EXEC: Specifies an executable file to start the daemon process.
- -u, –user USER: Specifies the user to run the daemon process.
- -g, –group GROUP: Specifies a group to run the daemon process.
- -s, –signal SIGNAL: Specifies a signal to send to the daemon process.
- -k, –stop: Specifies that the daemon process should be stopped.
- -n, –name NAME: Specifies the name of the daemon process.
- -R, –retry RETRY: Specifies the number of retries.
- –oknodo: Assume success without doing anything if the process is already running.
- –startas STARTAS: Specifies the name of the new process.
- –test: Display debugging information without executing commands.
- –version: Display version information.
These options can be combined to start, stop, restart, and control the daemon process.
start–stop–daemon —start —background —pidfile $PIDFILE —make–pidfile —user $DAEMON_USER —chuid $DAEMON_USER —startas $DAEMON — $DAEMON_OPTS |
startup.sh
Simply describe the command to be executed.
#!/bin/sh echo “Hello start” >> /opt/plcnext/log.txtpodman network create podmanpodman start mqttpodman start mqttpodman start mqttpodman start mynoderedecho “end…” >> /opt/plcnext/log.txt |
Useful link
https://www.plcnext.help/te/Service_Components/Using_OCI_containers.htm