如果它尚未运行,我如何在后台执行python脚本? 这个例子用在cron上。
如果你想在启动时启动一个脚本,理论上它保持不变,那么你应该把它作为一个服务添加到init.d中,并设置适当的运行级别。
当然,如果它应该下降,那么你会想要回来。
要做到这一点,你可以有一个shell脚本运行ps -aux | 像这样的grep'nameOfYourScript'。 当然,不包括grep命令,这将匹配以及大声笑。 有这样的脚本每五分钟用这样的cron * / 5 * * * *用户checkScript.sh检查
您可以编写checkScript来启动程序。
你可以做这样的事情:
#!/bin/bash # count the number of processes that match "NameOfProcess" but # exclude the grep process itself pc=`ps -ef |grep "NameOfProcess" |grep -v grep |wc -l` # if pc > 0, the process is already running and we can exit. if [ $pc -gt 0 ] ; then echo "Application is already running, exiting." exit 0 fi # othewise start the program /path/to/program
把这个放在crontab中或者从头开始
nohup /path/to/script.sh &
如果退出ssh会话,nohup会确保进程没有closures。 screen是另一种select