php 如何停止在 UNIX 中运行的脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11746104/
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-25 01:59:09  来源:igfitidea点击:

How do I stop a script running in UNIX?

phpunixcronterminal

提问by Don P

I ran a php script, let's use "mytestscript.php" for example.

我运行了一个 php 脚本,例如让我们使用“mytestscript.php”。

It will run continuously for a few hours. How can I stop it from the Terminal (UNIX) command line?

它将连续运行几个小时。如何从终端 (UNIX) 命令行停止它?

回答by theglauber

Assuming it's running in the background, under your user id: use psto find the command's PID. Then use kill [PID]to stop it. If killby itself doesn't do the job, do kill -9 [PID].

假设它在后台运行,在您的用户 ID 下:用于ps查找命令的 PID。然后使用kill [PID]来阻止它。如果kill它本身不能完成这项工作,请执行kill -9 [PID].

If it's running in the foreground, Ctrl-C (Control C) should stop it.

如果它在前台运行,Ctrl-C (Control C) 应该停止它。

Read the documentation on the pscommand and familiarize yourself with its options. It's a very useful command.

阅读有关该ps命令的文档并熟悉其选项。这是一个非常有用的命令。

回答by valscion

You can try using ps -e | grep phpto find all processes that have 'php' in their name. After that, you can do kill <PID>, replacing <PID>with the number the ps command gave you.

您可以尝试使用ps -e | grep php查找名称中包含“php”的所有进程。之后,您可以执行kill <PID>,替换<PID>为 ps 命令给您的数字。

回答by dopplesoldner

If you want an automated solution try this -

如果你想要一个自动化的解决方案试试这个 -

1.Create a new shell script - vi killer.sh

1.新建一个shell脚本—— vi killer.sh

2.Add the following

2.添加以下内容

#!/bin/bash

while true
do
  sleep 
  ps -ef | grep mytestscript.php | grep -v grep | awk '{print }' | xargs kill -9  
done

3.Grant executable permissions chmod +x killer.sh

3.授予可执行权限 chmod +x killer.sh

4.Execute your script as nohup ./killer.sh <time to sleep before killing> &

4.执行你的脚本 nohup ./killer.sh <time to sleep before killing> &

5.Leave it and go to the beach!

5.离开它去海滩吧!

回答by Manisha Mahawar

You can always find the process id of the running process. "ps -ef | grep mytestscript.php". Look at the output and note down pid of the process. use kill pid to kill the process.

您始终可以找到正在运行的进程的进程 ID。“ps -ef | grep mytestscript.php”。查看输出并记下进程的 pid。使用 kill pid 终止进程。