可以使用!
得到PID更防呆方式最后一个命令。
我建议类似于以下的东西,这也检查,如果你想运行的进程已经运行:当你想杀死它
#!/bin/bash
if [[ ! -e /tmp/test.py.pid ]]; then # Check if the file already exists
python test.py & #+and if so do not run another process.
echo $! > /tmp/test.py.pid
else
echo -n "ERROR: The process is already running with pid "
cat /tmp/test.py.pid
echo
fi
然后:
#!/bin/bash
if [[ -e /tmp/test.py.pid ]]; then # If the file do not exists, then the
kill `cat /tmp/test.py.pid` #+the process is not running. Useless
rm /tmp/test.py.pid #+trying to kill it.
else
echo "test.py is not running"
fi
当然,如果在命令启动后一段时间内发生杀戮,您可以将所有内容放在同一个脚本中:
#!/bin/bash
python test.py & # This does not check if the command
echo $! > /tmp/test.py.pid #+has already been executed. But,
#+would have problems if more than 1
sleep(<number_of_seconds_to_wait>) #+have been started since the pid file would.
#+be overwritten.
if [[ -e /tmp/test.py.pid ]]; then
kill `cat /tmp/test.py.pid`
else
echo "test.py is not running"
fi
如果您希望能够同时运行更多具有相同名称的命令,并且能够选择性地杀死它们,则需要对该脚本进行小量编辑。告诉我,我会尽力帮助你!
有了这样的事情,你确定你正在杀死你想杀的东西。类似pkill
或grey ps aux
的命令可能有风险。
您是否在'ps'的进程信息中找到了关键字“test.py”? – staticor