Php 不断运行python脚本

Php 不断运行python脚本,php,python,cgi,Php,Python,Cgi,我有一个python脚本,当网站上发生变化时,它会给我发短信。我怎样才能把它放在服务器上,这样它就可以全天候检查,而不必让我的笔记本保持打开状态?任何教程或文档都可以 我到处都在寻找这样做的方法,但真的找不到太多。我知道可以使用php来完成,但我不确定这是否是最好的方法。您需要设置一个cron作业。“Cron”是一个基于时间的作业调度器 例: 此Cron将每小时运行一次 下面是一个您需要作为守护进程运行python脚本。也就是说,将其与控制终端分离。您可以很容易地将其包装在shell脚本中,如下

我有一个python脚本,当网站上发生变化时,它会给我发短信。我怎样才能把它放在服务器上,这样它就可以全天候检查,而不必让我的笔记本保持打开状态?任何教程或文档都可以


我到处都在寻找这样做的方法,但真的找不到太多。我知道可以使用php来完成,但我不确定这是否是最好的方法。

您需要设置一个cron作业。“Cron”是一个基于时间的作业调度器

例:

此Cron将每小时运行一次


下面是一个

您需要作为守护进程运行python脚本。也就是说,将其与控制终端分离。您可以很容易地将其包装在shell脚本中,如下所示

#!/usr/bin/env bash

script_file=$(readlink -f "$0")  #absolute path to this file
python_file=$(readlink -f "$1")  #absolute path to the python script
output_file=$([[ "$2" == "" ]] && echo "/dev/null" || readlink -f "$2")  #absolute path to the output file

(
    exec 0>&- 0>/dev/null #close and redirect stdin
    exec 1>&- 1>>"${output_file}" #close and redirect stdout
    exec 2>&- 2>>"${output_file}" #close and redirect stderr
    python "$python_file" 
    "$script_file" "$python_file" "$output_file"
) &

将此脚本另存为
script.sh
并授予其可执行权限,即
chmod+x script.sh
现在执行
script.sh
将启动python脚本,并将stdout和stderr重定向到给定的输出文件。如果python脚本被终止,它也会重新启动它。

但是它在哪里运行呢。cron是否已经在服务器上安装了serverIts。您使用的是哪种服务器?在本教程中,您可以阅读标题为“设置和管理Cron作业”的部分,好的,所以基本上我的问题是如何在服务器上运行脚本?我知道cron-afterCron-Job会为您运行脚本。我将更新一个cron示例,以显示我的一个运行python脚本的crontab。是的,它将在我的笔记本电脑打开的情况下运行,但我可以将它放在什么服务器上?
#!/usr/bin/env bash

script_file=$(readlink -f "$0")  #absolute path to this file
python_file=$(readlink -f "$1")  #absolute path to the python script
output_file=$([[ "$2" == "" ]] && echo "/dev/null" || readlink -f "$2")  #absolute path to the output file

(
    exec 0>&- 0>/dev/null #close and redirect stdin
    exec 1>&- 1>>"${output_file}" #close and redirect stdout
    exec 2>&- 2>>"${output_file}" #close and redirect stderr
    python "$python_file" 
    "$script_file" "$python_file" "$output_file"
) &