Truenas core jail - startup python script exploding after 3 minutes

Thomas_VDB

Contributor
Joined
Sep 22, 2012
Messages
102
Hi all,

As the title says, I have created a small python script that reads a TCP socket, corrects the data and sends it back.
When I run the script in my jail via this command, it continues to run beautifully (until of course I close the session) :

Code:
/usr/local/bin/python3.9 /root/mbgateway.py


Now I have made a rc.d startup script to run in after the boot of the jail :
After creating the startup script, I did'nt forget to :
chmod +x
and then enable the service

Code:
#!/bin/sh
#
# PROVIDE: mbgateway
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="mbgateway"
rcvar="mbgateway_enable"

load_rc_config $name

start_cmd="${name}_start"
stop_cmd="${name}_stop"

mbgateway_start() {
    echo "Starting mbgateway"
    /usr/local/bin/python3.9 /root/mbgateway.py &
}

mbgateway_stop() {
    echo "Stopping mbgateway"
    # You might want to add logic here to stop the script gracefully
}

run_rc_command "$1"


Now when I run the jail, I can see (on the other end of the socket-comm) the python script is executing perfectly.
But after 3 minutes it stops.
What could be the issue?
It doesn't stop when I run the python script manually. (but then it only runs as long as the terminal session exists).
 

anodos

Sambassador
iXsystems
Joined
Mar 6, 2014
Messages
9,554
Hi all,

As the title says, I have created a small python script that reads a TCP socket, corrects the data and sends it back.
When I run the script in my jail via this command, it continues to run beautifully (until of course I close the session) :

Code:
/usr/local/bin/python3.9 /root/mbgateway.py


Now I have made a rc.d startup script to run in after the boot of the jail :
After creating the startup script, I did'nt forget to :
chmod +x
and then enable the service

Code:
#!/bin/sh
#
# PROVIDE: mbgateway
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="mbgateway"
rcvar="mbgateway_enable"

load_rc_config $name

start_cmd="${name}_start"
stop_cmd="${name}_stop"

mbgateway_start() {
    echo "Starting mbgateway"
    /usr/local/bin/python3.9 /root/mbgateway.py &
}

mbgateway_stop() {
    echo "Stopping mbgateway"
    # You might want to add logic here to stop the script gracefully
}

run_rc_command "$1"


Now when I run the jail, I can see (on the other end of the socket-comm) the python script is executing perfectly.
But after 3 minutes it stops.
What could be the issue?
It doesn't stop when I run the python script manually. (but then it only runs as long as the terminal session exists).
From standpoint of managing a python script via rc system, you can use `daemon(8)` to manage the application, write a pidfile, and lower privileges. See man daemon.
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
Put these in your script right below the initial comment section:
Code:
set -x
exec >/tmp/startup.log 2>&1


Then look for hints in /tmp/startup.log. Also consider using the "daemon" utility instead of just an ampersand to background the process. Could be something with your script wanting to output something but not having a controlling tty for example.
 

Thomas_VDB

Contributor
Joined
Sep 22, 2012
Messages
102
Put these in your script right below the initial comment section:
Code:
set -x
exec >/tmp/startup.log 2>&1


Then look for hints in /tmp/startup.log. Also consider using the "daemon" utility instead of just an ampersand to background the process. Could be something with your script wanting to output something but not having a controlling tty for example.
Just putting these 1 lines of code in my script, apparently helped.
The python script doesn't stop executing anymore (been waiting 10 minutes now).

Can this be explained?
 

Thomas_VDB

Contributor
Joined
Sep 22, 2012
Messages
102
Just putting these 1 lines of code in my script, apparently helped.
The python script doesn't stop executing anymore (been waiting 10 minutes now).

Can this be explained?
Too soon. It stopped again.

Can't find anything usefull in the log. The log just stops between 2 TCP packets.
 

Patrick M. Hausen

Hall of Famer
Joined
Nov 25, 2013
Messages
7,776
Is there a "global debug flag" for python? So it will print why it is terminating? That should end up in the log, then.
 
Top