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
Linux script start,stop,restart
提问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 sh
shell.
嘿,这是一个shell脚本!具体来说,使用sh
shell执行此脚本。
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 $1
is '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 case
statement.
退出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. $1
represents the command-line argument, and this value gets compared to the different possible values between case
and esac
.
根据命令行参数,您的脚本启动、停止或重新启动 SSH 服务器。$1
表示命令行参数,并且该值与case
和之间的不同可能值进行比较esac
。
回答by Apalala
The test –f /usr/bin/sshd
part returns true
if the file exists. The ||
is an or, so the || exit 0
gets 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 $1
is 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 所述),然后再次启动它。