Cronjob on Ubuntu Mate for Raspberry stops right after execution - python

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.

Related

Automatically run graphical python script on startup in ubuntu

I have a python script that starts a fullscreen animation when run - how do i set this to start at startup in ubuntu 16.04? Other answers to the "script on startup" problem don't work - because I'm assuming they're executed before the desktop environment has a chance to start up...
I have a script called "startup.sh" that reads as:
xrandr --output HDMI-0 -s 1280x1024
source activate demo
cd ~/demo/
python run.py
Where the first line sets the correct resolution - the second line activates an anaconda environment, the third line changes directory (parts of the script need it to be in that directory, and the 4th line finally runs the script that launches a fullscreen graphic (using pygame, if it matters.)
How can i set this to auto run after unity finishes setting up? Thanks so much... any help or comments at all are really appreciated...
EDIT: it looks like someone here had a similar problem: https://www.raspberrypi.org/forums/viewtopic.php?t=128602
They wanted to start a pygame script on startup - and were able to do so after their desktop environment loaded by putting a call to their program in ~/.config/lxsession/LXDE-pi/autostart - I'm not using LXDE - i'm on unity, so is there an equivalent?
Easiest way to run things at startup is to add them to the file /etc/rc.local.
But to do this properly, you should use the update-rc.d command. You may try it like this:
update-rc.d /path_to_script/script_name.sh start 2
Or use update-rc.d /path_to_script/script_name.sh defaults to create both startup and kill scripts all at once.
For more precise actions you can visit here for details information: http://manpages.ubuntu.com/manpages/xenial/man8/update-rc.d.8.html.

How to run a python script with cron on LINUX

I'm trying to run a python script on my raspberrypi using cron.
I did the following:
crontab -e # To edit a crontab job
After the cron file opened, I added the following line:
#reboot /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log # JOB_ID_!
If I understand the documentation correctly, this cron job should be executed every time after the system booted up. However in my case, when I reboot the computer, the script will not be executed.
What's strange:
I checked the log file and it's empty, so it seems like everything goes fine
If I run the given command manually (so basically write the following code to the terminal) it executes and works correctly: /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log
I guess I missed something really obvious but I can't see it. Please can I ask for any advise how to debug this. Thanks!
The cron definition looks correct; I just checked this on my Pi running Debian stretch and it works OK:
#reboot /usr/bin/python /home/pi/example.py > /home/pi/mylog.log
Some other possible reasons it might not work:
working directory issue (if you're using relative paths)
a long running script (being a scraping script it might take a while to complete) - you can check if it's still running using ps aux | grep python
the script does not output anything (would need some more details about the script)
Just to be sure you catch any errors from the script, redirect stderr to stdout by using 2>&1

Raspberry pi crontab and interrupts in python

I wrote a python script that logs some data to a txt file with the absolute path /home/pi/foo.txt through button presses that trigger interrupts. I've started the script many times through the command line without any problems: when you press go, it goes, and when you press stop, it stops. The script is located in /home/pi/log.py. I wrote a shell script that will execute this python script because I read that it may help on a tutorial, so let's call it log.sh which calls
#! /bin/sh
cd /home/pi
/usr/bin/python /home/pi/log.py
However, when I attempt to start this script through crontab by adding it to my
#reboot log.sh
the script will run, but no button presses will stop the script (aka stop button won't work). The cpu usage goes up to 100% and sticks there. I've tried copying and pasting the environment variables from my user environment into crontab, but that won't work either.
Any ideas?
I learned the hard way that the environment that crontab uses isn't the same as your user's environment.
Check out this stack over flow for more info about environment difference between user and crontab: https://serverfault.com/questions/698577/why-is-the-cron-env-different-from-the-users-env/698635
The best thing in my opinion to do is to just do everything in python. There's a python environment that behaves like crontab. It's called schedule(https://pypi.python.org/pypi/schedule).
I'm actually using it in a raspberry Pi thermostat project.
check it out here:https://github.com/mababio/raspi_thermostat/blob/c6aea6ded6874d0dc21ded34f07874dd7f97dd15/src/thermo/test/jobs.py

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

Raspberry Pi Auto Starting a program

So I have a python game (PyGame) running on a raspberry Pi.
I have followed the instructions found on many sites for getting the Raspberry Pi to auto login (those all work), auto run startx, but I'm stuck on getting my program to run once the GUI loads.
Many people (here on StackOverflow and other places) point to this presentation here:
http://www.slideshare.net/SeggySegaran/raspberry-pi-autostarting-a-python-program
I've tried both ways of doing it (putting the desktop file in autostart or putting the command in rc.local
I have opened up a Terminal window and copy / pasted the command to verify there are no typos and the code will run......
sudo python /home/pi/valley.py
and it will run. Is there a way to see a log to find out WHY the program doesn't launch? Is there a better way to get done what I want to get done?
I've got my python script to run at startup doing this:
sudo nano /etc/xdg/lxsession/LXDE/autostart
This will allow you to add an element to run when the LXDE desktop session begins (the raspian default GUI if setup to do from raspi-config)
It will probably have entries like these:
#lxpanel --profile LXDE
#pcmanfm --desktop --profile LXDE
#xscreensaver -no-splash
It's just a matter of adding your script there as well
#lxpanel --profile LXDE
#pcmanfm --desktop --profile LXDE
#xscreensaver -no-splash
#python /home/pi/yourAwesomePyScriptHere.py
If your python script uses GPIO, you need to run that as root (using sudo):
#sudo python /home/pi/yourGPIOScript.py
One thing I do want to point out: always test your script before hand.
I mean, run with the absolute path, make sure it still works, try to break it, make sure it's as robust as it can be. If there are errors in your script and you place it at start up you won't see those in a terminal window, but you will hog the cpu with python stuck in a loop at startup.
Also check out this answer on the RPi exchange
You can achieve this in two ways:
1). Using LXDE autostart.
2). As a service via init.d.
If you are starting X with "startx", you can also just stick your game in your .xinitrc. If your game binary is called "game" and is in your path, just do this:
echo "game" >> ~/.xinitrc
This works for other commands. Add a "&" if you want the command to keep running in the background.
This is how I start my window manager, load my wallpaper, start a compositor, etc. It is stupid simple, easy to change later, and can do anything you can do at a terminal prompt.
You can run your script automatically on startup of raspberry by using crontab.
Crontab is table that list all command to perform on scheduled time.
First, you need to edit crontab by using:
sudo crontab -e
And after this, add following line:
#reboot python path-of-your-script & (& should be at the end of line that means command will execute in background).
Save your script and reboot your system. When your system will start, your script will run automatically.

Categories