Monit not invoking python script <--> OS is CentOS - python-3.x

Monit not invoking python script <--> OS is CentOS. first line in python script is "#!/usr/bin/env python3" when i tried to invoke python script from my terminal its working but monit is not able to trigger the script.
I tried to call python script from shell script in monit but no luck. even i tried adding PATH variable as second line to shell script.
any help will be appreciated.

Issue is with PATH environmental variable, like cron, monit is only taking subset of values from PATH variable instead of all values. So after explictly adding $PATH variable after shell interpreter, issue got resolved

Related

CRON job is not executing Amass exe as a subprocess?

On Ubuntu 20.04, I have setup amass.exe to detect subdomains through the CRON system, but Amass is only working through manual execution of .py script, not through CRON.
Installation of Amass as follows -
sudo apt update
Sudo apt install snapd
sudo snap install amass
# amass.exe added to PROJ ROOT DIR.
The subdomains.py script invokes the amass exe through a subprocess call as follows:
import subprocess
domain = 'somedomain'
cmd = f'amass enum -passive -d {domain} -json {domain}.json'
subprocess.run(cmd, shell=True)
# on running subprocess.. a JSON file is created which is not taking place through CRON job.
Rest all other cron jobs with sub-processes are working fine except for Amass, where the .exe file permissions might not be accessible to the CRON handler.
Would you please advise what issue it could be in Amass script? Thanks.
Environmental Variables Missing
I don't know if the answer is still needed, but check if you've placed the proper environmental variable in the cronjob.
If not given, then just give the 'PATH' variable and put the cronjob again.
Also, you can add the Path in the python code, in case you don't wanna give the path in cronjob by using sys.path.insert(0,"") (before the subprocess statement).

Execute python executable and script inlucing full path

Im trying to create a python script with executes a series of python and sql scripts.
Some need to be executed using one python executable and some using another.
I've tried
from subprocess import call
call([r"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\pythonw.exe", r"C:\Path\to\python\file\blabla.py"])
And
call([r"cd C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3", "pythonw.exe", r"C:\Path\to\python\file\blabla.py"])
But the script isnt being executed.
I think the problem might be "How to execute an .exe file in windows terminal including full path to it"?
Please check my code. It is working well in my side.
from subprocess import call
call([r"C:\Python38\python.exe", r"E:\python\hello.py"])
As i think, the problem is that you use pythonw.exe.
if you use pythonw.exe, you can see any log in terminal.
Pythonw is useful for running script with GUI.

Issue tying to read environment variable in Python

Hi I'm trying to read an environment variable in python, if I execute my script from console with:
python3 myScript.py everything goes ok, but I need to run this script as a service in Ubuntu, in this case, the script can't get the environment variable. Anyone has past for the same issue? I noticed that when i'm trying to read the mongo URI from environment.
The correct way to achieve that is passing the environment variable or environment file to the service file, in my case I added this line to my service file:
EnvironmentFile = /etc/environment

Running two shell commands using Python 3.6 on Ubuntu 18.02

In order to get some software running I need to 1. Run a script that will execute a remote license manage, 2. Execute a shell script to start the software. I can do this by opening a command window in the directory with the rlm , and then type ./rlm to run the Linux executable. Then I can go into the directory that contains the shell script, open a terminal in that location and run ./myshell.sh. This opens the GUI for my software.
I would like to run these steps using a single Python script. I have tried:
#change the working directory...
os.chdir(r'/opt/mysoftwarelocation')
#confirm location change...
print(os.getcwd() )
#run ./rlm...
os.system('./rlm')
At this point I can see from a python terminal that the rlm is running.
I would then like to run the code below to run the shell script...
os.chdir(r'/opt/mysoftwarelocation/sumsubdirectory')
print(os.getcwd() )
os.system('./some.sh')
Unfortunately, after os.system('./rlm') finishes the script stalls and will not execute further and without an errors.
How to I get the second part of my script to run within a single Python script?
Have you tried to run the rlm command in the background?
subprocess module gives a nice interface for that

How to run .sh file from python program Ubuntu

I am trying to launch a .sh script from Python 3.3 in Ubuntu 13.10.
The script is supposed to shutdown the computer. I have already marked the sh script as executable through the terminal. I have tried to run the sh script through: os.system("script.sh"), subprocess.Popen("Script.sh"), and subprocess.call([script.sh]).
They keep returning the: OSError Exec format error.
Any help would be greatly appriciated!
I assume that script.sh isn't in your PATH but in your current working directory.
By default os.system and subprocess look in your path for the requested executable. So to execute something in your current working directory you need to specify the executable like this:
subprocess.call("./script.sh")
The ./simply says that the executable that should be executed is in the current working directory.

Resources