如何安排流程的终止? [英] How do I schedule a process' termination?

查看:85
本文介绍了如何安排流程的终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一个进程,等待几个小时,将其杀死,然后重新启动.有没有一种简单的方法可以使用Python或Bash做到这一点?我可以在后台运行它,但是如何识别它以对它使用kill?

I need to run a process, wait a few hours, kill it, and start it again. Is there an easy way that I can accomplish this with Python or Bash? I can run it in the background but how do I identify it to use kill on it?

推荐答案

使用bash:

while true ; do
    run_proc &
    PID=$!
    sleep 3600
    kill $PID
    sleep 30
done

$! bash变量扩展为最近启动的后台进程的PID. sleep仅等待一个小时,然后kill关闭该进程.

The $! bash variable expands to the PID of the most recently started background process. The sleep just waits an hour, then the kill shuts down that process.

while循环不断地反复进行.

The while loop just keeps doing it over and over.

这篇关于如何安排流程的终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