-1

I am a newbie to raspberry pi's so please forgive my lack of knowledge.

A few years ago I was given a raspberry pi 3, I had it set up with a crontab file which ran some commands for me which have been running as they were set to do since then. I now find I need to add some more commands to thr file but I cannot find where it was saved to or if I named it something different to just 'crontab'?

When I run :-

pi@raspberrypi:~ $ sudo crontab -l

I get a blank crontab file

Also when I run :-

pi@raspberrypi:~ $ sudo crontab -e

I also get a blank crontab?

How do I find where the file is for my user (pi)?

================================================================

Thanks for the answers, I followed answer '0' and got:-

pi@raspberrypi:~ $ cat /etc/passwd | grep -v nologin
root:x:0:0:root:/root:/bin/bash
sync:x:4:65534:sync:/bin:/bin/sync
pi:x:1000:1000:,,,:/home/pi:/bin/bash
lightdm:x:109:114:Light Display Manager:/var/lib/lightdm:/bin/false
pi@raspberrypi:~ $ 

Now I know there is a cron job somewhere on my system as it runs executables which move files to my HDD otherwise my pi's memory would fill.

This is an old version that was done, but has been changed as the directories in it are now differently named:-

#
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command    
#### first jpg files #####
5 1 * * * find /CCTV/RearOfHouse/ -type f -mtime +7 -name '*.jpg' -execdir mv  '{}' /CCTV/RearOfHouse/
5 2 * * * find /CCTV/FrontOfHouse/ -type f -mtime +7 -name '*.jpg' -execdir mv  '{}' /CCTV/FrontOfHouse/
1 3* * * find /CCTV/FrontOfHouse/ -type f -mtime +70 -name '*.jpg' -execdir rm -- '{}'$
1 4 * * * find /CCTV/RearOfHouse/ -type f -mtime +70 -name '*.jpg' -execdir rm -- '{}'$
##1 5 * * * find /CCTV/ -type f -mtime +60 -name '*.jpg' -execdir rm -- '{}' \;
0 1 * * * /home/pi/CCTV/RearOfHouse/CreatejpgDirs.sh
10 1 * * * /home/pi/CCTV/FrontOfHouse/CreatejpgDirs.sh
#### now mp4 files  #####
25 1 * * * find /CCTV/RearOfHouse/ -type f -mtime +7 -name '*.mp4' -execdir mv  '{}' /CCTV/RearOfHouse/
25 2 * * * find /CCTV/FrontOfHouse/ -type f -mtime +7 -name '*.mp4' -execdir mv  '{}' /CCTV/FrontOfHouse/
21 3 * * * find /CCTV/FrontOfHouse/ -type f -mtime +70 -name '*.mp4' -execdir rm -- '{$
21 4 * * * find /CCTV/RearOfHouse/ -type f -mtime +70 -name '*.mp4' -execdir rm -- '{}$
##21 5 * * * find /CCTV/ -type f -mtime +120 -name '*.mp4' -execdir rm -- '{}' \;
##21 6 * * * find /CCTV/ -type f -mtime +120 -name '*.mp4' -execdir rm -- '{}' \;
0 1 * * * /home/pi/CCTV/RearOfHouse/Createmp4Dirs.sh
10 1 * * * /home/pi/CCTV/FrontOfHouse/Createmp4Dirs.sh

so I don't know if the filename (for the cron job) has also been changed? So any clues as how to find it? Thanks WD

nb. here is my current structure:

pi@raspberrypi:~ $ ls
Desktop    Downloads  fdisk.save  home                 jpgDirsRemovedR.txt   jpgFilesRemovedR.txt  mnt                  mp4DirsRemovedR.txt   mp4FilesRemovedR.txt  Pictures  symlinks_all.txt  Videos
Documents  ExecFiles  fluxion     jpgDirsRemovedF.txt  jpgFilesRemovedF.txt  MagPi                 mp4DirsRemovedF.txt  mp4FilesRemovedF.txt  Music                 Public    Templates
pi@raspberrypi:~ $ cd ExecFiles

(and this is the executables run by the cron file)

pi@raspberrypi:~/ExecFiles $ ls
CreatejpgDirsF.sh  CreatejpgDirsF_WithDays.sh  CreatejpgDirsR.sh  CreatejpgDirsR_WithDays.sh  Createmp4DirsF.sh  Createmp4DirsF_WithDays.sh  Createmp4DirsR.sh  Createmp4DirsR_WithDays.sh
pi@raspberrypi:~/ExecFiles $
  • And if you googled: unix.stackexchange.com/questions/196009/… – Swedgin Jun 4 '20 at 12:12
  • So, you did not find them using Seamus' answer. Did you ls /var/spool/cron/crontabs? – Ljm Dullaart Jun 6 '20 at 11:31
  • Yes, and all that came up was 'pi' and if I then run 'sudo nano pi' all I get is a blank file? – DoubleUdee Jun 7 '20 at 13:43
  • Is there a way of recursively running through all the files under my user 'pi' to look for a certain string within them and the output the names of the files that the string exists in? – DoubleUdee Jun 7 '20 at 13:46
  • grep -r "string" /home/pi for all files under the user pi. But: 1) avoid asking further questions in the comments and 2) this probably won't help you here. – Ljm Dullaart Jul 3 '20 at 10:06
5

The crontabs are under /var/spool/cron/crontabs. The files there are supposed to be used by the crontab command and are not to be edited outside that.

How to find it? There are a number of options (besides google). The most elaborate way is probably

 sudo find / -name 'crontab*'

Another way is to look at the man page for crontab (man crontab); under "files" it is explained where the crontabs are.

However, if the crontabs are empty with crontab -l, they really are empty. You will see that when you cat those crontabs.

EDIT: As per comment, I must add that crontab -l lists the crontab for the user that executes the command. So, if you are logged-in as the user pi, and you do crontab -l, you get the crontab for the user pi. if you do sudo crontab -l, sudo makes crontab execute as root, and therefore you get the crontab for root.

  • 4
    if you do sudo crontab -l you see root's crontabs, because you used sudo; if you just do crontab -l you see the current user's crontabs. – Michael Harvey Jun 4 '20 at 19:12
1

If you setup cron for the pi user you edit it with

crontab -e

sudo crontab -e will edit root's crontab.

Don't worry about where it is actually "stored" - Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly.

  • the OP might want to get them back from a full system backup. – Ljm Dullaart Jun 4 '20 at 15:21
  • 1
    This does not answer the question. – Ingo Jun 4 '20 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.