Cron job


How do I set up a new cron job? Can I just edit crontab in /etc and add whatever I need (I want to add a weekly job to run the script for pihole)? It seems to work slightly different than in wheezy and before I break anything …



You can setup a simple weekly cron job for this. Please see below for a guide and example on how to set this up:

Create and edit your weekly cron job:

nano /etc/cron.weekly/my_cron_job

Enter the following into your new cron job:


#Run Gravity

#Restart dnsmasq
service dnsmasq restart

Give your cron job execute permissions so it can run:

chmod +x /etc/cron.weekly/my_cron_job

Test your cron job:


Yep that works :slight_smile: . Many thanks!

i know its super old but still first entry on google search :rofl:

i did everything like Fourdee said.
the test worked as well. but the cron job is not executed :confused:

did anything change?

many thanks for your report.

pls can you past the following command

ls -l /etc/cron.weekly

just replace cron.weekly with the cron folder you used.

i will try it later today.
just for the learning curve: what is ls -l doing?

ls is going to show the content of a directory

-rwxr-xr-x 1 root root 1478 Mai 28 2019 apt-compat
-rwxr-xr-x 1 root root 1621 Jan 19 19:54 dietpi
-rwxr-xr-x 1 root root 1187 Apr 19 2019 dpkg
-rwxr-xr-x 1 root root 96 Jan 20 20:56 my_job
-rwxr-xr-x 1 root root 249 Sep 27 2017 passwd

Are you able to show the content of your script?

the script is huge.

if i run /etc/cron.daily/my_job

it works without problems, does cron have logs somewhere?

how is it going if you test the execution like this

run-parts --test /etc/cron.daily/

i set up a job under crontab -e and its working

Note that scripts in /etc/cron./ must have alphanumerical (and - _) names, must not have dots (file endings) and must have executable flag. run-parts is quite strict:

can I just create a folder called/etc/cron.yearly. Will the weekly job count from 1…4 and the monthly job from 1…12 and the yearly job like forever?

Sorry for the questions, hope somebody can clarify!

Simply creating the folder will not work, You would need to add it as well to your cronatb, similar to what we do in /etc/crontab

Weekly jobs will be executed every week until you removed that job. They are not going to stop automatically after a period of time. Same for monthly or daily job.

You were right! Thanks, what I mean, how does rsnapshot count here? It is the wrong question in the wrong forum! Sorry! If you know the answer, I will appreciate that anyway, if you tell me ;-)…

BTW : Why is ‘crontab -e’ different than opening with ‘vim /etc/crontab’ . I thought this is literally the same command and one is recommended to use ‘crontab -e’?

Not sure what rsnapshot is, but probably it uses Cron for creating snapshots on a regular basis.

/etc/crontab usually contains the jobs to execute the scripts in /etc/cron.daily etc directories (as root user) while crontab -e allows you to create custom cron jobs (hence one can bypassing the script’s shell process) as user you are executing crontab with, so that it is necessary for jobs your want to run as non-root user, recommended for security reasons whenever you do not require root permissions. Also /etc/cron.d/ can be used for custom root user cron jobs which is hence pretty similar to sudo crontab -e.

1 Like

How does cron jobs in /etc/cron.d work? I have the following:

0/15 20-23  * * * /mnt/dietpi_userdata/pihole_custom/ >>/mnt/dietpi_userdata/pihole_custom/cron.log 2>&1

I can see it has been reloaded, but it’s never executed. Do I need to enable cron.minutely for it to work?

Sep 28 16:18:01 DietPi cron[7612]: (*system*pihole_custom) RELOAD (/etc/cron.d/pihole_custom)

EDIT: Nevermind, it seems 0/15 doesn’t work, but */15 does!

did you add this cron job yourself? Because it’s not part of our current setup? Or are you running an old installation?