问题描述
我正在尝试添加一个新的crontab来打开一个屏幕窗口,并在该窗口内执行php命令。
I'm trying to add a new crontab that open a screen window and execute php command inside this window.
我正在尝试什么:
sudo crontab -e
10 0 * * * * screen -d -m php /var/www/script.php
这不会创建屏幕,但是,如果我在-m之后删除,则会创建普通屏幕。
此代码返回我必须在终端上运行的
This doesn't create a screen, but, if I remove after -m, creates normal.This code returns that I have to run on a terminal
10 0 * * * * screen -d
我该怎么做?
可能吗?
How can I do this?It's possible?
谢谢。
编辑
已弄清楚。我要做的是将一些命令发送到附加的屏幕,例如:
Figured out. What I had to do is send some commands to attached screen, like this:
screen -S sessionname -X stuff 'command'`echo -ne '\015'`
echo -ne'\ \015'
模拟按Enter键。
echo -ne '\015'
emulates pressing the Enter key.
此处提到:
推荐答案
在cron中,您没有终端机(例如,请参见 )。屏幕要在终端中运行。在这种情况下运行它需要特殊处理。
In cron, you do not have a terminal (see for example Linux: Difference between /dev/console , /dev/tty and /dev/tty0). screen wants to run in a terminal. Getting it to run in that situation requires special handling.
根据其, -d
和 -m的组合
选项被特殊对待:
According to its documentation, the combination of -d
and -m
options is treated specially:
分离模式下的开始屏幕。这会创建一个新会话,但
没有附加到该会话。这对于系统启动
脚本很有用。
Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
在您的示例中,
php /var/www/script.php
是要运行的命令。它可能依赖设置环境变量来使其正常运行(例如特定的 PATH
)。在另一个问题中接受的答案显示了如何例如可以使用shell初始化( .profile
)来设置环境。您的shell可能使用 .bash_profile
等,但是原理是相同的。
is a command to be run. It may rely upon environment variables to be set to make it run properly (for instance a particular PATH
). The accepted answer in the other question shows how one might source the shell initialization (.profile
) for example, to set the environment. Your shell might use .bash_profile
, etc., but the principle is the same.
这篇关于运行屏幕和命令crontab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!