Dietpi: about crontab

Created on 10 Dec 2019  Â·  7Comments  Â·  Source: MichaIng/DietPi

I want to run a script like: */10 * * * * * bash /var/abc.sh

Which file should I add? /etc/crontab ? or crontab -e

It is also not lost when dietpi is upgraded

Please help me, thank you

Question

All 7 comments

@hifitime

you could try to copy your script to /etc/cron.minutely/ . Afterwards run dietpi-cron to enable and configure this feature. It allows to run scripts every chosen amount of minutes.

Jep, little addition:

  • Files in /etc/cron.*/ are not allowed to have file endings, hence rename abc.sh to abc as well: mv /var/abc.sh /etc/cron.minutely/abc
  • Take care it has the shebang #!/bin/bash (or whichever shell works), as those files are not explicitly executed with a specific shell.
  • Take care it is executable: chmod +x /etc/cron.minutely/abc
  • Check if it would be executed: run-parts --test /etc/cron.minutely/

Just for reference, as the bid strict rules of run-parts, used to execute the files in those dirs, I link the man page: https://manpages.debian.org/run-parts


Generally /etc/crontab works as well, but since it is a core config file that is part of APT packages and overwritten by dietpi-cron script, I would always add custom entries via crontab -e or crontab -u <username> -e, which are stored per-user at a different location, safe from being overwritten.

I want to run my script every 10 seconds. What should I do? Is there any good solution?
thank you

@hifitime
Cron only allows to run jobs on minutely basis, hence every 10 seconds is not natively possible. In this case I suggest you create a systemd unit that starts your script and do the 10 second loop within the script itself:

while :
do
   <your_code>
   sleep 10
done

Create and enable + start the systemd unit:

cat << _EOF_ > /etc/systemd/system/<your_service_name>.service
[Unit]
Description=<your_service_name>

[Service]
ExecStart=/path/to/your/script

[Install]
WantedBy=multi-user.target
_EOF_
systemctl daemon-reload
systemctl enable --now <your_service_name>

If it needs to start after certain other services, e.g. requires network or similar, you can add an After=<name>.service to the [Unit] section. After=dietpi-postboot.service should assure that it starts quite late. You can also have it controlled by dietpi-services then to e.g. have it stopped automatically when doing software installs, updates and other tasks throughout our scripts.

Thank you. Finally, which directory should I put my own files in? Like a script and so on... Any suggestions?
I want to be more standard and clear

Basically you can choose freely. E.g. use /mnt/dietpi_userdata/<name>.sh to have it together with other DietPi userdata and settings. Or /opt/<name>.sh for "optional" programs or within your users home dir, although as such systemd unit it runs for all users/system-wide.

Or: https://serverfault.com/questions/369723/what-is-the-appropriate-location-for-a-custom-script#369735
/usr/local/(s)bin/<name> so you can call it manually from console as well since this is inside the default $PATH, sbin or bin depending on if it requires root permissions to run or not.

Thank you very much for your patient answer, which has helped me a lot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kapot picture Kapot  Â·  3Comments

1021683053 picture 1021683053  Â·  3Comments

k-plan picture k-plan  Â·  3Comments

Fourdee picture Fourdee  Â·  3Comments

Fourdee picture Fourdee  Â·  3Comments