Fundamentals 10 min read

Master Linux Shell: Build Custom Log, Backup, and SpringBoot Management Scripts

This tutorial walks through essential Linux command-line techniques, showing how to create a reusable logmsg command, a timestamped backup utility, and a comprehensive Spring Boot administration script, complete with code examples, symbolic linking, permission handling, and execution guidance.

Programmer DD
Programmer DD
Programmer DD
Master Linux Shell: Build Custom Log, Backup, and SpringBoot Management Scripts

The article introduces common Linux commands and basic concepts, then demonstrates practical shell scripting through three real‑world examples.

1. Write a custom log command: logmsg

A function LogMsg prints a timestamp followed by a message, enabling consistent logging across scripts.

##→编写LogMsg函数,每次调用,重新打印时间戳
LogMsg () {
  local_time=`date +"%Y-%m-%d %H:%M:%S"`
  echo $local_time $1
}

if [ $sshd_count -lt 1 ]; then
  LogMsg '没有找到ssh服务,重启该服务'   # 调用LogMsg函数
  systemctl start sshd
else
  LogMsg 'ssh服务正常运行中...'        # 调用LogMsg函数
fi

Re‑execution example

[root@linuxido /shell]# sh 004-while.sh
2021-06-07 07:35:11 ssh服务正常运行中...
2021-06-07 07:35:16 ssh服务正常运行中...
...

To make the script globally available, a symbolic link is created in /bin and executable permissions are set.

##→创建logmsg命令的符号链接
ln -s /shell/logmsg.sh /bin/logmsg
chmod 555 /bin/logmsg   # 赋予所有用户可读可执行权限
logmsg linuxido.com    # 直接执行,输出带时间戳的日志
2021-06-07 23:25:19 linuxido.com

2. Write a common backup command: backup

The backup script adds a timestamp to the filename before copying, providing a simple versioned backup.

# vi bachup.sh
#!/bin/bash
# description:编写备份常用脚本

function backup() {
  newfile=$1.`date +%Y-%m-%d.%H%M.bak`   # 新文件名称,加上时间戳
  cp -p $1 $newfile                     # 复制一份文件
  echo "Backed up $1 to $newfile."      # 打印成功日志
}
backup $1

After creating the script, a symbolic link is placed in /bin for global use.

# ln -s /shell/bachup.sh /bin/backup
chmod 555 /bin/backup
touch sss.txt
backup sss.txt
Backed up sss.txt to sss.txt.2021-06-20.1034.bak.
ls -l sss.txt*
-rw-r--r-- 1 root root 0 sss.txt
-rw-r--r-- 1 root root 0 sss.txt.2021-06-20.1034.bak

3. Write a Java project management script: springboot-admin.sh

The script manages a Spring Boot JAR with start, stop, restart, and status operations, validating parameters and handling process control.

operation=$1      ## 第一个参数:start|stop|restart|status
springboot=$2    ## 第二个参数:Jar 包名

showUsage() {
  if [ "$operation" == "" ]; then
    echo "Usage: $0 {start|stop|restart|status} <jar>"
    exit 1
  fi
  if [ "$springboot" == "" ]; then
    echo "Jar file required"
    exit 1
  fi
  count=`ls $springboot | wc -l`   # 校验 jar 是否存在
}

function start() {
  count=`ps -ef | grep java | grep $springboot | grep -v grep | wc -l`
  if [ $count != 0 ]; then
    echo "$springboot is running..."
  else
    nohup java -server -Xmx1g -Xms1g -Xss512k -jar $springboot > /dev/null 2>&1 &
    echo "Start $springboot success..."
  fi
}

function stop() {
  count=`ps -ef | grep java | grep $springboot | grep -v grep | wc -l`
  if [ $count != 0 ]; then
    echo "Stop Success! 优雅关闭 $springboot Process..."
    kill $boot_id
  fi
}

function restart() {
  stop
  sleep 2
  start
}

function status() {
  count=`ps -ef | grep java | grep $springboot | grep -v grep | wc -l`
  jarStatus=`ps -ef | grep java | grep $springboot`
}

showUsage
case $1 in
  start)   start;;
  stop)    stop;;
  restart) restart;;
  status)  status;;
  *) echo -e "\033[0;34m 请正确操作:{start|stop|restart|status}.
 Example:bash springboot-admin.sh start test-springboot.jar \033[0m";;
esac

Conclusion

After completing this chapter, readers understand basic shell environment configuration, built‑in variables, core syntax, and how to write custom commands for service management, including logging, backup, and Java application control.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationBackupSystem AdministrationBashShell scripting
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.