update start.sh

This commit is contained in:
mshe 2026-06-08 15:37:14 +08:00
parent 872504f7f7
commit 6bab6d8b09
1 changed files with 3 additions and 34 deletions

37
app.py
View File

@ -29,51 +29,20 @@ async def run_spider_in_vnc(index: int) -> str:
Args:
index: VNC 显示编号
username: 用户名默认 user{index}
"""
if username is None:
username = f"user{index}"
try:
# 创建临时脚本
with tempfile.NamedTemporaryFile(mode='w', suffix='.sh', delete=False) as f:
f.write(f"""#!/bin/bash
PUBLIC_DIR="/shared"
username="{username}"
export DISPLAY=:{index}
echo "启动 VNC 服务"
sudo su - "$username" -c "vncserver :{index} -geometry 1280x800 -depth 24 -localhost no"
sudo su - "$username" -c "export DISPLAY=:{index} && xhost +" 2>/dev/null
sleep 3
cd $PUBLIC_DIR/learn-spider
sudo -E ./run.sh
sudo su - "$username" -c "vncserver -kill :{index}" 2>/dev/null
""")
script_path = f.name
# 执行脚本
os.chmod(script_path, 0o755)
# 调用外部 start.sh 脚本,传入 index 作为参数
process = await asyncio.create_subprocess_exec(
script_path,
"/home/dgs/vnc-server/start.sh",
str(index),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
# 清理临时文件
os.unlink(script_path)
if process.returncode == 0:
return f"✅ 爬虫执行成功\n输出:\n{stdout.decode()}"
else:
return f"❌ 爬虫执行失败\n错误:\n{stderr.decode()}"
except Exception as e:
return f"❌ 执行出错: {str(e)}"