标签归档:bash

链接

玩转Bash脚本

介绍Unix-like系统下最流行的Shell——Bash的脚本语法。不管是linux或者OS X都采用了Bash。本系列博文从一门编程语言的角度来介绍Bash,而非是介绍Shell中的各种命令。内容包括变量,流程控制,数组,函数,字符串处理等等。满足linux初学者的日常需要。

https://blog.csdn.net/guodongxiaren/column/info/wanbash

ubuntu中创建程序为自启动

#!/bin/bash
### BEGIN INIT INFO
# Provides:          appname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: appinfo
# Description:       appinfo
### END INIT INFO

bin="/home/user/app"

start(){
  ps -ef | grep $bin | grep -v grep > /dev/null
  if [ $? -eq 0 ]; then
    echo "The Service has started."
  else
    echo -n "Starting..."
    $bin 1>/dev/null 2>/dev/null &
    echo "Done!"
  fi
}

stop(){
  ps -ef | grep $bin | grep -v grep > /dev/null
  if [ $? -eq 0 ]; then
    echo -n "Stoping..."
    killall $bin
    echo "Done!"
  else
    echo "The service did not start."
  fi
}

case $1 in
start)
  start
;;
stop)
  stop
;;
status)
  ps -ef | grep $bin | grep -v grep > /dev/null
  if [ $? -eq 0 ]; then
    echo "The service is running."
  else
    echo "The service is not running."
  fi
;;
*)
  echo "Usage: $0 {start|stop|status}"
;;
esac

脚本放在 /etc/init.d/ 目录下,添加 执行 权限。

# 添加到服务
sudo systemctl enable app  # app 是服务文件名
# 启动服务
sudo service app start

继续阅读