shell to automate the deployment project

shell command to obtain the return value

    res=`docker run -dti -v $rootdir:/monitor -v /etc/localtime:/etc/localtime:ro 86912afeeaa8 /bin/bash`
    echo $res
    echo "${res} container is running....."

Strings and variables splicing

       imagedir="$rootdir/docker/escron.tar"

The difference between single and double quotes shell

       

 windows and linux platform compatible

       With vi / vim command to open the script file, enter ": set fileformat = unix", Enter, save and exit

Write a single line of text and multiple lines of text

       Single-line text

          echo Hello > filename.txt

       Multiple lines of text 

        cat>${shelldir}/hostcron.sh<<EOF
          #! /bin/bash
          docker exec ${res} /bin/bash /monitor/shell/crontbday.sh 

          echo $(date "+%Y-%m-%d %H:%M:%S") >>${rootdir}/msgday.log

          echo \ $ (date "+% Y-% m-% d% H:% M:% S") >> $ {rootdir} /msgday.log \ indicates that the following command as the escape character string is written to the file instead of the command execution results are written to the file
         EOF

The example code

#! /bin/bash
# Execute this script requires root privileges


basedir=`cd $(dirname $0); pwd -P;`
rootdir=$(dirname $basedir)
echo "the root dir is $rootdir"

echo 'loadding docker images'
imagedir="$rootdir/docker/escron.tar"
echo "the image at ${imagedir}"
docker load -i $imagedir
echo "loadding docker image complete...."

echo 'run a docker container......'
res=`docker run -dti  -v $rootdir:/monitor  -v /etc/localtime:/etc/localtime:ro 86912afeeaa8  /bin/bash`
echo $res
echo "${res} container is running....."

shelldir="$rootdir/shell"
echo "update the containerID to the shell"

cat>${shelldir}/hostcron.sh<<EOF
#! /bin/bash
docker exec ${res} /bin/bash /monitor/shell/crontb.sh
echo \$(date "+%Y-%m-%d %H:%M:%S") >>${rootdir}/msg.log
EOF

cat>${shelldir}/hostcron.sh<<EOF
#! /bin/bash
docker exec  ${res}  /bin/bash /monitor/shell/crontbday.sh
echo \$(date "+%Y-%m-%d %H:%M:%S") >>${rootdir}/msgday.log
EOF

echo 'setting the system crontab'
echo "*/60 * * * * sh ${shelldir}/hostcron.sh" >> /var/spool/cron/root
echo "1 0 * * * sh ${shelldir}/hostcronday.sh" >> /var/spool/cron/root
echo "the task is success finished.....!"
View Code

 

Guess you like

Origin www.cnblogs.com/yxh168/p/12133824.html