简体   繁体   English

Linux服务器:如何使用nohup确保作业正常运行?

[英]Linux server: How do I use nohup make sure job is working?

What I know and what I've tried: I have a script in R (called GAM.R) that I want to run in the background that outputs .rdata, .pdf, and .jpg files. 我所知道和尝试的事情:我在R中有一个脚本(称为GAM.R),该脚本要在运行.rdata,.pdf和.jpg文件的后台运行。 Running this from the command line is relatively simple: 从命令行运行此命令相对简单:

$ Rscript GAM.R

However, this code takes a very long time to run so I would love to send it to the background and let it work even after I have logged out and turned the computer off. 但是,此代码需要很长时间才能运行,所以我很想将其发送到后台,即使在我注销并关闭计算机后也可以正常工作。 I understand this is pretty easy, as well, and my code would look like this: 我知道这也很容易,我的代码如下所示:

$ nohup Rscript GAM.R >/dev/null 2>&1 &

I used this to see if it was working: 我用它来查看它是否有效:

$ fg
nohup Rscript GAM.R > /dev/null 2>&1

The problem: I don't know how to check if the code is working (is there a way I can see its progress?) and I don't know where the outputs are going. 问题:我不知道如何检查代码是否正常工作(有没有办法查看其进度?),我也不知道输出在哪里。 I can see the progress and output with the first code so I must not be too far off. 我可以看到第一个代码的进度和输出,所以我离得太远了。 It doesn't seem that the second code's outputs are going where the first code's outputs went. 似乎第二个代码的输出没有到达第一个代码的输出去的地方。

Your command line is diverting all output to /dev/null aka, The Bit Bucket. 您的命令行会将所有输出转移到/dev/null也就是The Bit Bucket。

Consider diverting it to a temporary file: 考虑将其转移到一个临时文件:

   $ nohup Rscript GAM.R >/tmp/GAM.R.output 2>&1 &

Then you can tail /tmp/GAM.R.output to see the results, it will show the last 10 lines of the file by default. 然后,您可以在tail /tmp/GAM.R.output查看结果,默认情况下它将显示文件的最后10行。 You can use tail -f to show the end of the file, plus new output in real time. 您可以使用tail -f显示文件的末尾,以及实时的新输出。

Note, the /tmp/ filesystem is not guaranteed to exist between reboots. 注意,不能保证/tmp/文件系统在两次重新引导之间存在。 You can put the file somewhere else (like ~/GAM.R.output if you need to be sure. 您可以将文件放在其他位置(例如,如果需要确定,例如~/GAM.R.output

Note, however, that if you turn your computer off, then all processing gets aborted. 但是请注意,如果您关闭计算机,则所有处理都将中止。 For this to work your machine must continue to run and not go to sleep, or shutdown. 为此,您的计算机必须继续运行并且不能进入睡眠或关闭状态。

What you are doing is that with the > you are redirecting the output of your script to /dev/null and by doing 2>&1 you are redirecting the error output to the same place. 您正在执行的操作是使用>将脚本的输出重定向到/dev/null并通过执行2>&1将错误输出重定向到同一位置。 Finally nohup executes your process and detach it from the current terminal. 最后, nohup执行您的进程并将其与当前终端分离。

So to sum up what you are doing is creating a process and redirecting its output and error output to a file called null that is stored under /dev . 因此,总而言之,您正在创建一个进程,并将其输出和错误输出重定向到存储在/dev下的名为null的文件。

To answer your question I suggest you redirect your outputs to a folder that you can access as normal user and not super user. 为了回答您的问题,我建议您将输出重定向到一个可以作为普通用户而非超级用户访问的文件夹。 Then to make sure that everything is ok you can print this file. 然后,确保一切正常,您可以打印此文件。

For example you can do : 例如,您可以执行以下操作:

nohup Rscript GAM.R >/home/username/documents/output_file 2>&1 &

and then to see the file from a terminal you can do: 然后从终端查看文件,您可以执行以下操作:

cat /home/username/documents/output_file

Lastly I don't think that your program will keep on running if your turn off your pc and I don't think there is a way to do that. 最后,我认为如果您关闭计算机,您的程序将不会继续运行,并且我认为没有办法做到这一点。

If you want to run your program in the background and access the output of the program you can easily do that by writing 如果要在后台运行程序并访问程序的输出,则可以通过编写以下代码轻松完成此操作

exec 3< <(Rscript GAM.R)

And then when you wish to check the output of the program run 然后当您希望检查程序运行的输出时

cat <&3 # or you can use 'cat /dev/fd/3'

Excellent! 优秀的! Thanks everyone for your helpful answers, particularly @Greg Tarsa. 感谢大家的有用回答,特别是@Greg Tarsa。 Ultimately I needed to use: 最终,我需要使用:

$ nohup Rscript GAM.R >/usr/emily/gams/2017_03_14 2>&1 & 

The above is used to run the script and save the screen output to emily/gams (called "2017_03_14", a file to be made by the command, not a folder as I had origionally thought). 上面的代码用于运行脚本并将屏幕输出保存到emily / gams(称为“ 2017_03_14”,该文件由命令创建,而不是我原来认为的文件夹)。 This also outputs my .rdata, .pdf, and .jpg output filesf from the script to usr/emily. 这还会将我的.rdata,.pdf和.jpg输出文件f从脚本输出到usr / emily。

Then I can see progress and running programs using: 然后,我可以使用以下命令查看进度和运行程序:

$ tail -f 2017_03_14 #Shows the last 10 lines of the program's progress
$ ps #shows your running projects
$ ps -fu emily #see running projects regardless of session, where username==emily

In the spirit of completeness, I can also note here that to cancel a process, you can use: 本着完整性的精神,我在这里还可以指出,要取消某个流程,您可以使用:

$ kill -HUP processid #https://kb.iu.edu/d/adqw 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM