[LINUX] Register a task in cron for the first time

--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

Install cron with apt

--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.

Register tasks in cron so that the shell can run on a regular basis

# 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

What failed

/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 made a mistake in writing "Run every 5 minutes" and "Run every 5 minutes"

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}

Recommended Posts

Register a task in cron for the first time
MongoDB for the first time in Python
Kaguru for the first time
A useful note when using Python for the first time in a while
[For self-learning] Go2 for the first time
See python for the first time
Start Django for the first time
Summary of stumbling blocks in Django for the first time
I tried tensorflow for the first time
Change the list in a for statement
Let's try Linux for the first time
What I learned by writing a Python Pull Request for the first time in my life
For the first time in Numpy, I will update it from time to time
I tried using scrapy for the first time
How to use MkDocs for the first time
Make a histogram for the time being (matplotlib)
[Note] Deploying Azure Functions for the first time
I tried python programming for the first time.
I tried Mind Meld for the first time
Try posting to Qiita for the first time
Let's display a simple template that is ideal for Django for the first time
Turn multiple lists with a for statement at the same time in Python
I tried Python on Mac for the first time.
I tried python on heroku for the first time
For the first time, I learned about Unix (Linux).
Until you win the silver medal (top 3%) in the competition you participated in within a month for the first time in data science!
AI Gaming I tried it for the first time
Let's make a cron program in Java! !! (Task Scheduler)
After attending school, I participated in SIGNATE's BEGINNER limited competition for the first time.
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time
It's okay to participate for the first time! A hackathon starter kit that you want to prepare "before" participating in the hackathon!
Define a task to set the fabric env in YAML
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
[Understand in the shortest time] Python basics for data analysis
Introducing yourself at Qiita for the first time (test post)
To write a test in Go, first design the interface
I tried the Google Cloud Vision API for the first time
I want to create a Dockerfile for the time being.
If you're learning Linux for the first time, do this!
The story of creating a "spirit and time chat room" exclusively for engineers in the company
What is a dog? Django--Getting Started with Form for the First Time POST Transmission Volume
About the need for the first slash in the subscriber name and publisher name
Build a Selenium environment on Amazon Linux 2 in the shortest time
Put the process to sleep for a certain period of time (seconds) or more in Python
Code that I wish I had remembered when I participated in AtCoder for the first time (Reflection 1 for the next time)
[Introduction to Python] How to use the in operator in a for statement?
A function that measures the processing time of a method in python
[For beginners] How to register a library created in Python in PyPI
The story of releasing a Python text check tool on GitHub x CircleCI for the first time
The first time a programming beginner tried simple data analysis by programming
Settings for running a test each time you save a file in the editor using watchmedo (watchdog)
I made a function to check if the webhook is received in Lambda for the time being
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
Introduction to Deep Learning for the first time (Chainer) Japanese character recognition Chapter 3 [Character recognition using a model]
The story of returning to the front line for the first time in 5 years and refactoring Python Django
The first step in Python Matplotlib
Try using FireBase Cloud Firestore in Python for the time being
If you are a beginner in programming, why not make a "game" for the time being? The story
[Hi Py (Part 1)] I want to make something for the time being, so first set a goal.