Linux 脚本启动、停止、重启

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4797717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:37:48  来源:igfitidea点击:

Linux script start,stop,restart

linux

提问by Erma

Can anyone please tell me what exactly this script does?

谁能告诉我这个脚本到底是做什么的?

#! /bin/sh
test –f /usr/bin/sshd || exit 0
case “” in
    start)
            echo –n “Starting sshd: sshd”
            /usr/sbin/sshd
            echo “.”
            ;;
   stop) 
          echo –n “Stopping sshd: sshd”
          kill `cat /var/run/sshd.pid`
          echo “.”
           ;;
   restart)
          echo –n “Stopping sshd: sshd”
          kill `cat /var/run/sshd.pid`
          echo “.”
          echo –n “Starting sshd: sshd”
            /usr/sbin/sshd
            echo “.”
            ;;
        *)
          echo “Usage: /etc/init.d/sshd start|stop|restart”
          exit 1
          ;;
    esac

I want to know what exactly this part:

我想知道这部分到底是什么:

#! /bin/sh
test –f /usr/bin/sshd || exit 0
case “” in
    start)
            echo –n “Starting sshd: sshd”
            /usr/sbin/sshd
            echo “.”
            ;;

does because the other part is the same! Please ;)

是因为另一部分是一样的!请 ;)

采纳答案by CanSpice

Which other part is the same? The way that script works is it checks the value of $1, which is the first parameter to the script supplied on the command-line. If it's 'start', then the part after start)is executed. If it's 'stop', then the part after stop)is executed. If it's 'restart', then the part after restart)is executed.

哪一部分是一样的?脚本的工作方式是检查 的值$1,它是命令行上提供的脚本的第一个参数。如果是“开始”,则start)执行后面的部分。如果是“停止”,则stop)执行后面的部分。如果是“重启”,则restart)执行后面的部分。

Line by line for that first part:

第一部分逐行:

#! /bin/sh

Hey, it's a shell script! Specifically, execute this script using the shshell.

嘿,这是一个shell脚本!具体来说,使用shshell执行此脚本。

test –f /usr/bin/sshd || exit 0

Is there a file called /usr/bin/sshd? If not, exit with a 0 return status.

有没有叫的文件/usr/bin/sshd?如果不是,则以 0 返回状态退出。

case “” in

Check the value of $1, the first command-line option.

检查$1第一个命令行选项 的值。

    start)

If $1is 'start'...

如果$1是“开始”...

            echo –n “Starting sshd: sshd”

Print "Starting sshd: sshd".

打印“ Starting sshd: sshd”。

            /usr/sbin/sshd

Execute /usr/sbin/sshd.

执行/usr/sbin/sshd

            echo “.”

Print ".".

打印“ .”。

            ;;

Exit the casestatement.

退出case语句。

回答by Joe

sshd writes its process ID to the file in /var/run. The backticks cause the script inside them to be executed by the shell, and the result is used in its place.

sshd 将其进程 ID 写入 /var/run 中的文件。反引号导致其中的脚本由 shell 执行,并在其位置使用结果。

The net result is kill [sshd pid]

最终结果是杀死 [sshd pid]

回答by ChrisJ

The part you mention starts the "sshd" program. It's the Secure Shell (SSH) daemon (server process).

您提到的部分启动“sshd”程序。它是 Secure Shell (SSH) 守护进程(服务器进程)。

Depending on the command-line argument, your script starts, stops or restarts the SSH server. $1represents the command-line argument, and this value gets compared to the different possible values between caseand esac.

根据命令行参数,您的脚本启动、停止或重新启动 SSH 服务器。$1表示命令行参数,并且该值与case和之间的不同可能值进行比较esac

回答by Apalala

The test –f /usr/bin/sshdpart returns trueif the file exists. The ||is an or, so the || exit 0gets executed (ending the script) only if the first part returned false.

如果文件存在,则该test –f /usr/bin/sshd部件返回true。该||是一个因此,|| exit 0被执行(结束脚本)仅在第一部分返回false。

In the case "$1"part, the $1is the first argument passed to the script.

case "$1"部分中,$1是传递给脚本的第一个参数。

回答by JohnK813

"I want to know what exactly this part... does because the other part is the same!"

“我想知道这部分到底是做什么的……因为另一部分是一样的!”

start)assumes that sshd is not already started, and starts it.

start)假定 sshd 尚未启动,然后启动它。

This is different from restart), which first stops the sshd process (as Joe describes), and then starts it again.

这与restart)不同,后者首先停止 sshd 进程(如 Joe 所述),然后再次启动它。