Run python program on startup in background on Intel Galileo - python

I have a python program that is an infinity loop and send some data to my database.
I want this python script to run when I power my Intel Galileo. I tried to make a sh script python myprogram.py and made it run on startup in etc/init.d. When I restarted my Galileo, nothing happened-Linux didn't load, Arduino sketch didn't load and even my computer didn't recognize it.
I guess this happened because the python program was an infinity loop.
Is there a way that I can run my system without problems and run my python script on startup?

I made the myprogram.py run in background with python myprogram.py & and it worked. The & is used to run whatever process you want in background.

Related

Shell script kill command

OS: Raspbian
Python: 3.7.3
I am trying to run and kill my shell script through a Python script. The purpose is so that I can simply press "Run" in my py script, instead of having to go through the terminal every time. Here is my shell script (T.sh):
#!/bin/bash
#Track command
cd /home/pi/rpi-deep-pantilt
. . /rpi-deep-pantilt-env/bin/activate
rpi-deep-pantilt track Raspi --edge-tpu
Here is my Py script:
import os
os.system('bash /home/pi/T.sh')
When I issue the command rpi-deep-pantilt track Raspi --edge-tpu in my terminal and press CTRL + C it kills the script, but it doesn't work when I use this Python script, and neither does pkill. The Python script stops, but the camera stays on and the tracking functionality is still operating. Is there any way I can incorporate some kill command that I can issue with a key interruption?
If there is a better way to go about this let me know. I'm very new to this as you can probably tell.
Python may create a new process for T.sh, so within your python code, try:
os.system("pkill T.sh")

Running python script in background in windows 10

I am running a python in Windows 10 Command prompt. After the system is kept idle the script stop working. The system also had sleep mode turned off. I need to press "enter" key to make script working again. I tried windows scheduler, however it shows CMD pop up everytime script is executed. Please suggest solution to run the script without any human intervention. I am running the script in cmd with 'schedule' module for running after specific interval.

Cronjob on Ubuntu Mate for Raspberry stops right after execution

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?

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

Running python program in a silent mode

Usually, when I run a python program, I need to open a terminal and type things like
python program.py
But I want to run this python program without a terminal (or some window like that) running on Windows. But it will be running anyhow, just like Google Drive is always running on my PC without any windows being popped up. Is this possible? (I am making this program to run as soon as the Windows is booted up.)
If on Linux, try:
python program.py &
On windows there is an exe called sc.exe that would make services to run in the background.
Syntax looks like this:
sc [servername] create MYService binpath="c:\anaconda\bin\python.exe c:\scripts\myprog.py" start=auto
although I'm not sure if you have to put the binpath in quotes or just put that whole thing in a bat file and run that.

Categories