I'm trying to do some tests dumping data from one database to another with mysqldump.
The mysqldump is set in PATH, and the command runs perfectly in the CMD interface, or via a .cmd.
It seems to run ok in python wrapped in a simple try/except block, but I don't get any result in the target database.
Working with:
MariaDB 10.1 & 10.5 /
Python 3.9
The command looks similar to this:
mysqldump --no-create-info --no-create-db --user root -p****** --port=some_port -h 127.0.0.1 some_database some_table |
mysql --user root -p***** --port=some_port -h 127.0.0.1 target_table
What I've tried is different variations on Popen (also trying to handle the pipe via subprocess, and splitting the args), but starting with the simplest soltuion given elsewhere on stackoverflow:
subprocess.Popen("mysqldump --no-create-info --no-create-db --user root -p****** --port=some_port -h 127.0.0.1 some_database some_table |
mysql --user root -p***** --port=some_port -h 127.0.0.1 target_table", shell=True)
I also made a .cmd file and ran this in windows and it works fine. Passing it to either Popen() or os.system() in python gives the same problem, seems to check out in a try/except, but no result seen in the target database.
What could be the problem, and how do I get this right?
Solved for anyone who wants to know.
Put the absolute paths in the command with the short names generated for non-8dot3 file names like so:
'C:\\PROGRA~1\\MARIAD~1.1\\bin\\mysqldump.exe --no-create-info --no-create-db --user root -p****** --port=some_port -h 127.0.0.1 some_database some_table |
C:\\PROGRA~1\\MARIAD~1.1\\bin\\mysql.exe --user root -p****** --port=some_port -h 127.0.0.1 some_database'
It's my work laptop where they only give permissions to add to PATH on my user account, and not the entire system. Got my Python configured to look at system PATH.
Was a bit confusing to see it work in the command line and a .cmd script, but in no way in Python. And when running, not telling me it isn't "recognized as an internal command..." when in other cases it does.
Related
I have a simple python script which I want to start a daemon-service in background in docker container
/sbin/start-stop-daemon --start --user root --make-pidfile --pidfile /var/lock/subsys/my-application.pid --exec 'python /opt/app/uc/monitor/bin/my-application.py'
when I execute this command in a shell I get
/sbin/start-stop-daemon: unable to stat //python /opt/app/uc/monitor/bin/my-application.py (No such file or directory)
However when execute just the below command in shell it works
python /opt/app/uc/monitor/bin/my-application.py
I'm sure the python is installed and all the links have been setup.
Thanks for the help
That error message implies that start-stop-daemon is looking for a file to open (the stat operation is a check before it opens the file) and treating your 'python ... ' argument as if it was a file.
See this example which confirms this. You may need to read the man page for start-stop-daemon, for your Ubuntu version, to check what a valid command would be for your setup.
Simplest solution is probably to create a shell script (say /opt/app/uc/monitor/bin/run-my-application.sh), and put this into it:
#!/bin/bash
python /opt/app/uc/monitor/bin/my-application.py
Be sure to do chmod +x on this file. If python is not found, use which python to find the path to python and use that in the script.
Now try:
/sbin/start-stop-daemon --start --user root --make-pidfile --pidfile /var/lock/subsys/my-application.pid --exec '/opt/app/uc/monitor/bin/run-my-application.sh'
I am running Django unit tests against a multithreaded app. Often a thread hasn't terminated by the time the unit test finishes, so the test database cannot be deleted. When I next run the tests, I get the message:
Type 'yes' if you would like to try deleting the test database 'test_appname', or 'no' to cancel`
The create_test_db autoclobber option is the functionality I want, but how can I use that? I can't find any examples or clues. I'm working in the PyCharm IDE, which is pretty configurable. I just want to delete the test database silently every time.
I'm putting tests in Transaction TestCase classes, running setup_test_environment() then Client().post(reverse(etc..))..
In Pycharm django tests, you can enable the option input and enter --noinput
see screenshot below
If you're using PyCharm and want to run a single test using the green arrow but you keep getting this error, you can modify the default django tests configuration template, so that you don't have to keep setting the --noinput option on each.
If you are using flush in some other way, (eg, on the pre-existing development database like here), the --noinput option supresses the user prompt, eg:
from django.core.management import call_command
call_command('flush', '--noinput')
My answer was to create a script like this:
export PGPASSWORD=the_password
if [[ `psql -h 127.0.0.1 -d postgres -U username -p 5432 -tAc "SELECT 1 FROM pg_database WHERE datname='test_djangoprojectname'"` == "1" ]]
then
psql -h 127.0.0.1 -d postgres -U username -p 5432 -a -w -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'test_djangoprojectname' AND pid <> pg_backend_pid();"
psql -h 127.0.0.1 -d postgres -U username -p 5432 -a -w -c "DROP DATABASE test_djangoprojectname;"
fi
The -d setting is database name - it can be any database your user has access to, except the one you are deleting.
The default username is postgres.
The -p setting is the port your database is on - 5432 is the default.
Save as (for example) del_test_db.sh (Windows users see below), then
chmod +x del_test_db.sh
Then in PyCharm:
Run, Edit Configurations...
Unfold Defaults, click Django tests
In the Before launch window click +, External tools, click +
Under Program, select your file del_test_db.sh, give the command a name (eg, 'del test db') and click OK.
Select your tool in the list and click OK
You may need to unfold Django tests in the left and delete existing test configurations
Then the script force deletes the test database before every run.
This works on Mac OS X and Ubuntu etc. For Windows the process is the same, except instead of export use SET, save the commands as a .bat file instead of .sh, and you don't need to chmod +x, and use the following syntax for the IF statement in the batch file:
if 'command' == '1' (
...
)
Apologies I'm unable to check this as I don't have a Windows machine.
Thanks to this answer for the code checking whether the database exists.
I just wanted to install a PostgreSQL Database. After 3 hours of trying I do not know what else to do. My last try included installing PostgreSQL via Homebrew -> Works perfectly fine.
But typing this:
which psql
I got this: /usr/local/bin/psql
From my view this sort of Path is wrong, a I saw a different one in most tutorials. But I have no idea what to do.
But I went on trying:
createuser -U postgres yrkIO -P
And the terminal asked me for a password only to give me this:
createuser: could not connect to database postgres: FATAL: role "postgres" does not exist
What can I do, I just want to run a PostgreSQL on my Python Flask App?
Have you tried it without forcing a password?
createuser -s -r postgres
This worked for me.
Also, remember to start a server
Start:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Stop server:
pg_ctl -D /usr/local/var/postgres stop -s -m fast
Deployment by using Python throws error:
I used Python code ( its your deploy.py) to deploy our proxy (our company proxy) into apigee platform. i read http://apigee.com/docs/api-services/content/deploying-proxies-command-line
but it throws error when i run "python api-platform-samples-master/tools/deploy.py -n apikey -u "yusuf.karatoprak#mobgen.com:Welcome#2014" -o yusufkaratoprak123 -e test -p / -d sample-proxies"
i would like to solve this situation. i added to python code it is not working. it throws me Error: name 'ZipFile' is not defined
The -d flag value needs to point to the directory that contains the /apiproxy directory for the sample you want to deploy. (In your command above, it appears that you are pointing at /sample-proxies, rather than, for example, /sample-proxies/apikey
Try using the deploy scripts. There is one in each sample proxy directory. There's a also a script, /setup/deploy_all.sh if you want to deploy all sample proxies.
Make sure you update /setup/setenv.sh before running the deploy scripts.
The error is in how you are calling it from the command line. You have a space in one of the parameters you pass in, which needs to be put inside of quotes. Turn -u yusuf karatoprak:123 into -u "yusuf karatoprak:123"
Fixed command line call:
python api-platform-samples-master/tools/deploy.py -n weatherapi -u "yusuf karatoprak:123" -o yk123 -e test -p / -d simpleProxy
Python crontab script doesnt seem to work. When i run it manually,
python /home/ec2-user/code1.py
it works fine but when put into cron.txt file for the crontab, doesnt.
My crontab file is:
#hourly python /home/ec2-user/code1.py >/dev/null 2>&1
i also tried
0 * * * * python /home/ec2-user/code1.py >/dev/null 2>&1
But neither have much luck.
sudo crontab -l
#hourly python /home/ec2-user/code1.py >/dev/null 2>&1
Shows everything functional.
I tried Crontab not running my python script and couple others with not much luck either.
EDIT:
With
PATH=/opt/python2.7/bin
MAILTO=my#email
*/5 * * * * /home/ec2-user/code1.py
Email i get is:
/bin/sh: /home/ec2-user/code1.py : No such file or directory
Yet I can open and edit the file no problem. I tried many different thing but it comes down to this: cron doesnt see the file.
Feels like I went through entire https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
and still no luck
Verify cron is running: ps aux | grep [c]ron should show a running cron process
Remove the redirects from the command so that cron emails you the output
Add a MAILTO=<email address> to your crontab, so that you get the email
Put the full path to python (/opt/python2.7/bin/python) instead of just python in the command
Add another command to crontab such as echo FOOBAR and verify that you get the email.
ls -l /homeec2-user/code1.py ? Should that be /home/ec2-user/code1.py
Only ever edit a user's crontab with crontab -e never from another platform, or by editing the file directly.
Run crontab -l | cat -A so that we can verify all the control characters are correct.
did you check the following points?
is your script executable? chmod 700 code1.py
the first line in your code should be, in most cases the python is installed at this place
#!/usr/bin/python
after that the crontab as follow should execute
0 * * * * /home/ec2-user/code1.py >/dev/null 2>&1
If the error message is correctly copy/pasted, it seems to reveal that there is a problem with the crontab file. If you created it on a foreign platform, it might be best to start over with an empty file, this time creating it in a native editor.
As others have already pointed out, redirecting output and errors to /dev/null basically makes debugging impossible, so don't do that. If your program creates copiously verbose uninformative output, run it in a wrapper which filters out the trivial diagnostics, or, if it is your own program, rewrite it to run silently in normal operation.
Did you try "/usr/bin/python" instead of "python"?
ps ax | grep python
will give you the path you could use.
try this command that should hopefully where your python is :
which python
very likely you will have something like
/usr/bin/python /home/ec2-user/code1.py