CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

python subprocess call OF bash script

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By crubio.abujas

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 22, 2022, 03:02
Default python subprocess call OF bash script
  #1
Member
 
Join Date: Apr 2021
Posts: 41
Rep Power: 5
AlxB is on a distinguished road
Hello,
I am on ubuntu 20.04 Linux with Python 3.8.10 and OpenFOAM is v2112.
I believed a simple subprocess.call should allow to run a bash script from a python one. The python script is:
Code:
#!/usr/bin/env python3 

# -*- coding: utf-8 -*- 


import subprocess 

subprocess.call("./BashTest")
The BashTest script is:


Code:
#!/bin/sh 

cd "${0%/*}" || exit    # Run from this directory 

 . ${WM_PROJECT_DIR:?}/bin/tools/CleanFunctions  # Tutorial clean functions 



#--------------------------- 
 
cd case 

cleanCase0 

echo "BashTest " $(date +%s) > timeLog.txt
The bash script works from the terminal: the txt file is produced and the cleanfunction cleans what it has to.


The python script does not: the txt file is not produced. The cleaning is not done.

However if I remove line 3 of the bash script,
HTML Code:
. ${WM_PROJECT_DIR:?}/bin/tools/CleanFunctions
, which refers to where the openfoam cleanfunctions are located, then the python script produces the txt file.

Therefore what could be wrong here ?


Many thanks in advance for your help.
AlxB is offline   Reply With Quote

Old   October 20, 2022, 09:26
Default
  #2
Senior Member
 
Nico
Join Date: Jan 2022
Location: Germany
Posts: 122
Rep Power: 6
Hr_kules is on a distinguished road
Hey, have you ever found a solution? I am stuck with the same problem
Hr_kules is offline   Reply With Quote

Old   October 26, 2022, 02:51
Default
  #3
Senior Member
 
Carlos Rubio Abujas
Join Date: Jan 2018
Location: Spain
Posts: 127
Rep Power: 9
crubio.abujas is on a distinguished road
Can any of you describe more about the error message you got?

I've tried the code and works fine for me.

If the problematic line is this one
Code:
. ${WM_PROJECT_DIR:?}/bin/tools/CleanFunctions
It may happen that you dont have the openfoam environment loaded when launching the script. In that case you should get a "parameter not set or null" error on the command line. If you remove this line, the command cleanCase0 fails, but it continues the execution of the echo command, thus generating the file. You can avoid this behavior by appending " || exit" after the command, which terminate the execution if the command fails.

Try this code:
Code:
#!/bin/bash
cd "${0%/*}" || exit    # Run from this directory 

# Load the environment if not already loaded
if [ -z $WM_PROJECT_VERSION ]; then
    . /opt/OpenFOAM-v2012/etc/bashrc
fi

. ${WM_PROJECT_DIR:?}/bin/tools/CleanFunctions # Tutorial clean functions 

#-------------------------- 
 
cleanCase0 || exit

echo "BashTest " $(date +%s) > timeLog.txt
Hopes it help
Hr_kules likes this.
crubio.abujas is offline   Reply With Quote

Old   October 26, 2022, 03:37
Default
  #4
Senior Member
 
Nico
Join Date: Jan 2022
Location: Germany
Posts: 122
Rep Power: 6
Hr_kules is on a distinguished road
Hey Carlos,

this is exactly how i managed to solve the problem a few days ago!
Hr_kules is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[OpenFOAM] Paraview python script, creating data using only CLI, saving in csv/excel file Ash Kot ParaView 1 September 24, 2021 12:23
Slurm Bash Script for STAR-CCM+ Jonathan_DS Main CFD Forum 1 June 6, 2021 15:02
[OpenFOAM] Python Script not working Attribute Error Oguzhan90 ParaView 0 July 5, 2019 04:56
[General] Using time in Python script lingdeer ParaView 0 August 11, 2014 01:57
run openfoam with bash script Gosi OpenFOAM 2 April 11, 2013 09:10


All times are GMT -4. The time now is 14:49.