4

I've searched through some answers but nothing seems to clarify my confusion.

I have a cron job I want to run every 5 minutes:

*/5 * * * * cd /mnt/internal-storage/coindata && shell/command coins update

Do I place this in the /etc/cron.daily folder or create a /etc/cron.minutely?

Also what kind of file I create inside this folder?

  • 3
    Run crontab -e and put it there? – muru Oct 12 '17 at 4:24
  • Whenever I run that I get a terminal with a bunch of tildes on separate lines and a "/tmp/crontab.6SjpJh" 0 lines, 0 characters at the bottom of terminal. All the tutorials I've read state that it's supposed to open an editor but it doesn't. – Derek Oct 12 '17 at 4:31
  • 2
    That sounds like an editor to me. Probably vi. – muru Oct 12 '17 at 4:31
  • 2
    Put the file in /etc/cron.d directory. See man page crontab(5). Line format is: minute hour dom month user command.... You might have an example in the directory. – hschou Oct 12 '17 at 5:16
4

The best solution for this is likely to add a line to your crontab. Accessing the crontab file may differ between implementations of cron, so I've provided commands for the two cron implementations in the official Arch repos. If you want a solution that does not require a specific cron implementation, I've written another answer that uses systemd/Timers instead.

crontab -e or variants of it use the EDITOR environment variable (defaults to vi). If you wish to use a different editor, export it to the EDITOR variable like so:

export EDITOR=vim

where vim is replaced with the editor of your choice.


Editing crontab with cronie:

crontab -e

Editing crontab with fcron:

fcrontab -e

Add your cron command to the file and save it:

*/5 * * * * cd /mnt/internal-storage/coindata && shell/command coins update

The format for lines in this file is

minute hour day_of_month month day_of_week command

If cron is not running, start its daemon.

For cronie: systemctl start cronie.service

For fcron: systemctl start fcron.service

If you want the commands in the crontab to continue to run after rebooting, make sure that the cron daemon is enabled:

systemctl enable cronie.service or systemctl enable fcron.service

3

Arch Linux's built-in timing system is systemd/Timers. The Arch Wiki lists several pros and cons for using it as a cron replacement. I would add another pro: no cron implementation needs to be installed.

Overall, I'd recommend using a cronjob for simplicity's sake, but this is a solution that should (in theory) work on any Arch installs; the cron implementation irrelevant.

An implementation in Timers (replace TIMERNAME with a name for your timer):

Timer file location: /etc/systemd/system/TIMERNAME.timer


[Unit]
Description=Run TIMERNAME every five minutes

[Timer]
OnCalendar=*:0/15
Persistent=true     

[Install]
WantedBy=timers.target

Timer service file location: /etc/systemd/system/TIMERNAME.service


[Unit]
Description=Run some commands - for use with TIMERNAME.service

[Service]
ExecStart=cd /mnt/internal-storage/coindata && shell/command coins update
# Alternatively could be used to run a script with the above commands in it.
# If your script is located at /usr/local/bin, change the above command to:
#    ExecStart:/usr/local/bin/SCRIPTNAME

[Install]
WantedBy=multi-user.target

To start, run:

systemctl start TIMERNAME.timer

To enable (so that it will continue working after future reboots):

systemctl enable TIMERNAME.timer

Stopping and disabling follow the same format.

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.