I'm new to Python and have been able to find answers to most of my questions here so I thought you guys could help me with this one:
I'm developing somewhat of a hybrid application using bash scripts to show menus and to call python applications (I'm doing it this way to simplify things).
My problem is that when I end the python application, it simply terminates the process and to go back to the menus, I have to start the whole program again. I tried using "subprocess.call('xxx')" but it opens the bash scrips inside of the application I am running and shows text (echo) only, no other functions.
Is there a way to end the python application first and then call the shell script?
You can wrap your code in a while-true loop
while :; do
# Assuming you want to show the menu before you start a program:
bash showMenu.py
python myScript.py
# When the above scripts exits the process will start all over again
# You might want to consider checking the exit code and only continue if the program exits with status code 0
# [ $? gt 0 ] && break
done
Related
I have a Python program that can update itself from a GitHub repository. When I activate the updating process, the Python script runs updater.bash script and kills itself. The bash script updates the program and then runs it again. But it keeps running despite I put exit 0 in the end of the updater. So, every update creates another bash script that eats more resources.
How can I kill the script after it runs the Python script?
exec python ... to replace bash with python program.
See: help exec
I'm trying to start two Python scripts that will run in an infinite loop.
The first script scrapes a webpage and dumps it into a CSV file.
The second script reads that CSV file and displays it on a webpage through Dash (localhost webserver).
With everything I've tried so far, it will run one script and wait for it to end before running the next (which doesn't work for me).
The only thing that has worked for me so far (which isn't optimal for production) is opening two Command Prompts and manually running each script in separate windows.
I've tried two buttons in PyQt.
I've tried a simple batch script (I'm on a Windows7 machine) with structure:
python file1.py &
python file2.py &
The functionality I need:
"Whilst the first script is scraping and dumping the web page, the other is at the same time reading and displaying it."
You can use start to launch both commands:
#echo off
start /b python file1.py
start /b python file2.py
A few notes about this: The commands will launch, but will not block the exit of the batch file, so unless you'll need to keep the command prompt open. Also, to kill them, use Ctrl-Break to stop the background processes.
It might be cleaner to use Python's multiprocessing to run the scripts, either as part of one of the scripts, or with a wrapper script, but that depends on your exact needs.
So you want to run file2.py after file1.py is completely finished its job?
If so, you can replace & with && so it would be something like this:
python file1.py && python file2.py
I wrote a python script that run an infinite loop and every half second checks if there are new files in a directory:
while True:
files = os.listdir(path_to_dir)
# do something
time.sleep(0.5)
The code runs on windows 10 in a cmd window and I need to make sure it will never stop.
A) I need to find a mechanism (or few mechanisms) that will restart the script in all possible scenarios that it might turn off (it is ok if the restart will happen only 2 minutes later...):
if the computer is restarting
if someone close the cmd windows
if the script end unexpectedly because of unhandle exception or memory leak (it is not suppose to happen...)
B) I want that once in a week, proactively,the script will be turn off and restart.
Any ideas?
Thanks!!
p.s. my first idea was that the python script will only check for new file, and the task scheduled will run it every second, but the minimum interval for task scheduler is 1 minute.
I think these answer all your questions:
Since your script runs on windows, you can use the Microsoft Task Scheduler (which is installed with Windows) to start your Python script when your computer starts up.
If you do not use the cmd window, you can change your Python script extention from .py to .pyw to run the script without a terminal window. A bit more on that here: Executing scripts.
For opening the script after an exception has happend, have a look at this blog post: How to Restart Python Script after Exception and Run it Forever.
To restart your script once a week, you can also use the Task Scheduler mentioned in answer 1. I think this post could help you with restarting your script: Start and stop a python task
Why is my Python script running twice in the background when I try to execute it at startup of my Raspberry Pi by adding the command to /etc/profile?
I have a command written at the end of the file /etc/profile for a Python script to run at startup of my Raspberry Pi, "sudo python /path/filename.py &", and for some reason it runs twice, every time. When I comment the line out and execute it manually from the command line it runs normally. Why does that happen and what can I do to prevent that from happening?
I know for fact that it is running twice in the background because in my code I have a buzzer that beeps twice at times and 3 times at others, and it beeps 4 times instead of 2 and 6 times instead of 3. Also the code ends up contradicting itself, clearly because each script run is trying to do something else at the same time.
Thanks in advance.
I'm answering my own question with a better method for running scripts at boot/startup.
I'm not exactly sure why this was happening, but I did learn that executing scripts on startup with this method is a bad practice and is best avoided.
I started using the Crontab instead.
This is what you need to do:
crontab -e
This opens up the crontab, then add the following line:
#reboot python /filelocation/filename.py
This will execute the script as soon as the Pi boots up.
No more double script runs!
Do you have VNC enabled? I think that's the problem. It was for me.
See this related discussion:
https://www.raspberrypi.org/forums/viewtopic.php?f=66&t=59285
So you can disable VNC, do the run levels danny suggested, create a new user (different to the one used for VNC) or start doing this sort of stuff in your script:
if [ x"$ALREADY_DONE" == x"" ]; then
export ALREADY_DONE=yes
foobar
fi
I'm having a curious problem. I have a bash script that is calling a python script within it. The python script executes successfully, but never fully terminates
Content of Bash script:
#! /usr/bin/env bash
python python_script.py
echo "bar"
content of Python script:
#Much stuff
sys.exit("The python script just ended")
What I expect to see on termination would be:
>The python script just ended
>bar
What I instead get is:
>The python script just ended
If I keyboard interrupt, the bash continues as:
^C>bar
What gives? Clearly the exit is calling properly, and there is nothing between that and the output statement in the bash script that called the python script.
(I can't necessarily give specifics on the workings of the "Much stuff" in the python script, as I'm modifying existing code that I don't fully understand. I've mostly left the workings of the script alone, and modified output more than anything for formatting, but I'm happy to try and provide you with any additional information requested)
What sys.exit() does is throw an exception of type SystemExit. If your script were to catch this exception, it would continue executing past sys.exit().
Also, if you have non-daemon threads, these could be preventing the process from terminating.
If that's the case, you can either turn them into daemon threads, or somehow signal to them that you wish to exit the script, and let them shut themselves down.
Finally, there's os._exit(), but you should not have to resort to that.
The following also works:
import os
print('Hellow world')
os._exit(os.EX_OK)
In Ubuntu, it exits the terminal. See also the example in this link Program With the os.exit() Method