daztop
Posts: 6
Joined: Fri May 31, 2019 12:14 pm

crontab always empty, but jobs are running

Fri May 31, 2019 12:21 pm

A while ago, I added a job to crontab to run a task every hour. I did this using crontab -e

A few weeks later, I decided I wanted to run it every 15 minutes so I ran crontab -e again - but the job wasn't there. So I added it with the new frequency and now it runs every 15 minutes AND every hour.

crontab -e always reports a new file. crontab -l lists "no crontab for pi".

I've tried running it as "sudo crontab -l" and it reports no crontab for root. I found a script that runs through every user on the device and lists crontab and they all say they have nothing - and yet the jobs run.

So I can easily add new jobs, but can't edit existing ones.

Anyone any idea what's going on? Where are my jobs being written to?

dl324
Posts: 122
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: crontab always empty, but jobs are running

Fri May 31, 2019 1:00 pm

daztop wrote:
Fri May 31, 2019 12:21 pm
A few weeks later, I decided I wanted to run it every 15 minutes so I ran crontab -e again - but the job wasn't there. So I added it with the new frequency and now it runs every 15 minutes AND every hour.
Are you saying that the job runs 5 times per hour?

Post the crontab entry for the job in question.

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

Re: crontab always empty, but jobs are running

Fri May 31, 2019 1:32 pm

Are you sure you didn't put an entry in the system-wide crontabs (/etc/crontab and all files in /etc/crontab.d)?

All the user crontabs are kept in /var/spool/cron/crontabs with the filenames being the user's names, though you have to be root to be able to look in that directory. Also never modify these user files directly, always use crontab to make changes.

The following command shows that pi is the only user who has their own crontab, and that file /var/spool/cron/crontabs/pi is the one that crontab -e edits.

Code: Select all

pi@rpi3:~ $ sudo ls /var/spool/cron/crontabs
pi
She who travels light — forgot something.

daztop
Posts: 6
Joined: Fri May 31, 2019 12:14 pm

Re: crontab always empty, but jobs are running

Fri May 31, 2019 4:15 pm

Yes, the job now runs 5 times an hour.

/var/spool/cron/crontabs is empty:

Code: Select all

pi@screenpi:~$ sudo ls -l /var/spool/cron/crontabs/
total 0
/etc/crontab only contains the default tasks:

Code: Select all

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
When I look in those folders, I have a bunch of unrelated tasks:

Code: Select all

cron.d:
total 0

cron.daily:
total 28
-rwxr-xr-x 1 root root 1474 Sep 13  2017 apt-compat
-rwxr-xr-x 1 root root  314 Apr 18  2017 aptitude
-rwxr-xr-x 1 root root  355 Oct 25  2016 bsdmainutils
-rwxr-xr-x 1 root root 1597 Jun 26  2018 dpkg
-rwxr-xr-x 1 root root   89 May  5  2015 logrotate
-rwxr-xr-x 1 root root 1065 Dec 13  2016 man-db
-rwxr-xr-x 1 root root  249 May 17  2017 passwd

cron.hourly:
total 8
-rwxr-xr-x 1 root root 191 Feb 22  2012 fake-hwclock
-rwxr-xr-x 1 root root  44 Apr  9 15:13 log2ram

cron.monthly:
total 0

cron.weekly:
total 4
-rwxr-xr-x 1 root root 723 Dec 13  2016 man-db

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

Re: crontab always empty, but jobs are running

Fri May 31, 2019 7:33 pm

That is definitely showing no crontabs other than the base system ones, not sure how cron is managing to run jobs when there are no crontabs defining them. When cron sees a crontab has changed since it last read it, it forget the job list it had cached for that crontab and if the crontab still exists it creates a new list of jobs accordingly. It sounds like cron is failing to drop the old jobs, not sure how that is happening.

You said that crontab -e always creates new files, that is strange. For the jobs to be being run the file must have been created for cron to read it, and nobody other than root can modify or remove the files (crontab runs as root:crontab) so running crontab -e a second time should give you the contents saved by the first (the file it gives you to edit is actually a temporary copy of your crontab, when you save and quit the editor it validates this file and if everything looks good it writes it to the actual crontab file).

Note that if you accidentally run crontab without any parameters then press CTRL-C to abort, running it like that will take anything typed afterwards as being the new potential crontab. Importantly don't just press CTRL-D as that will install an empty crontab overwriting whatever you had.

The only thing I can think of that could possibly be happening is if your SD card has died and made itself read-only, under this condition alterations to files can be kept in the file cache so it looks like they take effect but they never get written to the card, the next time you boot up all the changes will be gone. Quick check for this is to create a file in your home directory and reboot, if the file is gone then the SD card has died.
She who travels light — forgot something.

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

Re: crontab always empty, but jobs are running

Fri May 31, 2019 7:45 pm

Does this behaviour persist across reboots?

Is it a standard Raspbian install?

Have you go any external storage devices mounted, or a non-standard file system setup of any sort?

daztop
Posts: 6
Joined: Fri May 31, 2019 12:14 pm

Re: crontab always empty, but jobs are running

Fri May 31, 2019 8:08 pm

I reboot it daily. It's a standard Raspbian install on an SD card, with no other storage.

It's been like this for a few weeks ... I've just only got round to raising it as I wanted to add another job.

daztop
Posts: 6
Joined: Fri May 31, 2019 12:14 pm

Re: crontab always empty, but jobs are running

Fri May 31, 2019 8:09 pm

Oh - and SD card is still saving files ok.

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

Re: crontab always empty, but jobs are running

Fri May 31, 2019 8:12 pm

Very weird. I can't think of anything else that could cause that at the moment, but will respond if I do.

Silverlock
Posts: 87
Joined: Wed Apr 10, 2013 3:51 am

Re: crontab always empty, but jobs are running

Fri May 31, 2019 9:04 pm

Here's a potentially dumb question. Have you looked in /var/log/syslog to see if there's any logging info that might shed light on what's going on?
RPi3+external HDD/Raspbian Stretch

daztop
Posts: 6
Joined: Fri May 31, 2019 12:14 pm

Re: crontab always empty, but jobs are running

Fri May 31, 2019 9:44 pm

syslog is only logging CRON events for the tasks I mentioned above that are in /etc/crontab. Not the tasks I've added myself.

Silverlock
Posts: 87
Joined: Wed Apr 10, 2013 3:51 am

Re: crontab always empty, but jobs are running

Fri May 31, 2019 10:50 pm

If there's nothing in the syslog file, I would have to wonder if perhaps cron isn't the culprit.
RPi3+external HDD/Raspbian Stretch

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

Re: crontab always empty, but jobs are running

Sat Jun 01, 2019 12:46 am

By default cron is set up to log the start of all jobs, if your program is running but there is no mention in the logs of cron starting it then cron didn't start it.

Did you create a systemd service for it and a systemd timer to run the service periodically?
She who travels light — forgot something.

RDS
Posts: 739
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: crontab always empty, but jobs are running

Sat Jun 01, 2019 3:31 pm

I have had a similar experience previously and I was looking in the incorrect Directory for the cron file.

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

Re: crontab always empty, but jobs are running

Sat Jun 01, 2019 3:40 pm

RDS wrote:
Sat Jun 01, 2019 3:31 pm
I have had a similar experience previously and I was looking in the incorrect Directory for the cron file.
Normal user crontabs should never be accessed directly, the program crontab will edit the correct file (their location is distro specific and depends on which cron is used). Only the system-wide crontabs /etc/crontab and those in /etc/cron.* should be manually edited.
She who travels light — forgot something.

Return to “Beginners”