Answer the question
In order to leave comments, you need to log in
How to automatically run a Python program after changing it in OS Linux?
Can you please tell me how to implement the launch of a Python program on Linux, when it changes? That is, the idea is this, I edit the Python program file on my host PC, save it, after saving it is automatically loaded via WinSCP to a remote Linux machine. But it would not be bad if the program, after changing it, immediately started with output to the console ...
I tried to implement this using the incron utility, ran it: ~# service incron status
● incron.service - file system events scheduler
Loaded: loaded (/lib /systemd/system/incron.service; enabled; vendor preset: enabled)
Active: active (running)
Registered in the configuration: incrontab –e
/tmp/test.py IN_MODIFY python3 /tmp/test.py
But for some reason, after editing the file, the program does not start.
Answer the question
In order to leave comments, you need to log in
In general, I have not yet come up with anything better than writing a Python program to run another Python program)))
And since I need all this for debugging, I think this will be enough for now:
import os, datetime, sys, time, subprocess, io
def tracking_program(path):
dateFile1 = os.stat(path)
while(1):
dateFile = os.stat(path)
if dateFile != dateFile1:
print("\n\n\n")
proc = subprocess.Popen("python3 %s" % path, shell=True, stdout=subprocess.PIPE, encoding='utf8')
out = proc.stdout.readlines()
if out:
for fOut in out:
print(fOut[:-1])
print("\n\n\n")
dateFile1 = os.stat(path)
time.sleep(5)
tracking_program(sys.argv[1])
You want what is called ci/cd. And in a good way, this is done like this:
1) edit the files on your computer, make a commit to the git repository
2) on the side of the git repository, when a commit is received, automation starts, which will deliver your code to the server and perform the necessary actions on it. In a simple case, it will copy the files to the right place and restart the program. In a more complex one, it will assemble the program into a container and run it. In even more difficult - at first tests will run.
because your case is quite simple, than the option is not suitable:
1) write a systemd unit
2) once you copy via scp, then you have ssh, just copy the file and then send systemctl restart your_program.service
systemd.path
PathChanged=
may be used to watch a file or directory and activate the configured unit whenever it changes.
Well, in the Exec* of the unit you write what you need.
if the demonicity of the launched one is needed, then you prescribe a reboot of this daemon.
Again, it's not clear what exactly you wanted from the output, because both cron and systemd run the script in a thread separate from the user.console
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question