I have a quite simple question about starting and restarting python scripts inside an bash script. I hope it's not an duplicate but I didn't find a similiar question. I'm using a bash script which starts a couple of python scripts every time I create a docker container, but It should be probabaly the same on all linux based machines. It's simple as: python3 /mnt/device/script.py &
Now the script will keep running and measure the brightness until the container is stopped. Now I figured out that there are some problems with the sensor library which are brightly discussed on github and still not solved which causes the script to stop every couple of hours. For me It would be enough to have just some command which restarts my script which is running in the background every x hours to avoid that error. So what I'm looking for ist some command which will be like the follwing: python3 for hour=1 /mnt/device/script.py restart &
Thanks in advance!
You can use the Bash timeout command to send a signal to kill the Python script.
restart=""
while true; do
timeout 3600 python3 /mnt/device/script.py $restart
restart="restart"
done &
I'm guessing you don't really need this to run in the background (so maybe take out the &) and that you literally want restart to be passed as an argument after the first time.
Related
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
I have written a python (python version is 3) script that runs 24/7. The way I run my script in my Windows machine is the following. I right click on the .py file, then click on "Edit with IDLE" and then "Run". The script has no issue but, due to the many instructions printed in the python shell (I use a logger), after a couple of days this python shell gets very heavy. My newbie question is the following. Is there to limit the number of rows temporarily saved in the python shell to a specific number? Or perhaps somebody has a better suggestion to run this constantly running script that prints a lot of the script steps in the shell? Please, notice how I'm not asking how to run a script 24/7, it's my understanding the best way to do it is though a VPS. My problem is that the data saved in the displayed python shell gets bigger and bigger every day, so I only wonder how to limit the data temporarily displayed/saved in it. Thanks
I use a RaspberryPi 3 with UbuntuMate 16.04. On it, I want to start a little Python (3.5) program every midnight. For that I call a little shell script, so that I can change into the wanted directory comfortably.
crontab:
5 0 * * * /path/to/script/start.sh
start.sh (yes, it's executable):
#!/bin/bash
cd /path/to/wanted/workingDir/
python3.5 ControllerQueue.py
#also tried: python3.5 ControllerQueue.py &
Now if I execute the programm or script from the terminal, everything runs fine. But if I use the crontab it starts the script and stops right after. I also tried running the programm directly but get the same outcome. The paths are correct as I copied the workingDir path from the crontab-file and started it via the terminal.
Is there something I overlook?
As stofvl suggested, I saved the error output of my shell script. It turns out that I needed to add a display. My programm is divided into two scripts. One which provides a GUI and the other main application. The script only starts the main application, without the GUI, but it seems that this didn't matter.
This discussion helped me solve the problem.
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
Through an application I have made with Tkinter, I'm trying to add a command to run a script every week. When the program is closed the command should be in forever place.
I've sifted through the documentation on cron, but there doesn't seem to be a way to edit the crontab without using the shell. Also I've looked through the 'at' command, but that only seems to run once.
My question is - How can one create a weekly recurring task by issuing a single command in Python on Unix?
If not with only 1 command, can I use multiple?
In most modern Linux distros like Debian or Ubuntu, you can add an executable file (like a shell script or a symlink to one) into /etc/cron.weekly and it will be automatically run once a week for you. This is using the anacron command, which is fairly common these days.