Linux 如何查找作为 nohup 运行的脚本是否完成?

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

how to find whether a script run as a nohup finished or not?

linuxunixshellbackground-process

提问by learner135

I tried running a script using nohup like,

我尝试使用 nohup 运行脚本,例如,

nohup script.sh &

When I tried

当我尝试

ps -ef | grep "script.sh"

I couldn't find it there except for the grep which is being run with that string as a parameter.

除了以该字符串作为参数运行的 grep 之外,我在那里找不到它。

Am I doing it right?. Does this mean that the process has indeed finished execution? Thanks.

我做得对吗?这是否意味着该流程确实已执行完毕?谢谢。

采纳答案by Edward Dale

At the beginning of your shell script, write the PID to a file (for example, in /var/run). Then, you can just search for that PID to know if the process is done or not. You can get the PID of your shell script using the built-in $$variable.

在 shell 脚本的开头,将 PID 写入文件(例如,在 /var/run 中)。然后,您只需搜索该 PID 即可知道该过程是否已完成。您可以使用内置$$变量获取 shell 脚本的 PID 。

To record the PID, put at the top of your script:

要记录 PID,请放在脚本的顶部:

echo $$ > /var/run/myscript.pid

Then, to check if it's still running:

然后,检查它是否仍在运行:

ps -p `cat /var/run/myscript.pid`

You might not be able to write into /var/runas a normal user. If not, just use /tmp

您可能无法以/var/run普通用户的身份写入。如果没有,只需使用/tmp

回答by unbeli

Subject to nohup implementation, but in most cases it will work. After running

取决于 nohup 实现,但在大多数情况下它会起作用。运行后

nohup script.sh &

store the PID into a variable. $! is the PID of the last background process.

将PID存储到变量中。$! 是最后一个后台进程的PID。

HISPID=$!

Then you can check if it's there with ps or kill:

然后你可以用 ps 或 kill 来检查它是否存在:

ps -p $HISPID
kill -0 $HISPID

Unlike the other solution posted, this does not require modifying the script.sh

与发布的其他解决方案不同,这不需要修改 script.sh

回答by jim mcnamara

$! is definitely part of ksh and ksh93.

$! 绝对是 ksh 和 ksh93 的一部分。

echo $SHELL

will show you what shell you're running.

会告诉你你正在运行什么shell。

Example of reasonable usage of &

& 的合理使用示例

#!/bin/ksh
nohup ./myscript.sh argument1 2>&1> mylogfile &
# do some other task
cnt=0
while [ $cnt -le 100 ]
do
    # work on another task here
    cnt=$(( $cnt + 1 ))
done
wait

The wait statement pauses for any still-running child process. Normally you don't plunk a process out into the background, expect it to run forever, and then completely forget it.

等待语句暂停任何仍在运行的子进程。通常你不会把一个进程放到后台,期望它永远运行,然后完全忘记它。

If you want a fully detached process that runs forever, consider a daemon. Some folks write daemons in shell - not best practice - but it is done. Normally UNIX daemons are written in C.

如果您想要一个永远运行的完全分离的进程,请考虑使用守护进程。有些人在 shell 中编写守护进程——不是最佳实践——但它已经完成了。通常 UNIX 守护进程是用 C 编写的。

Chapter 13 of Stevens ' Advanced Programming in the UNIx Environment' 2ed is all about daemons.

Stevens 'Advanced Programming in the UNIX Environment' 2ed 的第 13 章都是关于守护进程的。

回答by decibel.places

I followed the suggestion by scompt.com modifying my script to store the pid

我按照 scompt.com 的建议修改了我的脚本以存储 pid

Then I noticed the pidis written to the output, so there is no need to store it:

然后我注意到pid被写入输出,所以不需要存储它:

$ nohup ./sync-all.production.sh > sync-all.production.log &

[1] 3428

回答by decibel.places

what has worked is

有效的是

sudo ps -e | grep [script name or fragment of name]

for example for a script named "mf-sync.js"

例如对于名为“mf-sync.js”的脚本

sudo ps -e | grep mf-sync

displays the script name and the pid; then I can use, for example if pid was 1234

显示脚本名称和 pid;然后我可以使用,例如,如果 pid 是 1234

sudo kill 1234

next I need to add a timeout to automatically kill it after enough time has expired for it to run normally, but that is another question

接下来我需要添加一个超时以在足够的时间到期后自动终止它以使其正常运行,但这是另一个问题

meanwhile I can babysit this process to run the sync for my client, until I have time to test the timeout in a script

同时我可以照看这个过程来为我的客户端运行同步,直到我有时间在脚本中测试超时

回答by Helena

Code:

代码:

ps r

to report all running processes.

报告所有正在运行的进程。