Laptop A has python file "file1.py". Computer B has python file "file2.py". I want to remotely enter into Computer B and run the python script file2.py. I am using SCREEN, and below is my code.
import os
import time
os.system('screen -S Test -d -m /dev/ttyUSB0 57600')
time.sleep(1)
os.system('screen -S Test -X stuff "file2.py"')
time.sleep(1)
os.system('screen -S Test -d -r')
time.sleep(0.25)
print "done"
How did you try to run your 'remote' script?
You can try ssh session:
ssh user#computer_b 'python file2.py'
Of course, you should provide a full path to your file2.py script and maybe (I'm not 100% sure) a full path to a Python executable on your remote Computer B. Another option is to make your file2.py executable, by adding a Python "shebang line" as the first line of your file2.py script and setting executable bit via chmod +x file2.py:
Should I put #! (shebang) in Python scripts, and what form should it take?
Related
experts, i have a 50 nos of python scripts that i want to run one by one on the same folder.But problem is that in the system where i work many people also use it very often.so there is a possibility of misuse of my python scripts.so i want to copy the 50 nos of python scripts to the same folder and delete the python scripts(script1.py,script2.py...) from the same folder after running the master script which content all the running scripts.so i did something like as below
#!/bin/sh
python script1.py
python script2.py
python script3.py
python script4.py
python script5.py
python script6.py
python script7.py
python script8.py
python script9.py
python script10.py
....
but in the above script problem is that if i delete the python scripts only first python script i.e. python script1.py is running not others.I hope experts will help me overcoming this problem.
You are doing it wrong.
You should write a main.py program which will execute your functions one after the other.
On the way that you are currently using only first one is executed as cli is passed to the script1.py and other are ignored if your script do not exit.
So for example if you made a infinite loop error then yeah other will never run.
I would suggest learning python imports and execution.
One way is:
# main.py you have the script1.py and others in same folder.
# you need to have the scripts inside of the functions so you could as well do that in same file.
# so script1 should look like:
def script1_func(): # any arguments that are needed can be passed
# code of your script here
return value # if your script returns some value write it there
# then in main.py
import script1
import script2
import script3
if __name__ == '__main__':
script1.script1_func()
script2.script2_func()
script3.script3_func()
# on that way you can execute them all in sequence. If needed to use paralelism check multithreading.
# if you need to remove them or any other files.
import os
basedir = os.path.abspath(os.path.dirname(__file__))
# this will give you a path to the file and then you just issue delete
if os.path.exists(basedir + "script1.py"):
os.remove(basedir + "script1.py")
else:
print("The file does not exist")
os.rmdir(basedir) # this will delete whole folder
Btw it is better to use better conventions and do not duplicate similar functions.
https://gist.github.com/MarkoShiva/7584151b08a095a1708bdee339f61a65
Other way to do the same is to run a exec on each of the files from bash which might lead to too much load of the machine if you have more scripts then cores.
That is other answer.
So other user suggested you using find command to execute scripts in parallel.
For your exact needs, you can use below solution
I have create 2 sample python files and a shell script to invoke both the scripts.
➜ 65976750 ls
sandex.sh script1.py script2.py
Debug mode execution
➜ 65976750 bash -x sandex.sh
+ mkdir ./sandexec
+ find . -name '*.py' -exec cp '{}' ./sandexec ';'
+ python3 ./sandexec/script1.py
running python script 1
+ python3 ./sandexec/script2.py
Running from script2
+ rm -rf ./sandexec
original script
➜ 65976750 cat sandex.sh
mkdir ./sandexec
find . -name "*.py" -exec cp {} ./sandexec \;
python3 ./sandexec/script1.py
python3 ./sandexec/script2.py
rm -rf "./sandexec"
➜ 65976750 ls
sandex.sh script1.py script2.py
➜ 65976750
i´ve a few problems with my python3 scripts.
an php script start an python3 script:
$comando = 'python3 /var/www/html/tmp/' . $usersession . '-newtenent-vcenter1.py';
shell_exec("/usr/bin/nohup ".$comando." >/dev/null 2>&1 &");
the python3 script write a few strings to an new created text file.
After all thinks are done, the script sould be start the next python3 script:
os.system('python3 /var/www/html/tmp/' + usersession + '-newtenent-cucm1.py')
BUT, python3 start the script "cucm1.py" and close it imeadlety! The script shoud be open an ssh session with paramiko.
The OS is an Ubuntu 18.x. I´ve added the www-data user to the script directory (so all scripts can be executed by the user www-data):
www-data ALL=(ALL) NOPASSWD: /usr/bin/python3 /var/www/html/
BUT, when we execute the first python3 script from the linux shell (as root) it work´s fine (the second script working fine).
any idea? THANK YOU!
Using ubuntu's 16.04 crontab and #reboot to run python3 script. The script runs properly on reboot as I see the logged output. However, my script's os.system command is not running. It runs fine if ran outside of crontab. My scripts are all executable.
crontab -l output:
SHELL=/bin/bash
#reboot nohup /usr/bin/python3 -u /home/path/scheduler.py >> /path/log.out &
scheduler.py code:
#...(check if web server is running...if not restart)
os.system('nohup /usr/bin/python3 -u /path/webserver/main.py &')
print('this function ran')
When I logged the output of the os.system command , there was no output.
As a side note, I am running python schedule commands to check the general health of a webserver. crontab doesn't seem to be the right tool for this so I just use crontab to start my python scheduler on reboot.
I am using flask as the webserver, and would use gunicorn and systemctrl if I could get it to work... but it didn't so this is my workaround.
The point is that, the command called by os.system is not in default path.
For example, tcpdump is not in /usr/bin/.
So, you can solve the problem by adding the full path of the command.
I was facing the same issue when we try to run python script directly in crontab it just by passes the os.system() commands.
Make launcher.sh:
#!bin/bash
cd /home/pi/
sudo python example.py
Then, make your script executable:
chmod 755 launcher.sh
And at last, add your script to crontab:
crontab -e
and add this line at the end:
#reboot sh /home/pi/launcher.sh
(I set the program to run at each reboot)
So, I have a python script as follows:
#! /bin/bash
import os
def addword(run):
os.system("gnome-terminal --working-directory=../dictionary -e 'python3 addto.py'")
The spawned terminal disappears immediately when I run addword(0) which should, to my knowledge, run addword as if it were a program. Is there a simple way to fix this? There is a 'dictionary' directory a directory higher than where this is being executed.
I'm on Ubuntu 15.04
I'm currently playing around with home networking and getting into servers and I found a python command that will make the directories of you python folder avaliable over your LAN. To execute it in windows, I've just been entering the following into Command Prompt:
cd..
cd..
cd Python33
python -m http.server 8000
This creates the server that can be accessed via x.x.x.x:8000. Instead of manually entering these commands every time, however, I tried to make a batch script that would do this automatically. Below is the script for the batch file. The problem I'm having, is When I run the batch file, command prompt is saying 'python' is not recognized as an internal or external command. I was wondering if someone could tell my why this is happening and offer a solution or reason as to why this couldn't work. Thanks.
#ECHO OFF
cd..
cd..
cd Python33
python -m http.server 8000
pause
EDIT:
Below is the code that works, this will also set the directory to the C drive.
#ECHO OFF
cd C:\
C:\Python33\python -m http.server 8000
Maybe it's because the batch file is located in a directory that is not 2- level deep (C:\path\to\batch.bat)
Specifying the path of the python executable will solve your problem.
#ECHO OFF
C:\python33\python -m http.server 8000
pause
Try this:
#ECHO OFF
cd..
cd..
cd Python33
python -m SimpleHTTPServer 8000
pause