[Linux] Execute git pull regularly with the crontab command

I'll show you how to get your PC to do the work on a regular basis using a command called crontab. You can use crontab on a mac with UNIX-like commands, or you can run batches on the server on a regular basis. It's the perfect command to get your PC to work. Let's see how to actually use it. This time, I will show you how to automatically execute git pull to keep the local repository up-to-date.

--Caution for crontab command --Description of crontab --Description in shell script

Notes on the crontab command

The crontab command is basically executed in combination with options. However, since there are notes, once check how to use with man


-l Display the current crontab on standard output.

-r Remove the current crontab.

-e Edit the current crontab using the editor specified by the VISUAL or EDITOR environment vari-
ables. The specified editor must edit the file in place; any editor that unlinks the file and
recreates it cannot be used. After you exit from the editor, the modified crontab will be
installed automatically.

By adding -l, you can see the current current state, By adding -r, you can delete crontab, You can edit by adding -e.

Crontab can be created for each logged-in user.

[The thing to be very careful about here is the difference between the options -e and -r. Since e and r are next to each other on the keyboard, if you make a mistake, it will be irreparable. Before you get used to it, you should always use man crontab to check which one you have before running it.

Description in crontab

In crontab, write the schedule and the command to be executed. This time I will write a shell script and execute it.

Schedule

You can specify minutes, hours, days, months, and days of the week. This is the order from the left. If it is *, it will not be specified, and if it is * / 2, it will be executed every other time.


 */5 * * * *

This is every 5 minutes

 0 23 * * *

This is 23:00 every day

 * */1 * * *

This will be every hour.

If you want to write in more detail, I think you should refer to this article etc.

Let's touch cron-Qiita

command

Please write down the absolute path. This time, write a shell script under / root.

/root/gitpull.sh Just write this next to the schedule.

*/5 * * * * /root/gitpull.sh

After editing crontab, restart it for reloading.


service crond restart

Description in shell script

Let's write git pull.sh.

gitpull.sh


cd (Go to the directory of the project you want to git pull)
git fetch
git reset --hard origin/master
git merge origin/master

For the time being, only the basic type.

git fetch git reset --hard origin/master

Brings the latest state of the remote to origin / master. You can apply changes to local files by reflecting the latest state in master with git merge.

In other words, you're just doing git pull

Please refer to this as well

What is git fetch?-Qiita

[For newcomers] Commands you don't want to use in Git --Qiita

that's all. If you run it every 5 minutes, the latest state of git will always be reflected. It's convenient.

Recommended Posts

[Linux] Execute git pull regularly with the crontab command
[Linux] Git command
Adjust file permissions with the Linux command chmod
Periodically execute commands with crontab
Let's execute the command on time with the bot of discord
Check the memory status of the server with the Linux free command
Check the operating status of the server with the Linux top command
Let's execute commands regularly with cron!
Check the HTTP status code response with the curl command (#Linux #Shell)
[Linux] OS recovery with restore command
[linux] kill command to kill the process
Check server I / O device usage with the Linux iostat command
[Linux / GCP] Corrected the error that occurred when using the Git command.
Using cgo with the go command
Hit the top command with htop
Replace the directory name and the file name in the directory together with a Linux command.
The --security option cannot be used with the dnf command on CentOS Linux
Ask for Pi with the bc command
[Linux] How to use the echo command
How to use the Linux grep command
Notify Slack when the linux command finishes
Try rewriting the file with the less command
Execute the COPY command using python's Psycopg
Linux command # 4
Linux command # 3
Linux command # 5
Tweet regularly with the Go language Twitter API
[Python] Let's execute the module regularly using schedule
Linux: Rename the process displayed by the ps command
Linux: Understand the information displayed by the top command
Compress with zip command of java side linux
It's a Mac. What is the Linux command Linux?
Get the latest Linux kernel version with Arch Linux
Update the XSERVER website with Git push (Note)
I want to use Linux commands at the command prompt! Use Linux commands at the command prompt instead of Git Bash