I'm modifying a pre-commit hook to check for config files that are being committed.
In my pre-commit I am running a python script. This script uses sp.run() to run any necessary bash commands.
Before, I could run the command in my pre-commit (outside of the python script) as
$SVNLOOK cat -t "$TXN" "$REPOS" /cfg/cfg.goto.lib.dev.yaml > /var/www/svn/repo/hooks/scripts/temp_cfg.yaml
This would work fine and would send the contents of the incoming config file to a temp file for parsing.
When I try to run this same command inside of an sp.run() statement, it fails and gives me a file not found error. I've tried prepending my statement with
/usr/bin/svnlook cat -t sys.argv[1] sys.argv[2] /cfg/cfg.goto.lib.dev.yaml > /var/www/svn/repo/hooks/scripts/temp_cfg.yaml
no luck there either.
Any idea why python doesn't think this file exists?
Related
I am trying to write a python script which would install a printer software on my machine using silent install.
The script is something like this and I run the script in a command line run as Admin-
cmd = 'PDFCreator-5_0_3-Setup.exe /COMPONENTS="program" /VERYSILENT /NORESTART'
response = subprocess.call(cmd, shell=True)
print('response:', response)
However, I would like to run this as an admin inside the script itself(as my script runs automatically as part of my code), but could not find a way yet. Help is much appreciated.
I have tried using the runAs option with Powershell, but when I use the above command with the Start-Process, I m getting syntax errors:
cmd_to_install = "& { Start-Process "pathofthefile+\PDFCreator-5_0_3-Setup.exe" -ArgumentList #("/COMPONENTS="program" /VERYSILENT /NORESTART") -Verb RunAs}"
well I'm trying to make a logger for my python script using a runner in a '.bat' format, that executes the script and saves an output file; without me having to do it manually.
when I tried to run my python script, script.py, and pass 20 as an argument for the script as well as redirecting the output to a log_file.txt, using windows command prompt, it worked just fine, and the log file was created.
~the cmd command:
python script.py 20 >> log_file.txt
But when I tried to run the same code using the runner ".bat" file it didn't work.
~The codes I've written inside the "runner.bat" is as follows
python script.py 20 >> log_file.txt
pause
~but the execution command is done by the bat file was-as seen from the screen-:
C:\Users\dahom\Desktop\folder>python script.py 1>>log_file.txt
I expected the ".bat" file to behave the same save the log_file as the cmd terminal.
But when I ran the bat file it didn't redirect the output to the log_file.txt
But it seems to be running the script, without but one indication that it takes some time for the script running.
note: both the batch file and the script are in the same folder/dir/path.
HERE is an image showing everything.
TRY:
#echo off
"C:\Users\dahom\AppData\Local\Programs\Python\Python37-32\python.exe" "C:\Users\dahom\Desktop\new.py" >> "C:\Users\dahom\Desktop\log_file.txt" & type "C:\Users\dahom\Desktop\log_file.txt"
pause
NEW.py:-
print("Echo Fox")
OUTPUT OF THE BATCH SCRIPT:-
Echo fox
Press any key to continue . . .
WORKING:-
Just Provide the full paths of each file used in the command (python exec, python script, text file etc). When the command get's pipe'd to file use & type "file_path" to display the contents of the file after writing it.
I want to run the exe file with command line arguments in Mac terminal
p1.exe -f input.txt
But im getting error -bash: p1: command not found
I have converted python file p1.py into p1.exe using
pyintsaller p1.py --onefile
And running the python file with arguments works
python p1.py -f input.txt
This isn't to do with Python, but is a basic command shell issue. To run an executable from the current directory, you need to use the ./ prefix.
./p1.exe -f input.txt
Note, it's a bit odd to use a .exe extension for a Linux executable.
Note that on Unix like systems (Linux/Unix/Solaris/MacOS). scripts can be run without explicitly invoking interpreter, if two conditions are meet:
script file starts with this line (or similar): #!/usr/bin/env python
file has executable attribute flag is set
Then you can run script like this:
./p1.py --onefile
./ means run thing from local directory. If this is not pressent the it tries to run things located by PATH variable, that is why you can run interpreter python
I have a python script that queries a database. I run it from the terminal with python3 myscript.py
I've added a cron task for it in my crontab file
*/30 9-17 * * 1-5 python3 /path/to/my/python/script\ directory\ space/myscript.py
The script imports a function in the same directory that parses login info for a database located in database.ini in the same directory. The database.ini is:
[postgresql]
host=my-db-host-1-link.11.thedatabase.com
database=dbname
user=username
password=password
port=10898
But currently cron outputs to the file in my mail folder:
Section postgresql not found in the database.ini file
The section is clearly present in the database.ini file, so what am I missing here?
Instead of running "python3 myscript.py" in the directory where it is present, try running it from some other directory (like home directory). Most likely you will see the same issue.
Note that cron's current-working-directory is different on different systems. So, the safest method is to explicitly switch to the directory where your script is and run the command there:
cd /path/to/my/python/script\ directory\ space/ && python3 myscript.py
Try this:
import os
...
change --> filename=database.ini
for --------> filename=os.path.dirname(__file__)+'/database.ini'
I have followed a few posts on here trying to run either a python or shell script on my ec2 instance after every boot not just the first boot.
I have tried the:
[scripts-user, always] to /etc/cloud/cloud.cfg file
Added script to ./scripts/per-boot folder
and
adding script to /etc/rc.local
Yes the permissions were changed to 755 for /etc/rc.local
I am attempting to pipe the output of the file into a file located in the /home/ubuntu/ directory and the file does not contain anything after boot.
If I run the scripts (.sh or .py) manually they work.
Any suggestions or request for additional info to help?
So the current solution appears to be a method I wrote off in my initial question post as I may have not performed the setup exactly as outline in the link below...
This link -->
How do I make cloud-init startup scripts run every time my EC2 instance boots?
The link shows how to modify the /etc/cloud/cloud.cfg file to update scripts-user to [scripts-user, always]
Also that link says to add your *.sh file to /var/lib/cloud/scripts/per-boot directory.
Once you reboot your system your script should have executed and you can verify this in: sudo cat /var/log/cloud-init.log
if your script still fails to execute try to erase the instance state of your server with the following command: sudo rm -rf /var/lib/cloud/instance/*
--NOTE:--
It appears print commands from a python script do not pipe (>>) as expected but echo commands pipe easily
Fails to pipe
sudo python test.py >> log.txt
Pipes successfully
echo "HI" >> log.txt
Is this something along the lines that you want?
It copies the script to the instance, connects to the instance, and runs the script right away.
ec2 scp ~/path_to_script.py : instance_name -y && ec2 ssh instance_name -yc "python script_name.py" 1>/dev/null
I read that the use of rc.local is getting deprecated. One thing to try is a line in /etc/crontab like this:
#reboot full-path-of-script
If there's a specific user you want to run the script as, you can list it after #reboot.