AWS Lambda 并不支持 nohup 命令,但可以通过使用 Python subprocess 模块运行命令,并将其在后台运行。例如,以下代码使用 subprocess 模块在后台运行 curl 命令:
import subprocess
def handler(event, context):
cmd = 'curl http://example.com > /dev/null 2>&1 &'
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, error = process.communicate()
return {'output': output, 'error': error}
这个示例将调用 curl 命令下载 example.com 的内容并将其发送到 /dev/null 中(以防输出进入控制台日志)。通过将 > /dev/null 2>&1 & 添加到命令的末尾,将可以将其放入后台运行。