Linux shell 自启动脚本写法

# **********************************************************************
#                     Linux shell 自启动脚本写法
# 说明:
#     我们在做系统的时候,写自启动脚本是常有的事,下面是一个样板分析。
#
#                                   2017-1-10 深圳 南山平山村 曾剑锋
# **********************************************************************

#!/bin/sh                                           # 脚本运行的shell
#
# sshd        Starts sshd.                          # 描述
#

# 接下来是运行脚本之前需要满足的一些条件,主要是做一些检查性质的内容,
# 防止后续程序在运行的时候,出现一些环境问题,或者对其他的后续程序需要
# 运行的先决条件进行检查。

# Make sure the ssh-keygen progam exists
[ -f /usr/bin/ssh-keygen ] || exit 0                

# Create any missing keys
/usr/bin/ssh-keygen -A

# 如果有文件需要生成,有些权限希望默认给出,这样就不用每次去给权限了。
umask 077

# 当系统开始启动的时候,运行的函数
start() {
    printf "Starting sshd: "
    /usr/sbin/sshd
    touch /var/lock/sshd
    echo "OK"
}

# 当系统开始停止的时候,运行的函数
stop() {
    printf "Stopping sshd: "
    killall sshd
    rm -f /var/lock/sshd
    echo "OK"
}

# 当系统开始重启的时候,运行的函数
restart() {
    stop
    start
}

# 这里可以认为是对上面三个函数进行分支调用
case "$1" in
  start)
    start               # 调用start函数
    ;;
  stop)
    stop                # 调用stop函数
    ;;
  restart|reload)
    restart             # 调用restart函数
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

# 退出并返回执行结果
exit $?
时间: 2024-08-03 15:22:31

Linux shell 自启动脚本写法的相关文章

Linux Shell 小脚本经典收藏

1.在两个文件中找出相同的号码 diff -y xx.txt oo.txt | egrep -v "<|>" | awk '{print $2}' 2.打印第几行到第几行之间 cat 1 | awk 'NR==2,NR==4{print}' 1.删除Linux远程用户连接会话 [[email protected] logs]# w 10:45:28 up 15 days, 16:23, 4 users, load average: 0.00, 0.00, 0.00 USER

Linux Shell之脚本的执行

(1)shell如何执行一个命令 Linux的命令分为两类:一类是shell的内建命令:另一类则是独立于shell的命令.别忘了,shell也只是系统中的一个程序而已,当它执行非内建命令时,本质上是在呼叫另一只程序,比如ls.下面验证一下: m@meng:~/scripts$ which sh /bin/sh m@meng:~/scripts$ file /bin/sh /bin/sh: symbolic link to `dash` m@meng:~/scripts$ file /bin/da

linux shell -常用脚本

1.判断登录用户 1.1脚本 [[email protected]_1 shell]$ vi check_user.sh #! /bin/sh echo "You are logged in as `whoami`"; if [ `whoami` != devtac ]; then echo "Must be logged in as devtac to run this script." exit fi echo "Running script at `

Linux Shell 01 脚本与变量

一 脚本的创建和执行 1. 创建文件*.sh,文件后缀为sh 2. 编辑脚本 首行必须为:#!/bin/bash   #指定解释脚本的shell 3. 赋权 chmod u+x a.sh 4. 执行 ./a.sh 二 shell脚本中的变量 1.变量的定义与删除 name="hello" #定义变量=前后不能有空格 unset name #删除变量 a. 当前shell中定义的变量只在当前shell有效 b. 不论是普通变量还是环境变量,归根结底都是变量,都遵循变量的基本操作 2.环境

Linux 开机自启动脚本详解

以kibana为例 ? ? 以下为skibana名称的脚本内容 #!/bin/bash #chkconfig: 2345 80 90 #description:kibana kibana="/usr/mysoft/kibana-4.4.1-linux-x64/bin/kibana" ? ? $kibana # daemon kibana echo "kibana4.4.1 service is started..." esac ? ? 如果是在windows上创建的

linux shell 学习脚本笔记

[[email protected] C07]# cat 7_1.sh #!/bin/bash if [ -f /etc/hosts ]   then     echo "[1]" fi if [[ -f /etc/hosts ]]   then     echo "[[1]]" fi if test -f /etc/hosts   then     echo "test1" fi [[email protected] C07]# cat 7_2

Linux Shell 相关记录

http://www.tutorialspoint.com/unix/unix-shell.htm Linux Shell 获取脚本的所在目录的绝对路径 basedir=$(cd $(dirname $0); pwd;) echo $basedir shell 执行错误马上退出,而不是继续执行 set -e

Linux Shell 运维脚本功底积累

1.删除Linux远程用户连接会话 [[email protected] logs]# w 10:45:28 up 15 days, 16:23, 4 users, load average: 0.00, 0.00, 0.00 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root tty1 - Sun21 4days 0.00s 0.00s -bash root pts/0 192.168.1.2 09:11 0.00s 0.07s 0

linux的自启动服务脚本的(/etc/rc.d/init.d或者其链接/etc/init.d)

转载地址:http://www.cnblogs.com/diyunpeng/archive/2009/11/11/1600886.html Linux有自己一套完整的启动体系,抓住了linux启动的脉络,linux的启动过程将不再神秘. 本文中假设inittab中设置的init tree为: /etc/rc.d/rc0.d/etc/rc.d/rc1.d/etc/rc.d/rc2.d/etc/rc.d/rc3.d/etc/rc.d/rc4.d/etc/rc.d/rc5.d/etc/rc.d/rc6