Start Python Script on Jail Start

davep1553

Cadet
Joined
Jan 11, 2020
Messages
6
It's more complicated then it sounds. I've been googling and trying various things on and off for a week now.

I need to run a python script I developed when my jail starts. Problem is I need to run the script in a bash window and inside a virtual environment. I have tried, tcsh won't run it successfully.

I created an rc.d script and determined rc.d scripts only seam to run in tcsh (or something similar) and won't run my python script.

I created a sh script and from an rc.d script had it invoke bash and pass it the path to the script. When I manually evoke my rc.d script from tcsh this works but not when the jail starts, I put other logic in the rc.d script (echo to a file) and can confirm my rc.d script runs commands before and after my call to bash but additional echo to file commands in the sh script do not execute.

The Python script is doing some house keeping then starting some Damon processes so it doesn't have to run forever but I do want it to start automatically when the jail starts.

Any help will be appreciated.

/etc/rc.d/MyMaitance
Code:
#!/bin/sh

. /etc/rc.subr

name="MyMaitance"
start_cmd="${name}_start"
stop_cmd=":"

MyMaitance_start()
{
    echo Start Function Start >> /mnt/temp/file1.txt
    bash /MyMaitance/MyMaitanceRunner.sh >> /mnt/temp/file1.txt
    echo Start Function End >> /mnt/temp/file1.txt
}

load_rc_config $name
run_rc_command "$1" 


/MyMaitance/MyMaitanceRunner.sh
Code:
#!/bin/sh

echo Sh Script Start >> /mnt/temp/file1.txt

cd /MyMaitance  >> /mnt/temp/file1.txt
source venv/bin/activate  >> /mnt/temp/file1.txt
MyMaitance -hk  >> /mnt/temp/file1.txt
MyMaitance -start  >> /mnt/temp/file1.txt

echo Sh Script End  >> /mnt/temp/file1.txt


file1.txt after I start the jail
Start Function Start
Start Function End
 
Last edited:

tprelog

Patron
Joined
Mar 2, 2016
Messages
297
You're on the right track, have a look here for details



... but I do want it to start automatically when the jail starts.

This example may help get you... started

/usr/local/etc/rc.d/MyMaintenance
Code:
#!/bin/sh
#
# PROVIDE: MyMaintenance
#

. /etc/rc.subr

name=MyMaintenance
rcvar=${name}_enable

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

load_rc_config ${name}

: ${MyMaintenance_enable:="NO"}
: ${MyMaintenance_msg="Hello World"}

MyMaintenance_start() {
  echo "${MyMaintenance_msg}" > /root/message.txt
}

run_rc_command "$1"


To enable this rc.d script to run when the jail starts, you can use
Code:
sysrc MyMaintenance_enable=yes




Problem is I need to run the script in a bash window and inside a virtual environment.

I am by no means an expert scripter, but I've have written a few rc.d scripts for python applications running inside a virtualenv. You may also find this example helpful. Here, I've added a simple test command, run using bash, that will activate the virtualenv and show the python version being used.

/usr/local/etc/rc.d/MyMaintenance
Code:
#!/bin/sh
#
# PROVIDE: MyMaintenance
# REQUIRE: LOGIN
#

name=MyMaintenance
rcvar=${name}_enable

. /etc/rc.subr && load_rc_config ${name}

: ${MyMaintenance_enable:="NO"}
: ${MyMaintenance_msg:="Hello World"}
: ${MyMaintenance_venv:="/root/venv"}

extra_commands="test"


start_cmd="${name}_start"
MyMaintenance_start() {
  echo "${MyMaintenance_msg}" > /root/message.txt
}

stop_cmd=":"

test_cmd="${name}_test"
MyMaintenance_test() {
  echo -e "\nTesting virtualenv...\n"
  # shellcheck disable=SC2016
  /usr/local/bin/bash -c '
    source ${1}/bin/activate
    echo "$(python --version)"
    deactivate
  ' _ ${MyMaintenance_venv}
  echo
}

run_rc_command "$1"


You would likely set the desired path to your virtualenv inside the rc.d script but I'd like to point out, you can also set this path using
Code:
sysrc MyMaintenance_venv=/path/to/virtualenv
 
Top