Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 每天使用shell启动和停止进程?_Bash_Shell_Cron - Fatal编程技术网

Bash 每天使用shell启动和停止进程?

Bash 每天使用shell启动和停止进程?,bash,shell,cron,Bash,Shell,Cron,我知道我可以用cron启动这个过程,但是我该如何停止它呢?我想在每天00:00开始一个新的进程1,然后在23:59开始一个进程2,执行ps-aux | grep process1,从线路中获取pid,然后终止它,但我想知道是否有更好的方法 如果这两种语言更容易使用,我也可以使用Python或Java。记录其PID: nohup my_service & echo $! > /var/run/my_service.pid 然后被那个PID杀死 您可能希望同时检查PID和进程名称,以

我知道我可以用cron启动这个过程,但是我该如何停止它呢?我想在每天00:00开始一个新的进程1,然后在23:59开始一个进程2,执行
ps-aux | grep process1
,从线路中获取pid,然后终止它,但我想知道是否有更好的方法

如果这两种语言更容易使用,我也可以使用Python或Java。

记录其PID:

nohup my_service &
echo $! > /var/run/my_service.pid
然后被那个PID杀死


您可能希望同时检查PID和进程名称,以确保进程在一天中没有崩溃,并且其他进程没有得到该PID。(更好的办法是设立监管机构。)但是,如果您确定没有其他进程使用该名称,您可以简单地使用
pkill
killall

为什么不让进程自行终止?
pgrep
是从进程列表中grep PID的正确方法。@Fosco我是linux noob,但在我看来,这并不总是可能的@Sjoerd这正是我需要的,谢谢