solmoller / eversolar-monitor

Script to capture data and create statistics from Eversolar/zeversolar Solar Inverters. Includes easy install image files for Raspberry Pi. Working edition since 2012 :-)
https://github.com/solmoller/eversolar-monitor/blob/wiki/Introduction.md
MIT License
30 stars 20 forks source link

Port '3837' occupied by another process after a few hours of successful run of eversolar.pl #73

Closed tobiasnorbo closed 1 month ago

tobiasnorbo commented 2 months ago

Hi,

I was succesful in installing the newest (legacy) OS for my RPI2, and install all the needed packages one by one, and getting it to work. I had some initial issues with the 3837 port being occupied, because I initiated the eversolar.pl script before I was completely done with editing the eversolar.ini file. I had to kill the process that was occupying the port. Everything was fine. The sensors also appeared in HA (although I get the same error as described in Issue #59 By LeighAS here: https://github.com/solmoller/eversolar-monitor/issues/59#issuecomment-1339713998 Where It cannot be used in the Energy tab).

The issue remaining here is, that it seems that I can provoke the "port 3837 occupied" error by checking the localhost:3837 from different browsers. So I tried running the script again after killing the processes occupying it, but without opening the server from localhost:3837, but only looking at the data through HA.

This seemed to work, but it appears, that after a few hours, I will get the same issue, where the port is occupied, thus not giving me any data. I tried to reset everthing 3 times during daylight hours today, but all with the same result.

ChatGPT has ideas on how to fix it ( but that is for now last resort), and I will also go through the script on @nagydavid repository to check if there is any differences there to the one on the main repository.

But some help from here would also be greatly appreciated. Maybe you had this too and know of a fix?

Below is the terminal output:

`YY@XXXXXX:~/eversolar-monitor $ cd ~/eversolar-monitor sudo perl eversolar.pl 2024-05-05 19:57:38: Severity 1, Configfile is: eversolar.ini Update old database version, or print 2 fail messages (part 1) : DBD::SQLite::db do failed: duplicate column name: vpv2 at eversolar.pl line 882. DBD::SQLite::db do failed: duplicate column name: ipv2 at eversolar.pl line 883. Done updating old database version.
Update old daily database version, or print 2 fail messages (part 2) : DBD::SQLite::db do failed: duplicate column name: pmax_today at eversolar.pl line 926. DBD::SQLite::db do failed: duplicate column name: pmax_time at eversolar.pl line 927. Done updating old daily database version.
bind to :3837: Address already in use at eversolar.pl line 947. child unresponsive for 5 seconds at /usr/share/perl5/HTTP/Server/Simple.pm line 261. YY@XXXXXX:~/eversolar-monitor $ sudo lsof -i :3837 sudo kill -9 [PID] COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME perl 21418 root 8u IPv4 96929 0t0 TCP :3837 (LISTEN) kill: failed to parse argument: '[PID]' YY@XXXXXX:~/eversolar-monitor $ sudo kill -9 21418 YY@XXXXXX:~/eversolar-monitor $ cd ~/eversolar-monitor sudo perl eversolar.pl 2024-05-05 19:58:20: Severity 1, Configfile is: eversolar.ini Update old database version, or print 2 fail messages (part 1) : DBD::SQLite::db do failed: duplicate column name: vpv2 at eversolar.pl line 882. DBD::SQLite::db do failed: duplicate column name: ipv2 at eversolar.pl line 883. Done updating old database version.
Update old daily database version, or print 2 fail messages (part 2) : DBD::SQLite::db do failed: duplicate column name: pmax_today at eversolar.pl line 926. DBD::SQLite::db do failed: duplicate column name: pmax_time at eversolar.pl line 927. Done updating old daily database version.
SolarWebServer: You can connect to your server at http://localhost:3837/ 2024-05-05 19:58:20: Severity 1, Connecting to the serial port 2024-05-05 19:58:38: Severity 1, done registering known inverters 2024-05-05 19:58:40: Severity 1, Cleaned received serial number: BS20006ZZZZZZZ. 2024-05-05 19:58:42: Severity 1, Inverter acknowledged registration 2024-05-05 19:58:44: Severity 1, Connected to inverter: 1 2000E1.00 ZL 2000S ZEVERSolar BS20006ZZZZZZZ 2024-05-05 20:03:56: Severity 1, BS20006ZZZZZZZ lost contact with inverter, forgetting inverter 2024-05-05 20:04:52: Severity 1, Cleaned received serial number: BS20006ZZZZZZZ . 2024-05-05 20:05:07: Severity 1, Cleaned received serial number: BS20006ZZZZZZZ . 2024-05-05 20:05:09: Severity 1, Inverter acknowledged registration 2024-05-05 20:05:23: Severity 1, Cleaned received serial number: BS20006ZZZZZZZ . 2024-05-05 20:12:02: Severity 1, Cleaned received serial number: BS20006ZZZZZZZ. 2024-05-05 20:12:04: Severity 1, Inverter acknowledged registration 2024-05-05 20:12:06: Severity 1, Connected to inverter: 1 2000E1.00 ZL 2000S ZEVERSolar BS20006ZZZZZZZ 2024-05-05 20:16:24: Severity 1, BS20006ZZZZZZZ lost contact with inverter, forgetting inverter 2024-05-05 20:16:44: Severity 1, Cleaned received serial number: BS20006ZZZZZZZ . 2024-05-05 20:16:46: Severity 1, Inverter acknowledged registration

`

tobiasnorbo commented 1 month ago

Just a small update. It seems that the script terminates, when I terminate the terminal I started it from. I'm therefore starting the script from inside the RPI2 now, without shutting it down. I will report back how this goes. Otherwise it might make sense to add functionality in the .ini file, for the script to start on boot, so there is no need to start it through the terminal?

tobiasnorbo commented 1 month ago

Update and closing of issue.

It seems the issue was, that I should not run the script from the terminal on my mac, but directly from the RPI. So for everyone experiencing this issue. Open the RPI from a virtual machine, such as Microsoft Remote Desktop, open a terminal within the RPI UI, and run the script from here. Remember to kill the PID that occupies the port before you run the script.

I also got some suggestions for fixing the issue from ChatGPT, that I will leave below, if you want to try that out. It seems that there is methods to still run the script from your computer terminal and ssh access, but I have not tested it, so please do this on your own risk.

Directly copied from ChatGPT:

To ensure your script continues to run in the background and doesn't terminate or encounter issues when the terminal is closed, you can use various methods to run it independently of your login session:

  1. Using nohup Command The nohup command allows a command to continue running in the background after logging out from a shell. It prevents the command from being terminated automatically when the terminal is closed.

bash cd ~/eversolar-monitor nohup sudo perl eversolar.pl > /dev/null 2>&1 & This command will:

Run eversolar.pl in the background (& at the end of the command). Redirect standard output (stdout) and standard error (stderr) to /dev/null so that they don't clutter your terminal or nohup.out file. nohup ensures the script isn't killed when the terminal closes.

  1. Using screen or tmux These tools allow you to create a detachable session that keeps running after the terminal is closed. They are particularly useful if you want to come back later and view the output directly.

Install screen or tmux if not already installed:

bash sudo apt-get install screen

or

sudo apt-get install tmux

Start a new detached session:

bash screen -S eversolar

or

tmux new -s eversolar

Run your script:

bash cd ~/eversolar-monitor sudo perl eversolar.pl

Detach from the session: bash screen: Press Ctrl-a followed by d. tmux: Press Ctrl-b followed by d.

To reattach: bash screen: screen -r eversolar tmux: tmux attach -t eversolar

  1. Creating a System Service If your script is meant to run permanently as a service, consider configuring it as a system service managed by systemd. This ensures it starts on boot and is managed by the system's service manager, which also handles crashes and logins independently:

Create a systemd service file (/etc/systemd/system/eversolar.service):

ini

[Unit] Description=Eversolar Monitor Service After=network.target

[Service] Type=simple User=root WorkingDirectory=/home/pi/eversolar-monitor ExecStart=/usr/bin/perl eversolar.pl Restart=always

[Install] WantedBy=multi-user.target

Enable and start the service:

bash sudo systemctl daemon-reload sudo systemctl enable eversolar.service sudo systemctl start eversolar.service

  1. Ensure Clean Exit and Port Release Make sure your script properly handles termination signals to clean up resources like network ports when it's told to shut down. This often involves trapping signals in your Perl script and implementing a clean shutdown procedure.

By using one of these methods, you can ensure your script runs continuously in the background and maintains its functionality independently of your terminal session. This should resolve the issue of the script stopping or the port becoming occupied when you terminate the terminal.