--Environment - Debian GNU/Linux 10 (buster) - apt 1.8.2 (amd64)
I want to fetch Git repository like Redmine regularly, I tried to register a task with cron for the first time
--Reference -apt command cheat sheet --Qiita -Notes on using cron on Debian / Ubuntu | This is a computer code -Cron Configuration Guide
#Update the repository list
$ apt update
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
...abridgement...
48 packages can be upgraded. Run 'apt list --upgradable' to see them.
N: Repository 'http://deb.debian.org/debian buster InRelease' changed its 'Version' value from '10.0' to '10.3'
#Search for cron
$ apt list cron
Listing... Done
cron/stable 3.0pl1-134+deb10u1 amd64
#Install cron
$ apt install -y cron
Reading package lists... Done
Building dependency tree
...abridgement...
Processing triggers for mime-support (3.62) ...
#Check the directory for cron
$ ls -la /etc/ | grep cron
drwxr-xr-x 2 root root 26 Apr 7 04:59 cron.d #Directory to put automatic task setting files other than the following
drwxr-xr-x 1 root root 44 Apr 7 04:59 cron.daily #Directory for storing automatic task configuration files that run daily
drwxr-xr-x 2 root root 26 Apr 7 04:59 cron.hourly #Directory to put the automatic task configuration file that runs every hour
drwxr-xr-x 2 root root 26 Apr 7 04:59 cron.monthly #Directory to put the automatic task configuration file that runs every month
drwxr-xr-x 2 root root 26 Apr 7 04:59 cron.weekly #Directory for the weekly automatic task configuration files
-rw-r--r-- 1 root root 1042 Oct 11 07:58 crontab #Hourly, daily, monthly, weekly automatic task main configuration file
#Check the startup status of cron
$ /etc/init.d/cron status
[FAIL] cron is not running ... failed!
#Since I just installed cron, no tasks are registered
$ crontab -l
no crontab for root
#Start cron
$ /etc/init.d/cron start
[ ok ] Starting periodic command scheduler: cron.
#Check the startup status of cron
$ /etc/init.d/cron status
[ ok ] cron is running.
# cron.git in d directory-Create a configuration file called cron and register the task
$ vi /etc/cron.d/git-cron
#The configuration file seems to need a blank line at the end
$ cat /etc/cron.d/git-cron
*/5 * * * * root /path/to/git-fetch.sh >> /var/log/cron.log 2>&1
#Give execute permission to the configuration file
$ chmod 755 /etc/cron.d/git-cron
$ ls -la /etc/cron.d/git-cron
-rwxr-xr-x 1 root root 51 Apr 7 05:46 git-cron
#Create a shell to run
$ vi /path/to/git-fetch.sh
# (Not the main subject)The content is to update Git of Redmine
$ cat /path/to/git-fetch.sh
#!/bin/sh
cd /path/to/hoge.git/
git fetch origin 'refs/heads/*:refs/heads/*'
cd /path/to/redmine
bundle exec rake redmine:fetch_changesets RAILS_ENV=production
#Give execute permission to the executing shell
$ chmod 755 /path/to/git-fetch.sh
$ ls -la /path/to/git-fetch.sh
-rwxr-xr-x 1 root root 185 Apr 7 05:57 /path/to/git-fetch.sh
/bin/sh: 1: /path/to/git-fetch.sh: Permission denied --Event: There was an error in the log that was supposed to be output in the configuration file. --Cause: The shell to be executed does not have execute permission. --Action: Give the shell execute permission
$ ls -la /path/to/git-fetch.sh
-rw-r--r-- 1 root root 185 Apr 7 05:57 git-fetch.sh
$ chmod 755 /path/to/git-fetch.sh
$ ls -la /path/to/git-fetch.sh
-rwxr-xr-x 1 root root 185 Apr 7 05:57 /path/to/git-fetch.sh
I wanted to "run every 5 minutes", but it was "run every 5 minutes" ...
Reference: How to set cron --Qiita
Minutes Hour Sun Monday{Execution command}
#Run at 5 minutes every hour
5 * * * * root {Execution command}
#Run every 5 minutes
*/5 * * * * root {Execution command}