Check a Python Script Is Running
Not sure about what you are asking but as given this may help, so if you just want to call one python script from another then you can use script 1
#!/usr/bin/python
from subprocess import call
call(["python", "update.py"])
Save this file in a script named script1 and run it, it will compile update.py.
If you want to check for any syntax error in update.py then you can use script 2
#!/usr/bin/python
from subprocess import call
call(["python","-m","py_compile", "update.py"])
If script2 compiles without any error then it shows that there is no syntax error in your program.
Thirdly if you want to check if update.py is running currently or not you can use script 3
#!/usr/bin/python
import psutil
import sys
from subprocess import Popen
for process in psutil.process_iter():
if process.cmdline() == ['python', 'update.py']:
sys.exit('Process found: exiting.')
print('Process not found: starting it.')
Popen(['python', 'update.py'])
This last script will tell if your script is running or not and if it is not running it will compile it.
How does one identify if python script was run for the first time?
If you want to keep track of all the times your software was run
There really is no way without writing to a file every time you start, and then appending to this file. So that way, you'd simply go look up the file to keep track of your program.
If you want to see when the last time your software was compiled
I guess another thing you could technically do is check the date of your generated compiled python bytecodes, but this would only tell you the last time your program executed, not the first time or the total number of times.
This link shows you how to check for the creation/modification/update times
.
How to determine if Python script was run via command line?
I don't think there's any reliable way to detect this (especially in a cross-platform manner). For example on OS X, when you double-click a .py
file and it tuns with "Python Launcher", it runs in a terminal, identically to if you execute it manually.
Although it may have other issues, you could package the script up with something like py2exe or Platypus, then you can have the double-clickable icon run a specific bit of code to differentiate (import mycode; mycode.main(gui = True)
for example)
how can you know if the python file is run directly and not from the command line?
The second answer provided in Detect if python program is executed via Windows GUI (double-click) vs command prompt functions correctly for the program to check if the program was run thru a GUI works. Thanks for the help guys!
Check if current Python script is running in a Jenkins environment
When a Jenkins job executes, it always sets some default environment variables.
In your python code you can just check to see if one (or more) of these variables exists.
You can go for the JENKINS_URL
environment variable as it is quite unique and probably wont be used for any other purpose beside what you want to achieve.
So your code can look like:
debug_mode = 'JENKINS_URL' not in os.environ
print('Script is starting up')
(...) # Do stuff
if debug_mode:
print('So many things to do...')
(...) # Do other stuff
Related Topics
Test a Function Called Twice in Python
How to Plot in Real-Time in a While Loop Using Matplotlib
Python Flask Threaded True Not Working
How to Close an Internet Tab With Cmd/Python
Check If a Python Script Is Already Running in Windows
Extract File Name from Read_Csv - Python
Find All CSV Files in a Directory Using Python
How to Pass a .Txt File to a Function in Python
How to Populate New Column Based on Values in Other Columns
Output the Same Amount of Rows as Asterisks Using For-Loop
Keras + Tensorflow and Multiprocessing in Python
How to Get String Objects Instead of Unicode from Json
Executing Multiple Functions Simultaneously
Python - How to Fix "Valueerror: Not Enough Values to Unpack (Expected 2, Got 1)"
Opencv Typeerror: Expected Cv::Umat for Argument 'Src' - What Is This
Python: Requests.Exceptions.Connectionerror. Max Retries Exceeded With Url