8

I'm trying to put project specific crontab config into code repository to make it easy to update cron jobs on all backends when deploying.

Is it possible to include files into crontab config? Something like

* * * * * execsomething /blablabla/blah/
* * * * * onemore /blablabla
@include '/home/user/project/crontab.conf'
8

Depending on your cron daemon and your distribution, there should be several directories in /etc where you can put files with jobs for cron.

Scripts (not cron-formatted jobs) placed in the following directories will be executed at the specified intervals. Note that frequency is guaranteed but the exact time it runs may not be. For most users these directories are sufficient:

/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

cron-formatted jobs (like your examples above) placed in the following directory will be executed by cron at the specified times. Note that in addition to the normal entries the user of the job needs to be specified as well, before the command. For packages and software distributions this method is the best way to distribute cronjobs:

/etc/cron.d

Example of a cron.d formatted command:

1 1 * * * root echo "serverfault is awesome" > /dev/null
  • Can the username be a variable? I have a bunch of commands that need to run as the same user, but might be a different user in different environments. Can I do something like $USER=joeblow 1 1 * * * $USER echo "serverfault is awesome" > /dev/null – Jay K May 10 '16 at 15:10
  • Depends on the cron daemon. cronie does not allow environment variables for example. – Boscoe May 16 '16 at 17:33
4

Not as far as I know; that's what /etc/cron.d is for.

As Boscoe has pointed out, there's also the /etc/cron.{hourly,daily,weekly,monthly} directories, but jobs in there can only run as root (unless you explicitly use /bin/su to change the EUID). Jobs in /etc/cron.d have an extra field that specifies the user they run as, and the way your question contains a /home/user path makes me think you want these jobs to be able to run as arbitrary users.

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.