Shell - crontab timing git pull and execute maven package

Table of contents

I. Introduction

2. Stepping on pits and practice

1. Original code

2. Mvn package is not executed and resolved [import environment variables]

3. git pull is not executed and resolved [add absolute path]

3. Summary


I. Introduction

The git task is deployed on the channel machine, and the jar package needs to be updated regularly at 6:00 every day and packaged and launched online, so it needs to be on the linux server:

A. Deploying maven: The previous article has laid out the complete process: Linux server Maven environment installation and testing

B. Deploy git: There is already git on the machine, so this step is omitted. If you don’t have git, you can search for the installation process

C.crontab: timed git pull + mvn package , this article mainly carries out the third step of practice.

2. Stepping on pits and practice

1. Original code

# 进入目标环境
echo `date`
echo cd 目录
cd $targetPath

# 更新代码
echo git 更新
git pull

# 重新打包
echo package...
sh $targetPath/package.sh

Add the code to the corontab and point the output stream to the log. After the point is found, there are only echo logs, but no pull and package logs:

Fri Mar 10 14:13:01 CST 2023
cd 目录
git 更新
package...

2. Mvn package is not executed and resolved [import environment variables]

The internal logic of package.sh is very simple, that is, mvn clean package was not executed. Through online query, it was found that it was caused by not adding environment variables. Add a source statement at the front end of the script to introduce environment variables:

source /etc/profile
source /etc/bashrc

After adding, it is found that the mvn package information in the redirected log has been output normally, but the git log is still missing:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:57 min (Wall Clock)
[INFO] Finished at: 2023-03-10T14:16:00+08:00
[INFO] ------------------------------------------------------------------------

3. git pull is not executed and resolved [add absolute path]

It is obvious that the environment variable has been added above. Why does git not take effect? ​​We checked $PATH:

/usr/local/bin

Then use whereis git to view the location of git:

git: /usr/bin/git /usr/share/man/man1/git.1.gz

git is under /usr/bin, PATH is /usr/local/bin, so git can't find it, here it is directly specified as an absolute path in the script:

source /etc/profile
source /etc/bashrc

# 进入目标环境
echo `date`
echo cd 目录
cd $targetPath

# 更新代码
echo git 更新
/usr/bin/git pull

# 重新打包
echo package...
sh package.sh

Add it to crontab again, and this time there are finally git logs:

Fri Mar 10 14:20:01 CST 2023
cd 目录
git 更新
Already up-to-date.
package...

3. Summary

It’s been a long time since I wrote a shell script, so I shouldn’t have spent half a day with such a simple logic, so I reflected deeply:

- Shell scripts must add environment variables

- Add environment variables and use whereis xxx to find the corresponding absolute path if you can’t find it. The following are commonly used:

whereis java
whereis python
whereis maven

Guess you like

Origin blog.csdn.net/BIT_666/article/details/129441997