Jsakkos
Posts: 29
Joined: Thu Dec 15, 2016 4:49 am

Cron not working?

Sat Feb 18, 2017 4:20 am

I have a script that runs at reboot on my Pi Zero, but when I updated the script today, the Crontab file seemed to get reset? When I looked, there were no additions that I had previously made. Even after adding back the entries, while pigpiod runs, my script does not. I can manually run the script and there doesn't seem to be anything wrong. Am I missing something? The lines I added to Crontab are as follows:

Code: Select all

@reboot sudo pidpiod&
@reboot sudo python3 /home/pi/git/SunriseSimulator/app.py &

pcmanbob
Posts: 7305
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Cron not working?

Sat Feb 18, 2017 10:32 am

Jsakkos wrote:

Code: Select all

@reboot sudo pidpiod&
@reboot sudo python3 /home/pi/git/SunriseSimulator/app.py &
you are missing a space between the end of the first line and &
also you should just run cron as root and not use sudo in your cron lines.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
rpdom
Posts: 15606
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron not working?

Sat Feb 18, 2017 11:45 am

pcmanbob wrote:you are missing a space between the end of the first line and &
A space is not required, but you don't to need to put "&" on the end of lines in crontabs at all. It serves no purpose.
pcmanbob wrote:you should just run cron as root and not use sudo in your cron lines
I totally agree. But remember to use the full paths to files.

Jsakkos
Posts: 29
Joined: Thu Dec 15, 2016 4:49 am

Re: Cron not working?

Sat Feb 18, 2017 4:57 pm

Ok so instead of crontab -e I should have been adding sudo in front?

Cron now looks like:

Code: Select all

@reboot              /usr/local/bin/pigpiod
@reboot python3 /home/pi/git/SunriseSimulator/app.py
Pigpiod is running, but app.py is still not running on reboot. What could I have inadvertently done to cause this?

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Cron not working?

Sat Feb 18, 2017 6:09 pm

Jsakkos wrote:Ok so instead of crontab -e I should have been adding sudo in front?
Cron now looks like:

Code: Select all

@reboot              /usr/local/bin/pigpiod
@reboot python3 /home/pi/git/SunriseSimulator/app.py
Pigpiod is running, but app.py is still not running on reboot. What could I have inadvertently done to cause this?
Have you show us app.py? Does it have a valid shebang at the top? Is it executable? How are you running it when you test it, like this?:

python app.py

Does it run just with?:

./app.py

You need to debug the whole effort by cutting it into pieces and making sure things work from the start all the way through one bit at a time. So if the above doesn't solve it, can you run a simple bash program from the crontab that just saves "Hi I'm here!" to your home directory? If that works, get your python program to run by adding code at the top that does the same things as the bash program and then exits. Does that work?

Jsakkos
Posts: 29
Joined: Thu Dec 15, 2016 4:49 am

Re: Cron not working?

Sat Feb 18, 2017 7:04 pm

stderr wrote: Have you show us app.py? Does it have a valid shebang at the top? Is it executable? How are you running it when you test it, like this?:
The file is here: https://github.com/Jsakkos/SunriseSimul ... ter/app.py
I guess my confusion here stems from the fact that it was working properly before I updated to the latest version of the code. When I test it, I go to the directory with app.py and use

Code: Select all

sudo python3 app.py
stderr wrote:Does it have a valid shebang at the top?
I'm not sure what you mean by this. What should I have at the top?
stderr wrote:Is it executable?
How can I check this?
stderr wrote: You need to debug the whole effort by cutting it into pieces and making sure things work from the start all the way through one bit at a time. So if the above doesn't solve it, can you run a simple bash program from the crontab that just saves "Hi I'm here!" to your home directory? If that works, get your python program to run by adding code at the top that does the same things as the bash program and then exits. Does that work?
I started by checking that the program still runs from the terminal, which worked normally. I then went to crontab and discovered that the two lines which run pigpiod and app.py were no longer there. After adding them back and restarting, I found that pigpiod was running but app.py was not. Forgive my ignorance, I'm still pretty new to Python and RPi, so I don't fully understand how crontab works, aside from the obvious.

User avatar
rpdom
Posts: 15606
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron not working?

Sat Feb 18, 2017 7:45 pm

Jsakkos wrote:
stderr wrote:Does it have a valid shebang at the top?
I'm not sure what you mean by this. What should I have at the top?
It doesn't have one (a line that looks like "#!/usr/bin/python3" which tells the command shell to run this code using python 3 in this case), but it doesn't need one as you are explicitly running it with python3.
stderr wrote:Is it executable?
How can I check this?
You can use the ls (LS) command with the -l (-L) option to show the permission flags, including the eXecute permissions, but again because you are specifying that python3 must run the program it doesn't matter.
I started by checking that the program still runs from the terminal, which worked normally. I then went to crontab and discovered that the two lines which run pigpiod and app.py were no longer there. After adding them back and restarting, I found that pigpiod was running but app.py was not. Forgive my ignorance, I'm still pretty new to Python and RPi, so I don't fully understand how crontab works, aside from the obvious.
One thing that is important to know is that "sudo crontab -e" and "crontab -e" edit different crontabs.

