I am using Debian and I have a python script that I would like to run during rc.local so that it will run on boot. I already have it working with a test file that is meant to run and terminate.
The problem is that this file should eventually run indefinitely using Scheduler. It's job is to do serial reads, a small amount of processing on those reads, and inserts into a MySQL database. However, I am nervous about then not being able to cancel the script to get to my login prompt if changes need to be made since I was unable to terminate the test script early using Ctrl+C (^C).
My hope is that there is some command that I am just missing that will accomplish this. Is there another key command that I'm missing that will terminate the python script and end rc.local?
Thanks.
EDIT: Another possible solution that would help me here is if there is a way to start a python script in the background during boot. So it would start the script and then allow login while continuing to run the script in the background.
I'm starting to think this isn't something that's possible to accomplish so other suggestions to accomplish something similar to what I'm trying to do would be helpful as well.
Thanks again.
Seems like it was just a dumb mistake on my part.
I realized the whole point of this was to allow the python script to run as a background process during boot so I added the " &" to the end of the script call like you would when running it from the shell and viola I can get to my password prompt by pressing "Enter".
I wanted to put this answer here just in case this would be something horribly wrong to do, but it accomplishes what I was looking for.
Making scripts run at boot time with Debian
Put your script in /etc/init.d/. So, if your script is in a file called my_script, it should be located at /etc/init.d/my_script.
Run update-rc.d my_script defaults as root.
Don't forget to make your script executable and include the shebang. That means the first line of the script should be #!/usr/bin/python.
Related
I'm fairly new to Python.
I'm running a python script on Linux named "locationGPIO2.py". I'm trying to write a separate script to monitor whether "locationGPIO2.py" is running at all times.
I want this separate script to basically be looping and checking if "locationGPIO2.py" is running, if it is, then do nothing, but if it is no longer running (either by user stopping the script or by crashing) then change a variable named 'heartbeat' to "Inactive".
I've been looking around for a solution for a while, but can't find anything that works. Could anybody help me implement this into my script?
Here's what I imagine the script should do:
ENTER CHECK IF SCRIPT IS RUNNING CODE HERE
If locationGPIO2.py is NOT running THEN:
heartbeat = "Inactive"
Else:
Do nothing
I have a python3.9 script I want to have running 24/7. In it, I use python-daemon to keep it running like so:
import daemon
with daemon.DaemonContext():
%%script%%
And it works fine but after a few hours or days, it just crashes randomly. I always start it with sudo but I can't seem to figure out where to find the log file of the daemon process for debugging. What can I do to ensure logging? How can I keep the script running or auto-restart it after crashing?
You can find the full code here.
If you really want to run a script 24/7 in background, the cleanest and easiest way to do it would surely be to create a systemd service.
There are already many descriptions of how to do that, for example here.
One of the advantages of systemd, in addition to being able to launch a service at startup, is to be able to restart it after failure.
Restart=on-failure
If all you want to do is automatically restart the program after a crash, the easiest method would probably be to use a bash script.
You can use the until loop, which is used to execute a given set of commands as long as the given condition evaluates to false.
#!/bin/bash
until python /path/to/script.py; do
echo "The program crashed at `date +%H:%M:%S`. Restarting the script..."
done
If the command returns a non zero exit-status, then the script is restarted.
I would start with familiarizing myself with those two questions:
How to make a Python script run like a service or daemon in Linux
Run a python script with supervisor
Looks like you need a supervisor that will make sure that your script/daemon is still running. You can take a look at supervisord.
I have a python program which is instantier as a linux service.
This service updates itself by downloading a new version of the code on an ftp server and launches a bash file to update the service.
In this file I have a line that destroys the current service before recreating it with the new source code.
I run this bash script with:
subprocess.call("sudo bash /home/pi/install.sh",shell=True)
I understand that this "subprocess" lives in my python program. And the bash script stop the linux service so stop the python program so stop itself ... And so it never ends.
What are the solutions to solve my problem?
I think there's several ways to do it - one of them being (maybe not the most elegant?) to make your python schedule a cron-job of the bash-script using python-crontab.
Say it's 13:00 and you want your job to run - then make the python script schedule a cron-job to 13:05 (just to add a time buffer).
You can then remove your cron-job after the bash-job has been run, either manually or implement it in your bash-script (or make it call a python script which uses python-crontab to remove it, it's fairly easy to do so)
Don't let the script stop the service. Just let it exit with a specific exit code if it installed a new version, and restart the service accordingly in the Python code.
I'm still new to writing scripts with Python and would really appreciate some guidance.
I'm wondering how to continue executing my Python script from where it left off after a system restart.
The script essentially alternates between restarting and executing a task for example: restart the system, open an application and execute a task, restart the system, open another application and execute another task, etc...
But the issue is that once the system restarts and logs back in, all applications shut down including the terminal so the script stops running and never executes the following task. The program shuts down early without an error so the logs are not really of much use. Is there any way to reopen the script and continue from where it left off or prevent applications from being closed during a reboot ? Any guidance on the issue would be appreciated.
Thanks!
Also, I'm using a Mac running High Sierra for reference.
You could write your current progress to a file just before you reboot and read said file on Programm start.
About the automatic restart of the script after reboot: you could have the script to put itself in the Autostart of your system and after everything is done remove itself from it.
Currently i am working on a project and i am using django-kronos for scheduling so, when user schedule a task i am running a script and end of the script i need to run
python manage.py installtasks
this is help to insert the tasks in crontab.
script is working fine when i execute in terminal but when i integrate it with my django app, it's throwing an error
IOError: Read crontab nobody: You (nobody) are not allowed to use this program (/usr/bin/crontab)
I really curious to know about which cause this problem?
or did i miss anything?
Setting setuid flag on the script (chmod u+s) will make the script run as the script file's owner UID if it's run as a separate process. You then need to watch who is allowed to execute it and/or do some authentication or check of the running user as with anyone else executing it, it'll be the same.
This is pretty much a workaround and is not advisable if some better facility is available in the app.
From what I can see, django-kronos logic is supposed to be run from manage.py rather than web code proper.