注销后如何保持 Perl 脚本在 Unix 上运行?
我有一个需要很长时间才能完成的脚本。
我宁愿注销并稍后检索其输出,而不是等待它完成。
我已经尝试过了;
at -m -t 03030205 -f /path/to/./thescript.pl
nohup /path/to/./thescript.pl &
我也验证了这些进程实际上存在于 ps
和 at -l
中,具体取决于我使用的调度语法。
当我退出 shell 时,这两个进程都会终止。有没有办法在关闭连接时阻止脚本终止?
我们这里有 cron,它们已设置并正常工作,但我想使用 at
或 nohup
作为一次性脚本。
我的语法有问题吗?还有其他方法可以产生预期的结果吗?
EDIT:
I cannot use
screen
or disown
- they aren't installed in my HP Unix setup and i am not in the position to install them eitherI have a script that takes a lot of time to complete.
Instead of waiting for it to finish, I'd rather just log out and retrieve its output later on.
I've tried;
at -m -t 03030205 -f /path/to/./thescript.pl
nohup /path/to/./thescript.pl &
And I have also verified that the processes actually exist with ps
and at -l
depending on which scheduling syntax i used.
Both these processes die when I exit out of the shell. Is there a way to keep a script from terminating when I close the connection?
We have crons here and they are set up and are working properly, but I would like to use at
or nohup
for single-use scripts.
Is there something wrong with my syntax? Are there any other methods to producing the desired outcome?
EDIT:
I cannot use
screen
or disown
- they aren't installed in my HP Unix setup and i am not in the position to install them either如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用屏幕。它创建一个终端,当您注销时该终端会继续运行。当您重新登录时,您可以切换回原来的状态。
Use screen. It creates a terminal which keeps going when you log out. When you log back in you can switch back to it.
如果您想在注销后保持进程运行:
disown -h
是一个有用的 bash 内置函数。与
nohup
不同,您可以在已运行的进程上运行disown
。首先,使用 control-Z 停止工作,从
ps
获取 pid(或使用echo $!
),使用bg
将其发送到背景,然后使用disown
和 -h 标志。不要忘记将您的作业设置为后台,否则当您注销时它会被杀死。
If you want to keep a process running after you log out:
disown -h <pid>
is a useful bash built-in. Unlike
nohup
, you can rundisown
on an already-running process.First, stop your job with control-Z, get the pid from
ps
(or useecho $!
), usebg
to send it to the background, then usedisown
with the -h flag.Don't forget to background your job or it will be killed when you logout.
这只是一个猜测,但我在某些版本的 ssh 和 nohup 中看到过:如果您使用 ssh 登录,那么您可能需要重定向 stdout、stderr 和 stdin 以避免当您退出时让会话挂起。 (其中一个可能仍然连接到终端。)我会尝试:(
我当前版本的 ssh 和 nohup 不再是这种情况 - 后者如果检测到任何连接到终端,就会重定向它们 - 但你可能使用不同的版本。)
This is just a guess, but something I've seen with some versions of ssh and nohup: if you've logged in with ssh then you may need to need to redirect stdout, stderr and stdin to avoid having the session hang when you exit. (One of those may still be attached to the terminal.) I would try:
(This is no longer the case with my current versions of ssh and nohup - the latter redirects them if it detects that any is attached to a terminal - but you may be using different versions.)
nohup 的语法看起来没问题,但您的帐户可能不允许进程在注销后运行。另外,尝试将 stdout/stderr 重定向到日志文件或 /dev/null。
Syntax for nohup looks ok, but your account may not allow for processes to run after logout. Also, try redirecting the stdout/stderr to a log file or /dev/null.
在后台运行您的命令。
获取后台作业的列表
现在,您可以有选择地拒绝上述任何作业及其 jobid。
即使您注销后,所有被拒绝的进程也应继续运行。
Run your command in background.
To get lits of your background jobs
Now you can selectively disown any of the above jobs, with its jobid.
All the disowned process should be keep on running even after you logged out.