If you use "sudo crontab -e" you are editing the crontab owned by "root" and all commands in the crontab will run as that user.

When you use "crontab -e" without sudo, you are editing your own crontab and the commands will run as your own user.

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: Cron not working?

Sat Feb 18, 2017 9:17 pm

You need to have the full pathname to python3
so the command have to be:

Code: Select all

@reboot /usr/bin/python3 /home/pi/git/SunriseSimulator/app.py

Jsakkos
Posts: 29
Joined: Thu Dec 15, 2016 4:49 am

Re: Cron not working?

Sat Feb 18, 2017 9:20 pm

rpdom wrote:It doesn't have one (a line that looks like "#!/usr/bin/python3" which tells the command shell to run this code using python 3 in this case), but it doesn't need one as you are explicitly running it with python3.
Thanks for clarifying.
rpdom wrote:You can use the ls (LS) command with the -l (-L) option to show the permission flags
When I do this, I get

Code: Select all

-rw-r--r-- 1 pi pi 8081 Feb 17 12:04 app.py
rpdom wrote: One thing that is important to know is that "sudo crontab -e" and "crontab -e" edit different crontabs.
This is really good to know, thanks. I had no idea. I did have the proper commands when I look at sudo crontab -e.

User avatar
rpdom
Posts: 15606
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cron not working?

Sat Feb 18, 2017 10:25 pm

Jsakkos wrote:
rpdom wrote:You can use the ls (LS) command with the -l (-L) option to show the permission flags
When I do this, I get

Code: Select all

-rw-r--r-- 1 pi pi 8081 Feb 17 12:04 app.py
Ok, the first character of the "-rw-r--r--" bit has special meanings, but is unimportant here.

The remaining three sets of three characters relate to the file permissions for the Owner of the file, members of the Group of the file, and everyone else.
The first "rw-" means "Read and Write access for the Owner 'pi'".
The next "r--" means "Read access for members of the Group 'pi'".
The last "r--" means "Read access to everyone else".

The third character can be set to "x", as in "rwx", which means "Read, Write and eXecutable access"

The eXecutable flag (usually) only matters when you try try running the file by just typing its name, not when you tell something like Python to run it.
Last edited by rpdom on Sun Feb 19, 2017 9:37 am, edited 2 times in total.

Jsakkos
Posts: 29
Joined: Thu Dec 15, 2016 4:49 am

Re: Cron not working?

Sun Feb 19, 2017 2:52 am

Canedje wrote:You need to have the full pathname to python3
so the command have to be:

Code: Select all

@reboot /usr/bin/python3 /home/pi/git/SunriseSimulator/app.py
I tried this, but it still doesn't seem to be running.

Jsakkos
Posts: 29
Joined: Thu Dec 15, 2016 4:49 am

Re: Cron not working?

Sun Feb 19, 2017 2:54 am

rpdom wrote: The first "rw-" means "Read and Write access for the Owner 'pi'".
The next "r--" means "Read access for members of the Group 'pi'".
The last "r--" means "Read access to everyone else".
Ok, so it's not executable, but since I'm explicitly telling it to run with python, that doesn't matter. Thanks.

User avatar
Paeryn
Posts: 2749
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Cron not working?

Sun Feb 19, 2017 4:48 am

Is the problem that your python program isn't running properly or is it that pigpiod hasn't finished setting up by the time your program tries using it (since your program looks like it quits if it hasn't)? Try writing a log file in your program so you can see if it is actually running, and especially log if it quits due to pigpio not being available.

Does your program rely on being run from within a particular directory (which more than likely won't be the one that cron runs it from)?
She who travels light — forgot something.

Jsakkos
Posts: 29
Joined: Thu Dec 15, 2016 4:49 am

Re: Cron not working?

Sun Feb 19, 2017 6:34 am

Paeryn wrote:Is the problem that your python program isn't running properly or is it that pigpiod hasn't finished setting up by the time your program tries using it (since your program looks like it quits if it hasn't)? Try writing a log file in your program so you can see if it is actually running, and especially log if it quits due to pigpio not being available.

Does your program rely on being run from within a particular directory (which more than likely won't be the one that cron runs it from)?
I think the last part might be the key here. When I tried running the code from another directory, I got the following error:

Code: Select all

pi@raspberrypi:/ $ python3 /home/pi/git/SunriseSimulator/app.py
Traceback (most recent call last):
  File "/usr/lib/python3.4/configparser.py", line 1116, in _unify_values
    sectiondict = self._sections[section]
KeyError: 'Wakeup Settings'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/git/SunriseSimulator/app.py", line 247, in <module>
    LED = LED_Communicator()
  File "/home/pi/git/SunriseSimulator/app.py", line 44, in __init__
    self.WakeupHour = int(config.get('Wakeup Settings', 'Hour'))
  File "/usr/lib/python3.4/configparser.py", line 754, in get
    d = self._unify_values(section, vars)
  File "/usr/lib/python3.4/configparser.py", line 1119, in _unify_values
    raise NoSectionError(section)
configparser.NoSectionError: No section: 'Wakeup Settings'
This makes it look like the configuration file is the issue. What is the proper way to specify a directory for config.ini that is the same as app.py?

Code: Select all

config = ConfigParser()
        config.read('config.ini')
        self.WakeupHour = int(config.get('Wakeup Settings', 'Hour'))

Jsakkos
Posts: 29
Joined: Thu Dec 15, 2016 4:49 am

Re: Cron not working?

Sun Feb 19, 2017 7:15 pm

The solution to this problem was making sure that the config.ini file reference had the full path, since the program was not being run from that directory.

Code: Select all

ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
filepath = os.path.join(ROOT_PATH, "config.ini")
config = ConfigParser()
config.read(filepath)

User avatar
TracyLBaker
Posts: 39
Joined: Sat Feb 18, 2017 3:44 pm
Location: Phoenix, AZ

Re: Cron not working?

Sun Feb 19, 2017 7:31 pm

Is it being placed in /etc/crontab, which is the system's cron job file? This is what is needed for @reboot to work, since a user isn't logged on yet.

This file is edited with nano or vim -- not the crontab command. The first line in the file says:
# /etc/crontab: system-wide crontab
crontab -e is per user, and is not used to set up system jobs -- even if executed as root (in which case it'll set up cronjobs for the user root).

If it needs to be executable, you can use something as easy as this:

chmod +x {name_of_your_file}

However, this will make it executable for users, groups and others. If you want just the user (owner), you can use chmod u+x {name_of_your_file}

Also, as others have said, when dealing with cronjobs; it is always a good idea to specify full paths, even within your scripts.
Red Hat Academy Professor - Estrella Mountain Community College
5 Pi 3 Model B+s: 1 headless media/NFS/SMB server - Raspbian Jessie, 1 Kali Linux w/fruitywifi, 1 headless CentOS 7 webserver (Apache, SELinux, firewalld, autofs, etc...), 2 in waiting

User avatar
Paeryn
Posts: 2749
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Cron not working?

Sun Feb 19, 2017 11:06 pm

TracyLBaker wrote:Is it being placed in /etc/crontab, which is the system's cron job file? This is what is needed for @reboot to work, since a user isn't logged on yet.
Wrong! cron runs every user's crontab regardless of whether they are logged in or not. Every user can have @reboot lines in their crontab and they will be run on reboot.
She who travels light — forgot something.

User avatar
TracyLBaker
Posts: 39
Joined: Sat Feb 18, 2017 3:44 pm
Location: Phoenix, AZ

Re: Cron not working?

Mon Feb 20, 2017 2:50 am

Paeryn wrote:
TracyLBaker wrote:Is it being placed in /etc/crontab, which is the system's cron job file? This is what is needed for @reboot to work, since a user isn't logged on yet.
Wrong! cron runs every user's crontab regardless of whether they are logged in or not. Every user can have @reboot lines in their crontab and they will be run on reboot.
Be that is it may, my @reboots didn't work on mine until I placed them into /etc/crontab -- which is the intended location for system jobs (which is what mine are). As a result, it is worth pointing out to the OP that this is worth a try.
Red Hat Academy Professor - Estrella Mountain Community College
5 Pi 3 Model B+s: 1 headless media/NFS/SMB server - Raspbian Jessie, 1 Kali Linux w/fruitywifi, 1 headless CentOS 7 webserver (Apache, SELinux, firewalld, autofs, etc...), 2 in waiting

pcmanbob
Posts: 7305
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Cron not working?

Mon Feb 20, 2017 3:04 pm

TracyLBaker wrote:
Paeryn wrote:
TracyLBaker wrote:Is it being placed in /etc/crontab, which is the system's cron job file? This is what is needed for @reboot to work, since a user isn't logged on yet.
Wrong! cron runs every user's crontab regardless of whether they are logged in or not. Every user can have @reboot lines in their crontab and they will be run on reboot.
Be that is it may, my @reboots didn't work on mine until I placed them into /etc/crontab -- which is the intended location for system jobs (which is what mine are). As a result, it is worth pointing out to the OP that this is worth a try.
well I have several Pi's running that have @reboot lines in the pi user cron file and they work on every reboot and also have timed execution of cron jobs in the same pi user cron file and they work and Pi is not logged on. both are running Jessie lite and head less, as far as I am aware both root and user cron files are executed at boot. To edit the root cron just use "sudo crontab -e"
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
jojopi
Posts: 3089
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron not working?

Mon Feb 20, 2017 9:03 pm

TracyLBaker wrote:Be that is it may, my @reboots didn't work on mine until I placed them into /etc/crontab
/etc/crontab has a different syntax—there is a username field before the command. In Raspbian there is also a pre-set "PATH=" line that is more complete than the default environment you will get in a new user crontab. Otherwise there is no reason you should not have got @reboot to work using "crontab -e".

And, no, it is not really a good idea to litter your jobs, plus everything they call, with absolute filenames. That is just ugly and error-prone and makes future changes inconvenient. If you need a less minimal PATH, put one in the crontab. If the working directory affects the behaviour of the job, then include a cd/chdir somewhere.

Return to “Troubleshooting”