Cronjob Setup up - Ubuntu 20.04

Start cronjob

crontab -e
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
# We start at 6:00AM UTC, 11:00PM PDT every day
0 6 * * * /home/shumeliu/CronTest/start_banff_nightly_stress.sh >> /home/shumeliu/CronTest/banff_nightly_stress_test.log 2>&1
crontab -l

Setup Bash file for cronjob executing

Tips for the script:

Git head short hash

commit_id=$(git rev-parse --short HEAD)

Get week of the year - woy

woy=$(date +"%V")

bash get week of the year - Search (bing.com)
string - How to use the ‘date’ command to display week number of the year? - Unix & Linux Stack Exchange

Get day of the week - dow

dow=$(date +%u)

bash - get week of year from day of year - Stack Overflow

Check if directory already exists

if [ ! -d "$bundle_name" ]; then
  mkdir $bundle_name
fi

Check if the fw build successfully downloaded

shell - How to get a response from any URL? - Unix & Linux Stack Exchange

Example

curl -ivs  https://genesis-dev.webbank.ssmb.com:21589/opconsole-sit/opconsole.html#
if [ $? -ne 0 ]
then echo "Server is UP"
else
echo "Server is down"
fi

our implementation

# Download the nightly build bundle packages
# If bundle is not ready sleep 15 minutes then try again.
download_ready=0
while [[ $download_ready == 0 ]]
do
    http_status=$(curl -w "%{http_code}" -u $user_cred $full_path --output $package_name)
    if [[ $http_status -ge 200 && $http_status -lt 300 ]]; then
        echo "Bundle package successfully downloaded to $bundle_holder/$package_name"
        download_ready=1
    else
        echo "Bundle download failed, need delay sleep 15 minutes and try again"
        sleep 15m
    fi
done

To search and replace fw1 test bundle name from config file

  sed -i.bak "/^fw1: /c\\$new_fw1/"  "$config_file"

    # Check if the replacement was successful
    if grep -q "^$new_fw1" "$config_file"; then
        echo "Line successfully replaced."
    else
        echo "Failed to replace the line."
    fi 

To get the HEAD commit date

To get the commit date from the HEAD of a Git branch, you can use the git log command with specific options to display the commit date of the latest commit. Here are the steps and a few different ways to do this:

  1. Using git log

You can use git log with the -1 option to show just the latest commit and the --format option to format the output to show only the date.

git log -1 --format=%cd

This will output the commit date of the HEAD commit in the default date format.

  1. Using git show

The git show command can also be used to display information about the latest commit.

git show -s --format=%cd
  1. Using git rev-parse with git show

You can combine git rev-parse to get the commit hash of the HEAD and git show to display the commit date.

git show -s --format=%cd $(git rev-parse HEAD)
  1. Custom Date Formats

If you want to display the date in a specific format, you can use the --date option along with --format. For example, to display the date in ISO 8601 format:

git log -1 --format=%cd --date=iso

Or to display the date in a relative format:

git log -1 --format=%cd --date=relative

Example Outputs

Default format:

git log -1 --format=%cd
Output: Thu Jun 22 12:34:56 2023 -0700

ISO 8601 format:

git log -1 --format=%cd --date=iso
Output: 2023-06-22 12:34:56 -0700

Relative format:

git log -1 --format=%cd --date=relative
Output: 2 hours ago

Put command to background

command > /dev/null 2>&1 & 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值